undefined add_topic_sql

Notice: Undefined variable: add_topic_sql on line 26

Warning: mysqli_query() [function.mysqli-query]: Empty query on line 26

$add_topic = “INSERT INTO name (name_first, name_last) VALUES (’$name_first’ , ‘$name_last’)”;

Line 26:
$add_topic_res = mysqli_query($mysqli, $add_topic_sql) or die (mysqli_error($mysqli));

I googled and searched for help on what exactly what $add_topic_res and $add_topic_sql is but didnt find any references. This is a simple database query i broke down from my PHP all in one book. I double checked to make sure my variables were being brought over from my html page, i double checked to make sure i was connecting to the database ok and i was able to echo $add_topic and get the fields ok. thoughts?

one more point for the newbie

$add_topic = “INSERT INTO name (name_first, name_last) VALUES (’$name_first’ , ‘$name_last’)”;

$add_topic_res = mysqli_query($mysqli, $add_topic_sql) or die (mysqli_error($mysqli));

needed to be

$add_topic_res = mysqli_query($mysqli, $add_topic) or die (mysqli_error($mysqli));

an walla. database insert, lesson learned; sometimes the book isnt always correct

Actually the book was correct.

The one is a query and the other is results. All your doing is rewriting over the same variable.

Had did something like -

$add_topic_res = “”;

$add_topic = “INSERT INTO name (name_first, name_last) VALUES (’$name_first’ , ‘$name_last’)”;

$add_topic_res = mysqli_query($mysqli, $add_topic_sql) or die (mysqli_error($mysqli));

You probably would have avoided this error. Also, you can turn off notices using error_reporting(). http://www.php.net for more information on what settings to use.

You just put error_report(“E_ALL”) at the top of your pages and this will force the error reporting setting in your php.ini file to whatever you want.

PS: E_ALL is very strict use the address I provided to see which on will allow you to turn off notices.

ah it rewrites the variable. that makes sense. and query and results, i was wondering what they meant by _res. i was only able to get it working using the same variable instead of rewriting another one. now i use add_topic_sql for the insert and add_topic_res for the command, works great.

Im going to spend my day attempting to display the topic results.
Thanks for the reply.

You mentioned to turn off errors… shouldn’t you leave all errors turned on when your new to programming?

Yes, you should leave on most errors. But there is a setting that will turn off Notices.

Its best to get rid of notices, but notices to not change the way the program will run. When I started I turned them off and as I became more proficient with PHP, I started to turn them on to get even a better understanding of it.

Can do as you wish, but when I started I just wanted to focus on getting the code working the way I wanted it to.

Sponsor our Newsletter | Privacy Policy | Terms of Service