We can avoid duplicate values storing in table when we insert data to table. Because the duplicate entry is one of the main problem, while insert data into mysql table . We removed duplicate values using 'DISTINCT' key in mysql query, if it is present in table. We can check whether the value is present in table or not using PHP and Mysql before add data to table.
We can also avoid duplicate values storing in mysql table while inserting by primary key.
Consider following example. You've a database named with 'new' and table named with 'user'. You try to add names in user table. Now you've to check the values is present or not while you inserting. The table look like this:
Now you try to add name 'guru' again. You can check whether the name 'guru' is present in table or not using below PHP script.
Where,
mysql_query() - execute the query and select if any similar values are present in table. mysql_num_rows() - returns numeric value of selected rows. If its value is 0, then there is no similar values present in table. Otherwise the value is already present in table.
Where,
you can also use mysql_fetch_row() instead of mysql_num_rows().
If the value is present in table, it could not be stored in table. Now you can avoid duplicate entries in table using PHP and Mysql.
Related Post:
How to store values into mysql table using php
We can also avoid duplicate values storing in mysql table while inserting by primary key.
Consider following example. You've a database named with 'new' and table named with 'user'. You try to add names in user table. Now you've to check the values is present or not while you inserting. The table look like this:
Now you try to add name 'guru' again. You can check whether the name 'guru' is present in table or not using below PHP script.
<?php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','');
mysql_select_db('new');
$name=$_POST['name'];
$query=mysql_query("select * from user where name='".$name."' ") or die(mysql_error());
$duplicate=mysql_num_rows($query);
if($duplicate==0)
{
$query1=mysql_query("insert into user values('".$name."')") or die(mysql_error());
}
else
{
echo'The name '.$name.' is already present in the user table';
}
}
?>
<html>
<body>
<form method='post' action='#'>
name: <input type='text' name='name'>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','');
mysql_select_db('new');
$name=$_POST['name'];
$query=mysql_query("select * from user where name='".$name."' ") or die(mysql_error());
$duplicate=mysql_num_rows($query);
if($duplicate==0)
{
$query1=mysql_query("insert into user values('".$name."')") or die(mysql_error());
}
else
{
echo'The name '.$name.' is already present in the user table';
}
}
?>
<html>
<body>
<form method='post' action='#'>
name: <input type='text' name='name'>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
Where,
mysql_query() - execute the query and select if any similar values are present in table. mysql_num_rows() - returns numeric value of selected rows. If its value is 0, then there is no similar values present in table. Otherwise the value is already present in table.
Where,
you can also use mysql_fetch_row() instead of mysql_num_rows().
If the value is present in table, it could not be stored in table. Now you can avoid duplicate entries in table using PHP and Mysql.
Related Post:
How to store values into mysql table using php
Thanks it worked...
ReplyDeletethank you so much it help me alot please check my blog
ReplyDeletealicomputertips.blogspot.com
no blogs
DeleteThis comment has been removed by the author.
ReplyDeleteHello Friends,
ReplyDeletePlease I solicit your kind review. I wrote the code below to help me check for duplicate record entry so my users will not enter same record twice in a year buts the code is neither posting the record not checking if there is any duplicate entries.
Here is how the system work. The application on its own allows multiple entries but once in a year. Every year members of a church gather for a conference and the system is used to capture their details. So for every participants, the system will capture their detail for that year once. If a participants details is entered twice the system should check the RegNo and Year fields to be sure the record has not been posted for that year. But its not working
Please I need help on this.
if(isset($_POST['submit'])) {
{
$con = mysql_connect('localhost', 'root', '', 'rmwworg_dbms') or die('Error connecting to MySQL server');
$Camp = $_POST['Camp'];
$Hostel = $_POST['Hostel'];
$Validate = $_POST['Validate'];
$Event_Title = $_POST['Event_Title'];
$Regno = $_POST['regno'];
$Title = $_POST['Title'];
$Surname = $_POST['surname'];
$OtherNames = $_POST['OtherNames'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$Age = $_POST['Age'];
$State = $_POST['State'];
$Country = $_POST['Country'];
$Assembly= $_POST['Assembly'];
$Sex = $_POST['Sex'];
$MaritalStatus = $_POST['MaritalStatus'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$photo2 = $_POST['photo2'];
$datetime = $_POST['datetime'];
$Year = $_POST['Year'];
$validatedby = $_POST['validatdby'];
$category = $_POST['category'];
$result = mysql_query("SELECT * FROM validatedparticipants WHERE RegNo = '$regno' AND Year = year(now())"); //check for duplicates
$num_rows = mysql_num_rows($result); //number of rows where duplicates exist
if($num_rows==0) { //if there are no duplicates...insert
{
$sql="INSERT INTO validatedparticipants (Camp, Hostel, Validate, Event_Title, RegNo, Title, Surname, OtherNames, Address, City, Age, State, Country, Assembly, Sex, MaritalStatus, Phone, Email, photo2, datetime, Year, validatdby, category)
VALUES ('$Camp', '$Hostel', '$Validate', '$Event_Title', '$regno', '$Title', '$surname', '$OtherNames', '$Address', '$City', '$Age', '$State', '$Country', '$Assembly', '$Sex', '$MaritalStatus', '$Phone', '$Email', '$photo2', '$datetime','$Year','$validatedby','$category')";
$result1 = mysql_query($con, $sql) or die('Error querying database.');
mysql_close($con);
}
}
else
{
echo"The Participant added";
}
}
header("valpartcard.php?RegNo=" . $row_rsValidateParticipant['RegNo'] . "");
}
If you want this feature, first of all you need store time of entry. Then before you are going to entry data, you have to check.
DeleteTry this query:
Deletemysql_query("SELECT * FROM validatedparticipants WHERE RegNo = '". $regno. "' AND Year = year(now())") or die(mysql_error());
for more please visit www.indiatriptravel.com
ReplyDeleteThank for posting helping phases.
ReplyDeleteCod Benzeri Oyunlar
ReplyDeleteTerraria Benzeri Oyunlar
Escape Benzeri Oyunlar
Ark Benzeri Oyunlar
Max Payne Benzeri Oyunlar
YYF