php_guest Posted January 30, 2009 Share Posted January 30, 2009 I have a profile page where user can add new titles and value for it. Titles are showed on page as strings and value as text fields. The problem is that with every refresh new title and text field with value is inserted (duplicate of previous). Please tell me how to prevent INSERT INTO titles to be performed again. <br /<b>Add title:</b> <br /><br /> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST"> <table> <tr><td style='text-align: left; font-weight: bold'> Title</td> <td style='text-align: left; font-weight: bold'>Value</td></tr> <tr><td><input type="text" name="title"</td> <td><input type="text" name="value"</td> <td><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form> <?php $userID=$_GET[id]; if ( $_POST['value'] != '' && $_POST['title'] != '' && $_POST['submit'] != '') { mysql_query("INSERT INTO titiles (uTitle, uValue, UserID) VALUES ('$_POST[title]', '$_POST[value]', '$userID')"); header("location: thispage.php"); } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table> <?php connect(); if (isset($_POST)) { foreach ($_POST as $ID => $value ) { mysql_query ("UPDATE titles SET uValue = '$value' WHERE ID ='$ID'"); } } $ID =3; $query = "SELECT * FROM titles WHERE UserID = $ID"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "<br /><b>Edit data:</b> <br /><br />"; while($row = mysql_fetch_assoc($result)) { extract ($row); echo " <tr> <td><b>$uTitle</b></td> <td><input type='text' name='$ID' value='$uValue'></td> </tr>"; } ?> <tr><td><input type="submit" value="Submit"></td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 30, 2009 Share Posted January 30, 2009 Well that depends. Do you want there to be duplicates as long as they're not happening just because someone's refreshed the page? If not, you should check to see if the value exists in the database before trying to insert it. If you do allow duplicates, you could set a session variable when the form's successfully submitted and check the value of this variable prior to inserting a new row. Quote Link to comment Share on other sites More sharing options...
php_guest Posted January 30, 2009 Author Share Posted January 30, 2009 is normal that it try keeping to insert the values for each refresh if you are keeping user on the same page when clicking on Submit? How long does $_POST[] array keep the value? So actually you need to check every time if the value from $_POST[] is already inside table when you have POST method and keeping user on the same page? And second question what is more common: To check with if statement if the value is already inside table or to insert into table only unduplicated data? What is syntax for this? Thanks a lot Quote Link to comment 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.