1

我正在尝试根据数据库中是否已存在帖子来插入/更新 MySql 数据库(我正在使用唯一的 user_id 进行检查)。以下作品:

$select_query = "SELECT * ";
$select_query .= "FROM test ";
$select_query .= "WHERE user_id = '$user_id'";

$check_user_id = mysqli_query($connection, $select_query);

$query  = "INSERT INTO test (";
$query .= "  user_id, name, message";
$query .= ") VALUES (";
$query .= "  '{$user_id}', '{$name}', '{$message}'";
$query .= ")";

$result = mysqli_query($connection, $query);

if ($result) {
    echo "Success!";
} else {
    die("Database query failed. " . mysqli_error($connection));
}

但是,当我将以下代码与 if/else 语句一起使用时,它不再起作用,尽管控制台报告“成功!” (意味着 $result 有一个值)。任何帮助将不胜感激。谢谢。

$select_query = "SELECT * ";
$select_query .= "FROM test ";
$select_query .= "WHERE user_id = '$user_id'";

$check_user_id = mysqli_query($connection, $select_query);

if (!$check_user_id) {
    $query  = "INSERT INTO test (";
    $query .= "  user_id, name, message";
    $query .= ") VALUES (";
    $query .= "  '{$user_id}', '{$name}', '{$message}'";
    $query .= ")";
} else {
    $query  = "UPDATE test SET ";
    $query .= "name = '{$name}', ";
    $query .= "message = '{$message}' ";
    $query .= "WHERE user_id = '{$user_id}'";
}

$result = mysqli_query($connection, $query);

if ($result) {
    echo "Success!";
} else {
    die("Database query failed. " . mysqli_error($connection));
}
4

5 回答 5

3

据我了解你的代码。您正在尝试检查 user_id 是否存在于您的数据库中.. 我做了一个简单的代码,我认为它对我有用..

    $select_query = mysql_query("SELECT * FROM test WHERE user_id = '$user_id'") or die (mysql_error());
$result = mysql_num_rows($select_query);

if(!$result){
    $query = mysql_query("INSERT INTO test (user_id, name, message) VALUES ('$user_id', '$name', '$message')");
        if($query){
            echo "Success!";
        }
        else
        {
            die (mysql_error());
        }
}
else{
    $query2 = mysql_query("UPDATE test SET name='$name', message='$message' WHERE user_id = '$user_id'")
}
于 2013-08-10T07:29:41.730 回答
0

使用 Code 在 mysql 中插入数据。

$query = mysql_query("INSERT INTO test set user_id = '$user_id', name = '$name', message = '$message'");
if($query){
    echo "Success!";
}
于 2014-02-21T11:47:07.333 回答
0

mysql_query返回操作标识符,而不是实际结果。这就是为什么$check_user_id总是如此,所以你总是试图更新(甚至不存在!)行。

您必须“阅读”mysql_query例如使用的结果

$check_user_id = mysql_num_rows( mysql_query($connection, $select_query) );

现在它返回 0 (false) 如果 q 没有结果$select_query

于 2013-08-10T07:23:54.860 回答
0

该语句为您提供了结果资源

$check_user_id = mysqli_query($connection, $select_query);

接下来您要检查if(!$check_user_id):由于否定,此条件的计算结果为 false !。因此,您的条件进入该else部分并且永远不会进入if.

总是有价值的$result,因为您在脚本末尾调用它。

于 2013-08-10T07:24:13.223 回答
0

Since you previously know the user_id, and assuming that is a primary key in the table, you could use "ON DUPLICATE KEY UPDATE" clause:

$query = mysql_query("INSERT INTO test (user_id, name, message) 
                     VALUES ('$user_id', '$name', '$message')
                     ON DUPLICATE KEY 
                        UPDATE name='$name', message='$message';
");

Same result with only one query.

Ref: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

于 2013-08-10T10:13:35.090 回答