【问题标题】:Checking to see if ID is already in database, if it is don't INSERT it again检查ID是否已经在数据库中,如果没有,请再次插入
【发布时间】:2017-07-22 18:56:54
【问题描述】:

当我使用空数据库运行页面时,它会正确插入数据。当我再次运行该页面时,它显示数据库中已经有一个 ID,但它还是插入了它。不知道如何或为什么,但我已经尝试了 if 语句中的所有布尔组合,但无法正确选择。

//pass in an ID to compare:
function checkOrderID($orderID) {
//Connect to the database: 
$mysqli = new mysqli("localhost", "root", "", "price");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Ask the database for some sweet, sweet data: 
$stmt1 = "SELECT orderID FROM orders";
$result = $mysqli->query($stmt1);

//flag (we want to believe that there are no similar IDS so lets make it true):
$flag = true;

//while we got some data, display that shit
while ($row = $result->fetch_assoc()) {
    //asign data to variable:
    $rowOrderID = $row['orderID'];

    //Does it match? if it does set the flag to false so it doesnt get inserted. 
    if ($rowOrderID == $orderID) {
        echo "Row ID" . $row["orderID"] . " Passed ID: " . $orderID . "<br>";
        echo "This order is already in the database" . "<br>";
        $flag = false;
    }
}
//hand the flag over to who ever needs it
return flag;
}

.

if (checkOrderID($orderID) == true) {
    //some mysql insert logic here

}

【问题讨论】:

  • id 是主键吗?或者是其他东西?如果是主要的,那么它不能再次插入
  • orderID 不是主键 @ShafiqulIslam
  • 你能回显 $rowOrderID 吗?"=="。 $订单ID;死();然后检查两个值都有 echo gettype($orderID ).'
    '; echo gettype($rowOrderID ).'
    ';
  • 行 ID:4661188581,通过 ID:4661188581 @ShafiqulIslam
  • 我现在得去上班了。我要么今晚晚点回来,要么明天中午之后回来。对不起!

标签: php mysql function mysqli


【解决方案1】:

你为什么要搞得这么复杂。做这样的事情:

$con=mysqli_connect("localhost","root","","price");
$check_query = mysqli_query($con,"SELECT * FROM orders WHERE orderID = $orderID");
if (mysqli_num_rows($check_query) == 0) {
    //mysql insert logic here
}

(请注意,当然你也会有你的连接逻辑)


注意:您正在以面向对象的方式使用Mysqli,但在此示例中,我没有使用面向对象的 DB 连接方式。连接变量$con 必须传递给mysqli_query() 方法。

另外...随机附注,但通常最好为您的 root mysql 用户设置密码。

【讨论】:

  • 感谢回复,我今天晚些时候下班后测试一下。我没有root密码的原因是我的测试是在本地WAMP上完成的。
  • 使用您的 check_query 我可以将 SELECT ALL 修改为 SELECT orderID 吗? @达拉斯卡利
【解决方案2】:

这里更好更简短,但请尝试在全局范围内使用数据库连接,而不是在您的方法中,并尝试使用准备好的语句。但除了那些你可以使用下面的代码。

//pass in an ID to compare:
function checkOrderID($orderID) {
    //Connect to the database: I suggest use global DB connection 
    $mysqli = new mysqli("localhost", "root", "", "price");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    //gets recodrs based on $orderID passed to this method
    $stmt1 = "SELECT * FROM orders where orderID=$orderID"; //Try to use prepared statement
    $result = $mysqli->query($stmt1);
    //Store number of rows found
    $row_count = $result->num_rows;
    if($row_count>0){
        return true;
    }
    else{
        return false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多