[CS-FSLUG] Preventing a form re-posting PHP/MYSQL

Chris Brault gginorio at sbcglobal.net
Fri Jun 16 20:14:19 CDT 2006


Let me see if I can help,

> I am busy learning MYSQL and PHP and I have created a simple
> page that displays current database entries and provides a form to
> add another one on the same page.
> Also, any idea how to prevent somone from posting a blank data in the form ?
> 
> =========================================================
> <?php
> // Displays memotext
> include 'includes/result_memo.inc.php';
> ?>
> 
> <form action ="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
> <label>Enter new memo here:<br />
> <textarea name="memotext" rows="10" cols="40">
> </textarea></label><br />
> <input type="submit" value="Submit memo" />
> </form>
> 
> 
> <?php
>    $memotext=$_POST['memotext'];
>    $sql = "INSERT INTO memo set
>    memotext = '$memotext',
>    memodate = CURDATE(),
>    memotime = CURTIME()";
>    if (!mysql_query($sql)) { echo "<p>Memo addition failed ' .
> mysql_error() . '</p>"; }
> ?>
> 
> ===========================================================

You could try querying the database to see if the date and time already 
exist. My SQL commends may be different from yours but you get the point.

<?php
   if (!mysql_query(SELECT * FROM memo WHERE memotime=$last_time && 
!mysql_query(SELECT * FROM memo WHERE memodate=$last_date) {
   $memotext=$_POST['memotext'];
   $sql = "INSERT INTO memo set
       memotext = '$memotext',
       memodate = CURDATE(),
       memotime = CURTIME()";
   $last_time=CURTIME();
   $last_date=CURDATE();
   if (!mysql_query($sql)) { echo "<p>Memo addition failed '. 
mysql_error() . '</p>"; }
   else {$last_time=CURTIME(), $last_date=CURDATE();}
?>

If I am correct, it won't be able to post an entry with the same time 
and date as the previous memo.

Gabe Ginorio




More information about the Christiansource mailing list