How to disable writing to database if i made duplicate entry, and show warning in alertbox?
<p>I am a bit new with php, so I need help. On one page i have small form with firstname and lastname fields. On submit i send it to external .php script where i want to check if there is already person with same name in database. If there is already person with entered name in database, I want to cancel writing to database, and return to the html page. I have done this, but I want to display the JS messagebox "Person with this name already exists in db!", and if the writing to base was canceled, i want my input fields show last written values. I've been reading a lot of litertaure, and I'm lost. Thanks for help anyway :)</p>

<p>This is my form:</p>

<pre><code>&lt;form name="form" action="write.php" method="post" onSubmit="return Validate()"&gt;
Firstname: &lt;input type="text" name="firstname"&gt;
Lastname: &lt;input type="text" name="lastname"&gt;
&lt;input type="submit"&gt;
&lt;/form&gt;
</code></pre>

<p>My php code:</p>

<pre><code>&lt;?php
$con=mysqli_connect("localhost","root","","phptest");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['firstname'])) { 
$firstname = $_POST['firstname'];
}
if (isset($_POST['lastname'])) { 
$lastname = $_POST['lastname'];
}
$duplicate=mysqli_query($con,"SELECT * FROM persons WHERE FirstName='$firstname'");
if(mysqli_num_rows($duplicate)&gt;0)
{
die('Name already exists' );//I want that this message shows in html file, as alert
}
else{
$sql="INSERT INTO Persons (FirstName, LastName)
VALUES('$firstname','$lastname')";
$result=mysqli_query($con,$sql);
if (!$result)
{
die('Error: ' . mysqli_error());
}
}
header( 'Location://localhost/ivanda.php' ) ;
mysqli_close($con);

?&gt;
</code></pre>
