joshgarrod Posted January 26, 2008 Share Posted January 26, 2008 hello, I have a script that is supposed to update my homepage welcome message and title, when you go through to the admin section it is should pll the information from the table andput it into the text boxes for editing and then update the content. I can't work out what is wrong. please could someone take a look, thanks in advance. i am getting this error: ERROR: Duplicate entry '1' for key 1 INSERT INTO Welcome (Title, WelcomeText) VALUES ('Hello','Hello') Code <? $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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87921-what-is-wrong-with-my-script/ Share on other sites More sharing options...
sasa Posted January 26, 2008 Share Posted January 26, 2008 set your primary key to autoincriment type Quote Link to comment https://forums.phpfreaks.com/topic/87921-what-is-wrong-with-my-script/#findComment-449824 Share on other sites More sharing options...
joshgarrod Posted January 26, 2008 Author Share Posted January 26, 2008 cool, half way there, now it adds it too rather than replacing, also the text doesnt display in the text boxes to edit. Quote Link to comment https://forums.phpfreaks.com/topic/87921-what-is-wrong-with-my-script/#findComment-449827 Share on other sites More sharing options...
revraz Posted January 26, 2008 Share Posted January 26, 2008 You need two more querries for that. One to read the record and put it in your form, and then another to UPDATE the record. Quote Link to comment https://forums.phpfreaks.com/topic/87921-what-is-wrong-with-my-script/#findComment-449831 Share on other sites More sharing options...
joshgarrod Posted January 26, 2008 Author Share Posted January 26, 2008 ok, i didnt realise i needed a query to display information. i wouldn't know where to start. how would i structure the update statement? Quote Link to comment https://forums.phpfreaks.com/topic/87921-what-is-wrong-with-my-script/#findComment-449832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.