Click to See Complete Forum and Search --> : replace when submit
Annaccond
03-25-2007, 06:59 PM
I need to put on my site form which would allow add some content to database. It have to be like simple guestbook but I want to it not add new records to database but replace those which already exist. In other words: I want to in database could be only 1 record (last submitted). How can I make it? :confused:
NightShift58
03-25-2007, 07:08 PM
Sure. Instead of using INSERT use the UPDATE instruction.
Annaccond
03-25-2007, 08:00 PM
Seems that I did something wrong because getting error. Here are codes:
FORM:
<form id="form1" name="form1" method="post" action="postfilename.php">
<input name="myTEXT" type="text" id="myTEXT" size="40" />
<input type="submit" name="Submit" value="Submit" /></td>
</form>
DATABASE:
CREATE TABLE `test_db` (
`myTEXT` varchar(65) NOT NULL default '',
PRIMARY KEY (`myTEXT`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
POST FILE:
$host="bla bla bla";
$username="bla bla bla";
$password="bla bla bla";
$db_name="bla bla bla";
$tbl_name="test_db";
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE INTO $tbl_name(myTEXT)VALUES('$myTEXT')";
$result=mysql_query($sql);
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
mysql_close();
NightShift58
03-25-2007, 08:04 PM
$sql="UPDATE $tbl_name (myTEXT) VALUES ('$myTEXT')"; In other words, drop the INTO.
If your table has no entries, your first time should be an INSERT. After that, UPDATE.
Annaccond
03-25-2007, 08:14 PM
Still getting error. What can be reason? Sorry, I'm n00b :(
NightShift58
03-25-2007, 08:40 PM
What does the error say?
Annaccond
03-25-2007, 08:49 PM
Nothing. I see only ERROR (command from post file).
Probably this whole code is wrong as for updating table after every submit. If someone could provide correct one I would be very grateful.
NightShift58
03-25-2007, 09:29 PM
<?php
$host="bla bla bla";
$username="bla bla bla";
$password="bla bla bla";
$db_name="bla bla bla";
$tbl_name="test_db";
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE $tbl_name(myTEXT)VALUES('" . $_POST['myTEXT'] . "')";
$result=mysql_query($sql);
if($result){
echo "Successful";
} else {
echo "ERROR";
}
?>
Annaccond
03-26-2007, 10:53 AM
Nothing changed, I still getting error :(
NightShift58
03-27-2007, 01:48 AM
<?php
$host="bla bla bla";
$username="bla bla bla";
$password="bla bla bla";
$db_name="bla bla bla";
$tbl_name="test_db";
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE $tbl_name (myTEXT) VALUES ('" . mysql_real_escape_string($_POST['myTEXT']) . "')";
$result=mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
if($result){
echo "Successful";
} else {
echo "ERROR";
}
?>
Annaccond
03-27-2007, 08:45 AM
Now I got:
SQL Error: UPDATE test_db (myTEXT) VALUES ('new message')
Something is wrong in your syntax next by '(myTEXT) VALUES ('new message')' in line 1
I tried to make code which would update just choosed row or id but it didn't workred too. Update of database seems to be harder than I thought. I hope to find other way to solve this problem :rolleyes:
NightShift58
03-27-2007, 11:19 PM
I must've been drunk the whole time...<?php
$host="bla bla bla";
$username="bla bla bla";
$password="bla bla bla";
$db_name="bla bla bla";
$tbl_name="test_db";
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE $tbl_name SET `myTEXT` ='" . mysql_real_escape_string($_POST['myTEXT']) . "'";
$result=mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
if($result){
echo "Successful";
} else {
echo "ERROR";
}
?>
MrCoder
03-28-2007, 04:37 AM
All work and no play hey Night?
Annaccond
03-28-2007, 05:03 AM
Last code working perfect. Thank you so much NightShift58 for all suggestions. That was really helpful :)
NightShift58
03-28-2007, 02:26 PM
All work and no play hey Night?No choice... married...