Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP&SQL Problems! $end error or unexpected ; error


ghost's Avatar
0 0
mysql_connect("localhost","root" , "password") or die (mysql_error());

mysql_select_db("tutorial1") or die (mysql_error());


$personaldata = array("name" => $_POST['name'], "age" => $_POST['age'], "sex" => $_POST['sex']);

mysql_query("INSERT INTO personalinformation (name, age, sex) VALUES('$personaldata[name]', '$personaldata[age]', '$personaldata[sex]')") or die(mysql_error());

echo "You've submited this information. <br> name:" .$personaldata['name']. "<br> Age:" .$personaldata['age']. "<br> Sex:" .$personaldata['sex'].

?>```

Heres my code. Now I must say that the sex option is a select menu in the html. 

The error I get is $end error. and when I add the ; at my last echo, I get unexpected ; on that last echo line, and the $end error is on the line of " ?>" which is weird. 

Thanks for any help you guys can give me!

ghost's Avatar
0 0

should be a semi colon at the end of your second last line, not a dot.


ghost's Avatar
0 0

Now I have a little bug. For some reason when I Submit the info. my last echo has a little problem seems like it not showing the information it's just blank as the last echo says it shouldn't. When I check my database. The only value I get is The sex value not the others :S

Thanks


ghost's Avatar
0 0

Try echoing all your $_POST variables, think something goes wrong there then.


ghost's Avatar
0 0

You've submited this information. name: Age: Sex: M

again only the sex is the seen one. Keep in mind that the sex is a select meny where name and age are text fields.


ghost's Avatar
0 0

try this

<?php
mysql_connect("localhost","root" , "password" or die (mysql_error());

mysql_select_db("tutorial1" or die (mysql_error());


$personaldata = array("name" => $_POST['name'], "age" => $_POST['age'], "sex" => $_POST['sex']);

mysql_query("INSERT INTO personalinformation (name, age, sex) VALUES('$personaldata['name']', '$personaldata['age']', '$personaldata['sex']'" or die(mysql_error());

echo "You've submited this information. <br> name:" .$personaldata['name']. "<br> Age:" .$personaldata['age']. "<br> Sex:" .$personaldata['sex'];

?>

ghost's Avatar
0 0

It's because of how you are inserting them into the database. You are trying to use the array with constants meaning that instead of using $array['key'] you are doing $array[key] which reference to two different values. So just do:

mysql_query("INSERT INTO  table (field, field, field) VALUES ('" . $array['key'] . "', '" . $array['key'] . "', '" . $array['key'] . "')");

That should insert it into the database correctly.