Takson Posted February 26, 2011 Share Posted February 26, 2011 I am new to PHP and am currently learning the ropes. I have writen some code to insert a line into a mySQL database. I have created 3 fields in the mySQL database and am passing values two them. I can use a declared variable to pass information to the filed ID (Index filed in mySQL) but if I use a variable to pass purely text values the database is not updated. mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ($category, $internalId, $category2)" ); If I replace the variables with specific text, the rows are added successfully. mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ('werwerwer', $internalId, 'werrr')" ); It must be something stupid, I am sure. Full code below ob_start(); $host="localhost:8888"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="expenses"; // Database name $tbl_name="expense_category"; // Table name $category="test3"; $internalId="7"; $category2="test3"; // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ('werwerwer', $internalId, 'werrr')" ); echo "Done now 6!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/228878-using-variables-as-part-of-insert-statement/ Share on other sites More sharing options...
DavidAM Posted February 26, 2011 Share Posted February 26, 2011 When you put string data in an SQL statement, it has to have quotes around it. You put quotes around the literals here: mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ('werwerwer', $internalId, 'werrr')" ); You have to do it here as well: mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ('$category', $internalId, '$category2')" ); Quote Link to comment https://forums.phpfreaks.com/topic/228878-using-variables-as-part-of-insert-statement/#findComment-1179873 Share on other sites More sharing options...
SaMike Posted February 26, 2011 Share Posted February 26, 2011 I would also seperate your variables with {} like so: mysql_query( "INSERT INTO $tbl_name (category_Name, ID, test) VALUES ('{$category}', {$internalId}, '{$category2}')" ); i think its good coding practice to do this? could be wrong tho Quote Link to comment https://forums.phpfreaks.com/topic/228878-using-variables-as-part-of-insert-statement/#findComment-1179900 Share on other sites More sharing options...
Takson Posted February 26, 2011 Author Share Posted February 26, 2011 Thank you very much guys, I have just successfully completed my first PHP Web Service integration. 2 fields * 3 records only, but it has worked. By this time tomorrow....... Quote Link to comment https://forums.phpfreaks.com/topic/228878-using-variables-as-part-of-insert-statement/#findComment-1179915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.