简体   繁体   中英

Check if Number already exist in row mysql database

<?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

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.

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");
}
<?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 ";
    }

}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM