检查值是否已存在不起作用(PHP)

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

I working on how to check if value already exist in a database. Whenever I try to input something (Example: 000) there's always an error: Warning: mysql_num_rows() expects parameter 1 to be resource..., but the input was saved in the database

我正在研究如何检查数据库中是否已存在值。每当我尝试输入内容时(例如:000),总会出现错误:警告:mysql_num_rows()期望参数1为资源...,但输入保存在数据库中

then if I input the same again, the condition to check if value exist doesn't work

然后,如果我再次输入相同的内容,检查值是否存在的条件不起作用

if (isset($_POST['add']))
    {
        $docid = $_POST['docid'];

        $check = mysql_query("SELECT doc.docid, doc_details.docid FROM doc, doc_details WHERE docid='$docid'");
        $number_of_rows = mysql_num_rows($check);
        if ($number_of_rows > 0)
            {
                echo "<script> alert('Your input already exist, no input made'); </script>";
            }
        else
            {
                $insert = mysql_query("INSERT INTO doc (docid) VALUES ('$docid')");
                $insert = mysql_query("INSERT INTO doc_details (docid) VALUES ('$docid')");
                echo "<script> alert('ADDING: Successful'); </script>";
            }
    }

1 个解决方案

#1


0  

As described in the manual mysql_query returns false when an error occurs. As the error says, $check is not a resource, so it's probably false. So there is an error in your first SELECT query, you should debug that.

如手册中所述,mysql_query在发生错误时返回false。正如错误所说,$ check不是资源,所以它可能是错误的。所以你的第一个SELECT查询中有一个错误,你应该调试它。

You could try this, it'll probably tell you what's wrong:

你可以尝试这个,它可能会告诉你什么是错的:

    $check = mysql_query("SELECT doc.docid, doc_details.docid FROM doc, doc_details WHERE docid='$docid'");
    if (false === $check)
      var_dump(mysql_error());

Since false is never bigger than 0, your script will always attempt to insert, ignoring the error.

由于false永远不会大于0,因此您的脚本将始终尝试插入,忽略错误。

Offtopic; you should migrate to mysqli_* instead of using mysql_ functions.

无关;你应该迁移到mysqli_ *而不是使用mysql_函数。

#1


0  

As described in the manual mysql_query returns false when an error occurs. As the error says, $check is not a resource, so it's probably false. So there is an error in your first SELECT query, you should debug that.

如手册中所述,mysql_query在发生错误时返回false。正如错误所说,$ check不是资源,所以它可能是错误的。所以你的第一个SELECT查询中有一个错误,你应该调试它。

You could try this, it'll probably tell you what's wrong:

你可以尝试这个,它可能会告诉你什么是错的:

    $check = mysql_query("SELECT doc.docid, doc_details.docid FROM doc, doc_details WHERE docid='$docid'");
    if (false === $check)
      var_dump(mysql_error());

Since false is never bigger than 0, your script will always attempt to insert, ignoring the error.

由于false永远不会大于0,因此您的脚本将始终尝试插入,忽略错误。

Offtopic; you should migrate to mysqli_* instead of using mysql_ functions.

无关;你应该迁移到mysqli_ *而不是使用mysql_函数。