检查行mysql数据库中是否已存在Number

时间:2014-02-17 04:38:58

标签: 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");
    }


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

}

?>

如果$ number已存在于数据库中,我想拒绝注册,有人帮助我吗?无法弄清楚。我不能使用一些编码方法。不知道如何使用其他

3 个答案:

答案 0 :(得分:1)

ALTER TABLE tblcitizen ADD CONSTRAINT tblcitizen UNIQUE (number);

现在,如果该数字已经存在,插入将失败,您需要检查mysql_error以了解如何继续。如果您使用的是不推荐使用的mysqli或PDO接口,则要检查的方法是mysqli_error或PDO::errinfo,它们的工作方式相同。

答案 1 :(得分:0)

首先使用选择查询,即AND请使用mysqli而不是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");
}

答案 2 :(得分:0)

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

}

?>