Reputation: 21
<?php
date_default_timezone_set('Asia/Manila');
$conn=mysql_connect("localhost","root","");
mysql_select_db("dbposo",$conn);
if(isset($_POST['add']))
{
if( empty($_POST['fname']) || empty($_POST['mname']) || empty($_POST['lname']))
{
echo "<script>alert('Please fill up all the fields!')</script>";
header("Refresh: 0;url=admin_publicviolation.php");
}
else
{
$number=$_POST['number'];
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$age=$_POST['age'];
$gender=$_POST['gender'];
$violation=$_POST['violation'];
$officer=$_POST['officer'];
$offenses=$_POST['offenses'];
$date=date("y-m-d");
$time=date("H:i:s");
$price=$_POST['price'];
mysql_query("insert into tblcitizen(number,fname,mname,lname,age,gender,violation,officer,offenses,date,time, price) values('$number','$fname','$mname', '$lname', '$age', '$gender', '$violation' , '$officer', '$offenses','$date','$time','100')",$conn) or die(mysql_error());
echo "<script>alert('Successfully Recorded, First penalty: 100php!')</script>";
header("Refresh: 0;url=admin_publicviolation.php");
}
}
?>
i want to reject registration if the $number is already exists in the database, somebody help me? can't figure it out. i can't use some coding method. don't know how to use other
Upvotes: 0
Views: 3205
Reputation: 38
<?php
date_default_timezone_set('Asia/Manila');
$conn=mysql_connect("localhost","root","");
mysql_select_db("dbposo",$conn);
if(isset($_POST['add']))
{
if( empty($_POST['fname']) || empty($_POST['mname']) || empty($_POST['lname']))
{
echo "<script>alert('Please fill up all the fields!')</script>";
header("Refresh: 0;url=admin_publicviolation.php");
}
// Check if already exists
$number = $_REQUEST['number'];
$result = mysql_query("SELECT * FROM tblcitizen WHERE number = '$number' ");
if (mysql_num_rows($result)==0) {
// Go ahead and insert everything in table
$number=$_POST['number'];
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$age=$_POST['age'];
$gender=$_POST['gender'];
$violation=$_POST['violation'];
$officer=$_POST['officer'];
$offenses=$_POST['offenses'];
$date=date("y-m-d");
$time=date("H:i:s");
$price=$_POST['price'];
mysql_query("insert into tblcitizen(number,fname,mname,lname,age,gender,violation,officer,offenses,date,time, price) values('$number','$fname','$mname', '$lname', '$age', '$gender', '$violation' , '$officer', '$offenses','$date','$time','100')",$conn) or die(mysql_error());
echo "<script>alert('Successfully Recorded, First penalty: 100php!')</script>";
header("Refresh: 0;url=admin_publicviolation.php");
echo 'Your data was sent';
}
else
{
echo "Number already exists ";
}
}
?>
Upvotes: 0
Reputation: 34677
ALTER TABLE tblcitizen ADD CONSTRAINT tblcitizen UNIQUE (number);
Now, if the number already exists, the insert will fail and you'll need to check mysql_error to know how to proceed. If you're using the non-deprecated mysqli or PDO interfaces, the method to check are mysqli_error or PDO::errinfo which work the same way.
Upvotes: 1
Reputation: 1020
first use the select query which is AND please use mysqli not mysql
$query = mysql_query("SELECT * FROM tblcitizen WHERE number = '".$number."'");
$fetch_rows = mysql_num_rows($query);
if ( $fetch_rows > 0 )
{
// then do something
} else {
mysql_query("insert into tblcitizen(number,fname,mname,lname,age,gender,violation,officer,offenses,date,time, price) values('$number','$fname','$mname', '$lname', '$age', '$gender', '$violation' , '$officer', '$offenses','$date','$time','100')",$conn) or die(mysql_error());
echo "<script>alert('Successfully Recorded, First penalty: 100php!')</script>";
header("Refresh: 0;url=admin_publicviolation.php");
}
Upvotes: 0