Jump to content

[SOLVED] $_POST and submit problem


php_guest

Recommended Posts

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/143153-solved-_post-and-submit-problem/
Share on other sites

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.