检查数据库中是否已存在数据

时间:2022-09-25 23:51:11

I'm trying to check if an user id already exists in the database. But for some reason it doesn't work. I've tried different sollutions but all failed.

我正在尝试检查数据库中是否已存在用户标识。但由于某种原因,它不起作用。我尝试了不同的解决方案,但都失败了。

db_connect.php is working fine, i double checked!

db_connect.php工作正常,我仔细检查!

create_user.php

<?php

$response = array();

if (isset($_POST['user_id'])){

        $id = $_POST['user_id'];
        $fullName = $_POST['user_fullName'];
        $firstName = $_POST['user_firstName'];
        $lastName = $_POST['user_lastName'];
        $birthday = $_POST['user_birthday'];
        $friends = $_POST['user_friends'];
        $totalFriends = $_POST['user_totalFriends'];
        $email = $_POST['user_email'];
        $gender = $_POST['user_gender'];
        $likes = $_POST['user_likes'];
        $events = $_POST['user_events'];
        $hometown = $_POST['user_hometown'];

    require_once __DIR__ . '/db_connect.php';

    $db = new DB_CONNECT();

    $query = mysql_query("SELECT * FROM 'userData' WHERE 'id' = '$id'");
    $user_data = mysql_num_rows($query);

    if(empty($user_data)) {         

        $result = mysql_query("INSERT INTO userData(id, fullName, firstName, lastName, birthday, friends, totalFriends, email, gender, likes, events, hometown) VALUES ('$id', '$fullName', '$firstName', '$lastName', '$birthday', '$friends', '$totalFriends', '$email', '$gender', '$likes', '$events', '$hometown')");

        if($result){
            $response["success"] = 1;
            $response["message"] = "User successfully created";

            echo json_encode($response);
        }

        else{
            $response["success"] = 0;
            $response["message"] = "Error occurred";

            echo json_encode($response);
        }
    }

    else {

        $response["success"] = 1;
        $response["message"] = "User already in database";

        echo json_encode($response);            

    }
}

else{
    $response["success"] = 0;
    $response["message"] = "Required userdata is missing";

    echo json_encode($response);
}   

?>

Well. If i run this script in a browser it works perfectly. When i run it in my android app it always creates a new user.

好。如果我在浏览器中运行此脚本,它可以很好地工作。当我在我的Android应用程序中运行它时,它总是创建一个新用户。

Thanks! T

1 个解决方案

#1


1  

You have added single quotes around table name and field.

您已在表名和字段周围添加单引号。

Change it to back ticks.

将其更改为后退。

$query = mysql_query("SELECT * FROM 'userData' WHERE 'id' = '$id'");

Update to:

$query = mysql_query("SELECT * FROM `userData` WHERE `id` = '$id'");

Also, please don't use mysql_* functions as they are deprecated for security reasons and will be removed in future versions.

另外,请不要使用mysql_ *函数,因为出于安全原因它们已被弃用,并且将在以后的版本中删除。

Single quotes are for user input.

单引号用于用户输入。

Keywords/Database name/Table Name/Field Names can be enclosed with back ticks.

关键字/数据库名称/表名称/字段名称可以用后面的刻度括起来。

#1


1  

You have added single quotes around table name and field.

您已在表名和字段周围添加单引号。

Change it to back ticks.

将其更改为后退。

$query = mysql_query("SELECT * FROM 'userData' WHERE 'id' = '$id'");

Update to:

$query = mysql_query("SELECT * FROM `userData` WHERE `id` = '$id'");

Also, please don't use mysql_* functions as they are deprecated for security reasons and will be removed in future versions.

另外,请不要使用mysql_ *函数,因为出于安全原因它们已被弃用,并且将在以后的版本中删除。

Single quotes are for user input.

单引号用于用户输入。

Keywords/Database name/Table Name/Field Names can be enclosed with back ticks.

关键字/数据库名称/表名称/字段名称可以用后面的刻度括起来。