I have a select option of categories fetched from mysql and text box to enter value for that category and submit button to enter that value in database as follows.
Now after selecting category i enter into text box some value for that particular category only and hit submit button. But before i insert, i want to check for duplicate value for that category, so when i hit submit button it should check database for values in db for category selected and if value is already there than show error else insert into db.
My code
index.php
<form action="action.php" enctype="multipart/form-data" method="post">
<div class="row">
<div >
<label>Category</label>
<br>
<select name="cat" id="cat" style="width:100%;height:0.27in" required >
<option value="0000" selected="selected">Select cat</option><?php
include ('connection.php');
$qry_sel_dept="select * from general_master where name_cd = 'Ptype' ";
$qry_sel_dept_exe=mysql_query($qry_sel_dept);
while($fetch=mysql_fetch_array($qry_sel_dept_exe)){
?>
<option value="<?php echo $fetch['c_id'];?>" ><?php echo $fetch['category'];?></option>
<?php }
?></select>
<input type="text" name="te" id="te" />
<input type="submit" name="submit" id="submit" />
action.php
<?php
include('./../connection.php');
if(isset($_POST['submit'])){
$cat=$_REQUEST['cat'];
$te=$_REQUEST['te'];
$sqlc= mysql_query("INSERT INTO general_master set category='".$cat."' , name= '".$te."' " );
}
?>
mysql Table contains
c_id
category
name
Can it be done using javascript and ajax? How?
Any help will be greatly appreciated. Thanks in advance
action.php- RiggsFolly"SELECT COUNT(*) as knt WHERE category='$cat' AND name= '$te'"query using those 2 columns in the search criteria. If you get a count of 1 it already exists, if 0 returned it does not exist. SQL 102 - RiggsFollyUNIQUEto the field you don't want to have duplicated values. You don't need to check if there are duplicated values as long as the key is ready to avoid duplications by system: dev.mysql.com/doc/refman/5.7/en/constraint-primary-key.html - Marcos Pérez Gude