mysql insert into a specific row
I am new to mysql and would like to ask a question - hopefully someone can help me. Here is my current code:
<?
$title=$_POST['name'];
$text=$_POST['text'];
mysql_connect("localhost", "test", "test") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$location')");
Print "Your information has been successfully added to the database.";
?>
I would like to achieve that the data inserted g开发者_如何学Gooes into the column where my ID is 1.
Thank you very much, Nareen
Well then you should be doing an update, so long as your ID already exists. e.g.
UPDATE data D
SET D.YourColumn = @value
WHERE D.ID = @id;
...otherwise nothing will update. Might help to read a bit about the UPDATE
statement -- I've heard it's an essential piece of knowledge to database development. ;)
Sounds like you need an UPDATE
statement.
mysql_query("UPDATE `data` SET `field1`='".mysql_real_escape_string($name)."', `field2`='".mysql_real_escape_string($email)."', `field3`='".mysql_real_escape_string($location)."' WHERE `ID` = 1");
You should always use mysql_real_escape_string
when data comes from users (for instance when using $_GET
, $_POST
, `$_REQUEST, etc.) to make sure it's safe.
精彩评论