replace when submit

I’m new in PHP :oops: 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).

Those are my codes:

SUBMIT FORM:
[php]

[/php]

DATABASE:
[php]CREATE TABLE test_db (
myTEXT varchar(65) NOT NULL default ‘’,
PRIMARY KEY (myTEXT)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;[/php]

POST FILE:
[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=“INSERT INTO $tbl_name(myTEXT)VALUES(’$myTEXT’)”;
$result=mysql_query($sql);

if($result){
echo “Successful”;
}

else {
echo “ERROR”;
}

mysql_close();[/php]

It working but adding another records to database. I tried replace INSERT by UPDATE but I’m getting error. Could someone help me create working code, please?

What does your UPDATE query look like, and which errors are you getting? Also, have you tried the Update Syntax on the MySQL Manual yet?

I tried UPDATE and REPLACE. I just putted them in POST FORM file instead of INSERT INTO. As for error it’s just “ERROR” message (that one defined in POST FORM). I didn’t tried Update Syntax because I’m not sure how to use it.

I just need to new submitted message replacing message submitted earlier. Actually every submit adding another records to database, it;s like:

SUBMIT —> “1st message”
SUBMIT —> “1st message”,“2nd message”, …
SUBMIT —> “1st message”,“2nd message”,“3rd message”, … etc

and I need it:
SUBMIT —> “1st message”
SUBMIT —> “2nd message” (1st message is deleted and replaced by 2nd message)
SUBMIT —> “3rd message” (2nd message is deleted and replaced by 3rd message) etc

How can I make it correctly? I’m beginner and really need help :roll:

Okay, that’s possible. Next question: why in the world would you want that, as opposed to new entries in your database? And do you think you could go very far with a varchar defined at 65 characters? That means your total message would be 65 characters long, as most. That’s shorter than this post.

Sorry, but that’s just not gonna work.

As now I’m trying to this script work or to get any script which would do what I need. It’s just for test with 65 characters varchar but talking honestly even that would be enough because I need this script for temporary saving of very short messages (links). I thought about saving message in txt file instead of database but I expected with database it will be more easy… Seems that I was wrong :o

I’m actually thinking that a storage file is better suited for this (saves you the overhead of a MySQL server, not to mention the resources). Here’s a page that’ll get you started on reading/writing files: Remote Files.

Thank you for all tips. I’ll read more about it and try to do it that way.

Sponsor our Newsletter | Privacy Policy | Terms of Service