joshstevens19 Posted May 1, 2013 Share Posted May 1, 2013 hey guys rite i have created a website and i am trying to insert a indivual piece of data into 1 column of my phpmyadmin page table. But i am doing a TV show website and just creating a easy and basic forumn.. i am trying to insert a reply code which when you press reply it insert it into ID number 1 of column reply.. at the moment all it does it insert it into the end of another ID which it is creating can anyone help me here is my code? <?php if (isset($_POST['submitted'])) { $name = $_POST['entername']; $usernameorguest =$_POST['usernameorguest']; $reply =$_POST['reply']; $sqlinsert = "INSERT INTO `forumposts` WHERE (`POSTID`=1) INTO (`REPLY`) VALUES('$reply')"; $query = mysql_query($sqlinsert); if (!$query) { echo "error inserting new record" . mysql_error(); } } // end of mani if statment //$newrecord ="1 record added to the database"; ?> Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 i know i have undefined variables at the moment just trying to get it to insert into the correct box Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 1, 2013 Share Posted May 1, 2013 I've never seen SQL like that before. What DB are you using? MySQL (and every other SQL engine I've seen) would be: INSERT INTO `table` (column, column) VALUES (value, value) Quote Link to comment Share on other sites More sharing options...
InoBB Posted May 1, 2013 Share Posted May 1, 2013 (edited) your problem is your query string: $sqlinsert = "INSERT INTO `forumposts` WHERE (`POSTID`=1) INTO (`REPLY`) VALUES('$reply')"; shoudl be: $sqlinsert = "INSERT INTO `forumposts`(`REPLY`) VALUES('$reply')"; also, look into prepared statements http://php.net/manual/en/mysqli.prepare.php Edited May 1, 2013 by InoBB Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 sorry guys i knew i wrote it wrong.. okay i have replaced the code and now it comes up with this error? error inserting new recordYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `POSTID`=1' at line 1 what does this mean?? just want to insert the data in my valables into the result column in ID1 ? i dont understand why it will not work? thanks a lot josh Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 your problem is your query string: $sqlinsert = "INSERT INTO `forumposts` WHERE (`POSTID`=1) INTO (`REPLY`) VALUES('$reply')"; shoudl be: $sqlinsert = "INSERT INTO `forumposts`(`REPLY`) VALUES('$reply')"; also, look into prepared statements http://php.net/manual/en/mysqli.prepare.php I've never seen SQL like that before. What DB are you using? MySQL (and every other SQL engine I've seen) would be: INSERT INTO `table` (column, column) VALUES (value, value) sorry guys i knew i wrote it wrong.. okay i have replaced the code and now it comes up with this error? error inserting new recordYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `POSTID`=1' at line 1 what does this mean?? just want to insert the data in my valables into the result column in ID1 ? i dont understand why it will not work? thanks a lot josh Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 <?php if (isset($_POST['submitted'])) { $name = $_POST['entername']; $usernameorguest =$_POST['usernameorguest']; $reply =$_POST['reply']; $sqlinsert = "INSERT INTO `forumposts`(`REPLY`) VALUES('$reply') WHERE `POSTID`=1"; $query = mysql_query($sqlinsert); if (!$query) { echo "error inserting new record" . mysql_error(); } } // end of mani if statment //$newrecord ="1 record added to the database"; ?> this is my code now your problem is your query string: $sqlinsert = "INSERT INTO `forumposts` WHERE (`POSTID`=1) INTO (`REPLY`) VALUES('$reply')"; shoudl be: $sqlinsert = "INSERT INTO `forumposts`(`REPLY`) VALUES('$reply')"; also, look into prepared statements http://php.net/manual/en/mysqli.prepare.php Quote Link to comment Share on other sites More sharing options...
InoBB Posted May 1, 2013 Share Posted May 1, 2013 (edited) Are you trying to INSERT into a row that already exists? If is the case, you need to switch to an UPDATE query or, if it is a new row, remove the WHERE Edited May 1, 2013 by InoBB Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 Are you trying to INSERT into a row that already exists? If is the case, you need to switch to an UPDATE query or, if it is a new row, remove the WHERE it is a column that already exsists in my phpmyadmin table.. it is empty and this code should post it in to that column.. i change it to INSERT and it comes up with this error message error inserting new recordYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO `forumposts`(`REPLY`) VALUES('test') WHERE `POSTID`=1' at line 1 saying the code is incorrect.. when i have ran it without the insert of POSTID it insert but not on the one i want it to it creates a new ID as that is on ai and posts it in that section?? any ideas?? thanks josh Quote Link to comment Share on other sites More sharing options...
InoBB Posted May 1, 2013 Share Posted May 1, 2013 (edited) ok theres progress, simply change your SQL syntax to UPDATE: $sqlinsert = "UPDATE `forumposts` SET `REPLY` = $reply WHERE `POSTID` = 1"; Edited May 1, 2013 by InoBB Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 1, 2013 Share Posted May 1, 2013 (edited) I think you need to do some basic research on how the database works. What a column is, what a row is, what a table is, what phpMyAdmin is, what syntax goes with what query, etc. Cause none of what you're saying makes any sense. You should not re-use IDs. Edited May 1, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 ok theres progress, simply change your SQL syntax to UPDATE: $sqlinsert = "UPDATE `forumposts` SET `REPLY` = $reply WHERE `POSTID` = 1"; i have done and it brings up this error ? error inserting new recordYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`REPLY`) VALUES('pdijspdjf') WHERE `POSTID`=1' at line 1 Quote Link to comment Share on other sites More sharing options...
InoBB Posted May 1, 2013 Share Posted May 1, 2013 I think you need to do some basic research on how the database works. What a column is, what a row is, what a table is, what phpMyAdmin is, what syntax goes with what query, etc. Cause none of what you're saying makes any sense. You should not re-use IDs. Very true. Josh, read over the manual about sql syntax for a better understanding of how it works. http://dev.mysql.com/doc/refman/5.5/en/handler.html Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted May 1, 2013 Author Share Posted May 1, 2013 Very true. Josh, read over the manual about sql syntax for a better understanding of how it works. http://dev.mysql.com/doc/refman/5.5/en/handler.html okay thanks will do Quote Link to comment 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.