Welcome to WebmasterWorld Guest from 54.167.86.132

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP error

     
4:13 pm on Oct 6, 2012 (gmt 0)

New User

joined:Oct 6, 2012
posts: 1
votes: 0


<?php
include once('config.php');

$link=mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);

if (!$link)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db(DB_DATABASE);

$query="INSERT INTO".data_feedback."(First Name,Last Name,Email,Telephone,Comments)
VALUES
('". $_POST['firstname']."','". $_POST['lastname']."','". $_POST['email']."',". $_POST['telephone']."','". $_POST['comments']."')"

$data=mysql_query($query);
if($data)
{
echo"Feedback is recieved";
}
else{
echo"There is an error";
}

?>


Ive got a syntax error on line 17.
This is the part:$data=mysql_query($query);
Im new to php so i cant figure it out,can anyone help
5:10 pm on Oct 6, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


Try a semicolon on the previous line.

But your code is a pretty good example of how not to access a database from a security perspective.
ref: [xkcd.com...]
You have to do input validation.

I'd also suggest to not learn to program using the mysql interface anymore (it's obsolete) and use the mysqli one instead (note the i), and prepared statements instead.
4:07 am on Oct 8, 2012 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14643
votes: 93


This is one of the few times a syntax error was doing you a favor by stopping unfiltered web input from getting to your SQL server.
7:40 pm on Oct 8, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


^^^

I agree with Incredibill & Swa66;

But just to clarify, you've got a concatination issue:-

'". $_POST['firstname']."',
'". $_POST['lastname']."',
'". $_POST['email']."',
". $_POST['telephone']."',
'". $_POST['comments']."'

Splitting it like this REALLY highlights your syntax error. And it's just one char too..

I prefer giving a clue to a problem, rather than telling you that you're fundamentally flawed for the third time :)

Cheers,
MRb

11:03 pm on Oct 8, 2012 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14643
votes: 93


Now that it's Monday I'll get serious here as there's other issues such as directly accessing the values without testing to see if they were passed:
$_POST['lastname']

You need to test variable presence using isset() or empty() first to make sure all the parameters were passed otherwise you'll get errors accessing them in the SQL statement as well.

Even if you're doing data validation on the page using Javascript it's insufficient, especially for those of us using NoScript in Firefox or other browser equivalents, and leaves vulnerabilities in the server side code.
6:41 pm on Oct 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


^^^

To add to this, you can sort out ALL of your $_POST vars by passing them through the array_map() - and using it's callback mechanisim will save time and lines of code too.

Then using something like mysql_real_escape_string, strip_tags, trim you can make the data a little safer.

Cheers,
MRb
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members