对于更新前的用户

时间:2018-06-20 09:41:42

标签: php search

我已经制作了一个API,可以使用INSERT在mysql数据库中插入7个字段(全名,城市,血型,密码,mobileno,lastdonated,created_date)的数据,请帮助我添加一些代码来搜索用户是否已经使用mobileno字段添加,然后再添加记录,否则显示消息“用户已经存在”。

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
if(count($_REQUEST) > 0)
{
    $conn = mysqli_connect("localhost", "creative_bloodapp", "PasSwORd", 'creative_bldapp');
        $fullname           =$_POST['fullname'];
        $city               =$_POST['city'];
        $bloodgroup         =$_POST['bloodgroup'];
        $password           =$_POST['password'];
        $mobileno           =$_POST['mobileno'];
        $lastdonated        =$_POST['lastdonated'];
        $created_date       = date('Y-m-d H:i:s');
    $sql = "INSERT INTO register (fullname, city, bloodgroup, password, mobileno, lastdonated, created_date) VALUES ('" . $fullname . "','" . $city . "','" . $bloodgroup . "','" . $password . "','" . $mobileno . "','" . $lastdonated . "','". $created_date . "');";
    $qur = $conn->query($sql);
    if($qur){
        $query="SELECT * FROM register ORDER BY mobileno DESC LIMIT 1;";
        $data=array();
        $result=mysqli_query($conn, $query);
        $row=mysqli_fetch_assoc($result);
        mysqli_close($conn);
        response(1,"User has been registered!",$row);
    }else{
        mysqli_close($conn);
        response(0,"Not Registered!",NULL);
    }
}
else
{
    response(0,"Not Registered!",NULL);
}
function response($status,$status_message,$data)
{
    header("HTTP/1.1 ".$status);
    $response['status']=$status;
    $response['status_message']=$status_message;
    $response['data']=$data;
    $json_response = json_encode($response);
    echo $json_response;
}
?>

3 个答案:

答案 0 :(得分:2)

正在寻找这样的东西;我更改了一些查询并为您重新编写了一部分。还添加了mysqli_real_escape_string

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
if(count($_REQUEST) > 0)
{
    $conn = mysqli_connect("localhost", "creative_bloodapp", "PasSwORd", 'creative_bldapp');
    $fullname           = mysqli_real_escape_string($conn, $_POST['fullname']);
    $city               = mysqli_real_escape_string($conn, $_POST['city']);
    $bloodgroup         = mysqli_real_escape_string($conn, $_POST['bloodgroup']);
    $password           = mysqli_real_escape_string($conn, $_POST['password']);
    $mobileno           = mysqli_real_escape_string($conn, $_POST['mobileno']);
    $lastdonated        = mysqli_real_escape_string($conn, $_POST['lastdonated']);
    $created_date       = date('Y-m-d H:i:s');


    $query="SELECT * FROM register  WHERE mobileno = '".$mobileno."'";
    $qur=mysqli_query($conn, $query);
    if(count(mysqli_num_rows($qur) !== 0)){
        $data=array();
        $result=mysqli_query($conn, $query);
        $row=mysqli_fetch_assoc($result);
        mysqli_close($conn);
        response(1,"User has been registered!",$row);
    }else{
        $sql = "INSERT INTO register (fullname, city, bloodgroup, password, mobileno, lastdonated, created_date) VALUES ('" . $fullname . "','" . $city . "','" . $bloodgroup . "','" . $password . "','" . $mobileno . "','" . $lastdonated . "','". $created_date . "');";
        $qur=mysqli_query($conn, $query);
        mysqli_close($conn);
        response(0,"Not Registered!",NULL);
    }
}
else
{
    response(0,"Not Registered!",NULL);
}
function response($status,$status_message,$data)
{
    header("HTTP/1.1 ".$status);
    $response['status']=$status;
    $response['status_message']=$status_message;
    $response['data']=$data;
    $json_response = json_encode($response);
    echo $json_response;
}

答案 1 :(得分:0)

是的,请使其移动无唯一键,这样

usage: sudoadm.py [-h] [-o] [-a [LDAP_ATTRS [LDAP_ATTRS ...]]] [-d]

                  [dn, object_class, attributes [dn, object_class, attributes ...]]

positional arguments:
  dn, object_class, attributes

optional arguments:
  -h, --help            show this help message and exit
  -o, --overview        print overview
  -a [LDAP_ATTRS [LDAP_ATTRS ...]], --add [LDAP_ATTRS [LDAP_ATTRS ...]]
                        add sudo item
  -d, --delete          delete sudo item

答案 2 :(得分:0)

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
if(count($_REQUEST) > 0)
{
    $conn = mysqli_connect("localhost", "creative_bloodapp", "PasSwORd", 'creative_bldapp');
    $fullname           = mysqli_real_escape_string($conn, $_POST['fullname']);
    $city               = mysqli_real_escape_string($conn, $_POST['city']);
    $bloodgroup         = mysqli_real_escape_string($conn, $_POST['bloodgroup']);
    $password           = mysqli_real_escape_string($conn, $_POST['password']);
    $mobileno           = mysqli_real_escape_string($conn, $_POST['mobileno']);
    $lastdonated        = mysqli_real_escape_string($conn, $_POST['lastdonated']);
    $created_date       = date('Y-m-d H:i:s');

    $verifysql = "select id from register where mobileno='".$mobileno."'";
    $qur = $conn->query($verifysql);
    if($qur->num_rows == 0){
       $sql = "INSERT INTO register (fullname, city, bloodgroup, password, mobileno, lastdonated, created_date) VALUES ('" . $fullname . "','" . $city . "','" . $bloodgroup . "','" . $password . "','" . $mobileno . "','" . $lastdonated . "','". $created_date . "');";
       $result = $conn->query($sql);
       if($result){
          mysqli_close($conn);
          response(200,"User has been registered!",$row);
       }else{
          mysqli_close($conn);
          response(500,"Registeration Failed",NULL);
       }
    }
    else{ 
        mysqli_close($conn);
        response(409,"User Already Exists!",NULL);
    }
}
else
{
  response(400,"Not Registered!",NULL);
}