wright67uk Posted November 29, 2012 Share Posted November 29, 2012 I'm trying to insert some form results into a table. I've tested my connection, and the query echos out fine. I understand that, I need to sanitize my variables, but for now I'm trying to figure out why nothing gets inserted into my database. Does my code seem to be ok, or is this most likely down to the way my table has been setup? <?php session_start(); include 'connection.php'; $insert_query = ("insert into users(name, email, phone, postcode, type, start, user, password, tac) values ( " . $_SESSION['name'] . ", " . $_SESSION['email'] . ", " . $_SESSION['phone'] . ", " . $_SESSION['postcode'] . ", " . $_SESSION['type'] . ", " . $_SESSION['start'] . ", " . $_POST ['user'] . ", " . $_POST ['password'] . ", " . $_POST ['tac'] . " ) ") or die(mysql_error()); echo "Data Inserted!"; echo $insert_query; mysql_query($insert_query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/ Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Strings in SQL need quotes. Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396103 Share on other sites More sharing options...
wright67uk Posted November 29, 2012 Author Share Posted November 29, 2012 So more like the below? <?php session_start(); include 'connection.php'; $insert_query = ("insert into users('name', 'email', 'phone', 'postcode', 'type', 'start', 'user', 'password', 'tac') values ( " . $_SESSION['name'] . ", " . $_SESSION['email'] . ", " . $_SESSION['phone'] . ", " . $_SESSION['postcode'] . ", " . $_SESSION['type'] . ", " . $_SESSION['start'] . ", " . $_POST ['user'] . ", " . $_POST ['password'] . ", " . $_POST ['tac'] . " ) ") or die(mysql_error()); echo "Data Inserted!"; echo $insert_query; mysql_query($insert_query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396104 Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Right idea, wrong place. Quotes around the strings you're inserting. The list of fields you're inserting into don't always have to be quoted, but if you do then use backticks ` for them. Regular double " and single ' quotes are strictly (and necessary) for string values. INSERT INTO table (field1, field2, field3) VALUES ("string 1", "string 2", "string 3") INSERT INTO table (`field`, `field2`, `field3`) VALUES ('string 3', 'string 4', 'string 5') Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396108 Share on other sites More sharing options...
wright67uk Posted November 29, 2012 Author Share Posted November 29, 2012 " . $_SESSION['name'] . ", sorry if this isn't sinking in, but are you saying I should have more quotes around the example? Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396115 Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 No, I'm saying you put the quotes in the wrong place. Compare where the quotes are in the examples I posted with where the quotes are in your code. Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396124 Share on other sites More sharing options...
NomadicJosh Posted December 1, 2012 Share Posted December 1, 2012 (edited) Also, I want to suggest using a function like: function isSessionSet($caller) { if(isset($_SESSION[$caller])) { return $_SESSION[$caller]; } } How to use: $query = "insert into users(name, email) values ( '" . isSessionSet('name') . "', '" . isSessionSet('email') . "' )" or die(mysql_error()); This will prevent your error logs from being crowded with unnecessary "index" errors. Edited December 1, 2012 by parkerj Quote Link to comment https://forums.phpfreaks.com/topic/271332-inserting-session-variables-into-mysql/#findComment-1396671 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.