Jump to content

Recommended Posts

if ($_REQUEST['name'] != "") {

$code = $_REQUEST['name'];

 

$query="INSERT INTO `friends` (`name`) values ('" . mysql_real_escape_string($name) . "')";

 

$result=mysql_query($query) or die(mysql_error());

echo("Inserted " . htmlentities($name) ." into database");

} else {

echo("<p>Please enter a name</p>");

}

?>

 

--

 

the database has the unique function already so it knows not to allowmultiple entries that are the same.

 

That's my code, how do I make it so that if that name is already there it refreshes page (shows everything still) but says taht that name was already used instead of saying "Duplicate entry 'frank' for key 1"

 

So like right above where they wrote their name they get a message saying "name already added to list."

 

thanks ;)

Link to comment
https://forums.phpfreaks.com/topic/167630-warning-users-of-duplicate-entry/
Share on other sites

You should ALWAYS reload the page after an insert or update query. This prevents duplicate entries being inserted or mysql errors if a user ever clicked refresh

 

i.e

if($_POST['submit']) {
// insert record
mysql_query("INSERT INTO ......");
// reload page
header("Location:index.php");
exit();
}

I dont know how I can explain it easier.

 

If you ever insert into a database or update a record always reload the page using the header() function after the query. This will prevent multiple records being inserted if the user was to click the refresh button in their browser (it will keep running over the same bit of code). You only run the queries when a user clicks the form submit button right? So if you reload the page the form is not in submit mode.

 

To check if a name already exists then you would write a SELECT query prior to an insert query. See if the name exists from the SELECT query i.e. SELECT id FROM table WHERE name='neil'. If a record is returned then you throw an error and don't continue to insert the record.

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.