seany02 Posted April 26, 2009 Share Posted April 26, 2009 i want to insert values into a single column of a a table. ive attempted it with this; <?php $db = @mysql_connect("localhost","root",""); if (!$db) { do_error("Could not connect to the server"); } // Connect to the database @mysql_select_db("Rufus" ,$db)or do_error("Could not connect to the database"); // Run query $name=$_POST["secondtxt"]; $name2=$_POST["thirdtxt"]; $result = mysql_query("INSERT INTO project WHERE project_id='$name' (user_id) VALUE ('$name2)",$db); ?> however the column doesnt appear changed in the database. Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/ Share on other sites More sharing options...
wildteen88 Posted April 26, 2009 Share Posted April 26, 2009 Are you wanting to make changes to an existing record in the table? if so you'll want to run an UPDATE query. INSERT INTO is for creating new records only. $sql = "UPDATE project SET user_id='$name2' WHERE project_id='$name'"; Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819703 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 yeah thats exactly what i want to do. i tried that line, it made no change to the database, no errors were displayed on screen Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819711 Share on other sites More sharing options...
wildteen88 Posted April 26, 2009 Share Posted April 26, 2009 Did you just add that line, then tested your code immediately after? If you noticed I assigned the query to a variable called $sql. You'll need pass this variable to mysql_query() in order for the query to execute. Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819714 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 i just changed the line. how do i pass it to the mysql_query()??? my ineptness is shocking Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819716 Share on other sites More sharing options...
wildteen88 Posted April 26, 2009 Share Posted April 26, 2009 Just like you have already done with $db variable in your original code. mysql_query($sql, $db) or die('ERROR!<br />Query: '.$sql.'<br />'.mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819717 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 i changed my code to this, it didnt work. anything else you can do for me? <?php $db = @mysql_connect("localhost","root",""); $sql = "UPDATE project SET user_id='$name2' WHERE project_id='$name'"; if (!$db) { do_error("Could not connect to the server"); } // Connect to the database @mysql_select_db("Rufus" ,$db)or do_error("Could not connect to the database"); // Run query $name=$_POST["secondtxt"]; $name2=$_POST["thirdtxt"]; $result = mysql_query("$sql,VALUE ('$name2)",$db); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819731 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 and i tried this <?php $db = @mysql_connect("localhost","root",""); $sql = mysql_query; if (!$db) { do_error("Could not connect to the server"); } // Connect to the database @mysql_select_db("Rufus" ,$db)or do_error("Could not connect to the database"); // Run query $name=$_POST["secondtxt"]; $name2=$_POST["thirdtxt"]; $sql = "UPDATE project SET user_id='$name2' WHERE project_id='$name'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819736 Share on other sites More sharing options...
wildteen88 Posted April 26, 2009 Share Posted April 26, 2009 Umm, just add the line I provided in my post here after the following line $sql = "UPDATE project SET user_id='$name2' WHERE project_id='$name'"; Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819737 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 I think this is what you mean. but i now get this error in the browser ERROR! Query: Query was empty <?php $db = @mysql_connect("localhost","root",""); if (!$db) { do_error("Could not connect to the server"); } // Connect to the database @mysql_select_db("Rufus" ,$db)or do_error("Could not connect to the database"); // Run query $name=$_POST["secondtxt"]; $name2=$_POST["thirdtxt"]; mysql_query($sql, $db) or die('ERROR!<br />Query: '.$sql.'<br />'.mysql_error()); $sql = "UPDATE project SET user_id='$name2' WHERE project_id='$name'"; ?> i do appreciate your help alot, i know im very difficult sometimes Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819756 Share on other sites More sharing options...
wildteen88 Posted April 26, 2009 Share Posted April 26, 2009 Did you read my post correctly. I said add that line after the line I specified in my post. if you understood the PHP syntax correctly you would of understood what you've just done wrong. Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819761 Share on other sites More sharing options...
seany02 Posted April 26, 2009 Author Share Posted April 26, 2009 thanks it does work. the post with the hyperlink in wasnt very clear, maybe its my eyes only been working on the site for 12 hours. perhaps it would have been easier to say swap the lines over, not imply that i dont understand the syntax. i did memtion i was php inept earlier, and wouldnt be on this forum if i did. thanks again, especially for your time. Quote Link to comment https://forums.phpfreaks.com/topic/155731-solved-is-it-possible-to-insert-values-into-a-single-column/#findComment-819772 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.