Jump to content

How do I show error messages on the input form


loozah

Recommended Posts

I'm not sure if I'm posting in the proper area for this question, if not I apologize... I am trying to place the "Duplicate entry" error next to the text input field in the script below.  The script works fine but if the input is duplicated of course it dies when the mysql_error is reached.  I need it to show the form input field with the date still in the field and something like "That input is already in the database", I have done multiple searches but not sure if I'm wording my searches properly... still a newbie at this stuff. Bye the way the column is set to "Unique" in the db to prevent duplicates.

Thank in advance for any help

 

<?php

 

include 'my_connect.php';

 

if ($_POST['submit'])

{

$title = $_POST['title'];

 

mysql_query("INSERT INTO my_dbtable (title)

VALUES ( '$title')") or die(mysql_error());

}

?>

 

 

<form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input name="title" type="text" value="" />

<label>

  <input type="submit" name="submit" id="submit" value="submit" />

</label>

</form>

 

This seems to be working...

 

<?php

 

include 'my_connect.php';

 

if ($_POST['submit'])

{

$title = $_POST['title'];

 

$result = mysql_query("SELECT * FROM my_dbtable");

while($row = mysql_fetch_array($result))

 

if ($title == $row['title'])

$error = "YerSoooFukt!";

 

else

 

mysql_query("INSERT INTO my_dbtable (title)

VALUES ( '$title')") or die(mysql_error());

 

}

?>

 

 

<form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input name="title" type="text" value="" />

<label>

  <input type="submit" name="submit" id="submit" value="submit" />

</label>

<?php echo $error; ?></form>

 

Thanks for the speedy assistance

<?php
include 'my_connect.php';
$msg = '';
if ($_POST['submit'])
{
      $title = $_POST['title'];
      $res = mysql_query("select * from my_dbtable where title='$title");
      if(mysql_num_rows($res)) 
      {
           $msg = "Duplicate entry";
      }
      else   
      {
          mysql_query("INSERT INTO my_dbtable (title) VALUES ( '$title')") or die(mysql_error());
       }
}
?>

 

<form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="title" type="text" value="<?php if(isset($_POST)) echo $_POST['title'];?>" /> <?php if(isset($msg)) echo $msg;?>
<label>
  <input type="submit" name="submit" id="submit" value="submit" />
</label>
</form>

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.