joshgarrod Posted January 26, 2008 Share Posted January 26, 2008 Hi, I am building a mini backend cms for a website and I want to be able to update the homepage welcome message by filling in a form, but I am gettin the following error: ERROR: Duplicate entry '1' for key 1 INSERT INTO Welcome (Title, WelcomeText) VALUES ('Hello','Hello') Can someone please have a look, thanks in advance. Below is m code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Add stock</title> </head> <body> <p><font face="Arial, Helvetica, sans-serif"> <? $usr = "username"; $pwd = "password"; $db = "database"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> </font></p> <TITLE></TITLE> <P>Update welcome message </P> <font face="Arial, Helvetica, sans-serif"> <? # this is processed when the form is submitted # back on to this page (POST METHOD) if ($REQUEST_METHOD=="POST") { # double-up apostrophes $Title = str_replace("'","''",$Title); $WelcomeText = str_replace("'","''",$WelcomeText); # setup SQL statement $SQL = " INSERT INTO Welcome "; $SQL = $SQL . " (Title, WelcomeText) VALUES "; $SQL = $SQL . " ('$Title','$WelcomeText') "; #execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); $ID=mysql_insert_id(); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P>Welcome message updated</P>\n"); } ?> </font> <FORM NAME="fa" ACTION="index.php?page=welcomeMessage" METHOD="POST"> <TABLE> <TR> <TD><font face="Arial, Helvetica, sans-serif"><B>Title: </B> </font></TD><TD><font face="Arial, Helvetica, sans-serif"> <INPUT NAME="Title" TYPE="text" id="Title" value="<?php echo $Title; ?>" SIZE=40> </font></TD></TR> <TR> <TD height="96" VALIGN=TOP><font face="Arial, Helvetica, sans-serif"><B>Welcome message : </B> </font></TD><TD><p><font face="Arial, Helvetica, sans-serif"><textarea name="WelcomeText" cols=40 rows=5 id="WelcomeText"><?php echo $WelcomeText; ?></textarea> </font></p> </TD> </TR> <TR> <TH COLSPAN=2><font face="Arial, Helvetica, sans-serif"> <input name="submit" type="submit" value="Update message" /> </font></TH> </TR> </TABLE> <p align="center"> </p> </FORM> <font face="Arial, Helvetica, sans-serif"> <? mysql_close($cid); ?> </font> </BODY> </HTML> </body> </html> Link to comment https://forums.phpfreaks.com/topic/87918-help-cms-script-updating-of-entries-in-database/ Share on other sites More sharing options...
interpim Posted January 26, 2008 Share Posted January 26, 2008 If the row already exists in the database, change your query to an UPDATE, and in your SQL statement "UPDATE Welcome WHERE Title ='title'" Since it seems like your Title is your primary key, you cannot insert a new row with the same value as an already existant Primary ID. If you need to change the title as well, then I would suggest adding another field to your database and set it as your unique id. Link to comment https://forums.phpfreaks.com/topic/87918-help-cms-script-updating-of-entries-in-database/#findComment-449838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.