Jump to content

using query does not insert into the phpmyadmin table?


Recommended Posts

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";
 
 
 
 
 
?>

 

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

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

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

<?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

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

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.

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.