Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.