mysqli更新不会更新

时间:2018-02-13 10:37:51

标签: php forms mysqli

我刚刚将一些旧代码从mysql转换为mysqli。我连接到数据库并读取AgentNumber的值,然后这个号码将分配给某个电子邮件地址。一切正常,电子邮件发送到不同的电子邮件地址。但是,我希望在表单提交后更新数据库,但我无法让更新功能正常工作。 首先是我的数据库的截图: screenshot of database

此处成功连接后代码片段:             //将数据库中的表名放入变量中。         $ agents =“agents”;

circle.java

我无法更改号码,我尝试了很多我在这里找到的陈述。 非常感谢任何帮助。

奥蒂

2 个答案:

答案 0 :(得分:0)

我忽略了我的号码没有前导零,我还需要再次添加连接到我的更新查询。 这是我的工作代码片段:     //针对数据库的查询。         $ extractID =" SELECT * FROM $ agents";

    // Create a query against the database as a variable as with variables for connection ($mysqli) and for query ($extractID)
    $result = mysqli_query($mysqli, $extractID);

    // Check which number is currently in the database as AgentNumber and use that number to assign the email address
    while($row = mysqli_fetch_assoc($result))
    {
        $agent_number = $row['AgentNumber'];
        $agent_id = 00001;
        // echo $agent_number  ."<br>";
        if ($agent_number == '00056') { 
            $agent_number = '00057'; // to 00057 - not really necessary to set here as usesd as number in update query
            // echo $agent_number  ."<br>";
            $insertNew = mysqli_query($mysqli, 'UPDATE `agents` SET `AgentNumber` = "00057" WHERE `AgentID`="00001"');
        } elseif ($agent_number == '00057') {
            $agent_number = '00058'; // to 00058 - not really necessary to set here as usesd as number in update query
            $insertNew = mysqli_query($mysqli, 'UPDATE `agents` SET `AgentNumber` = "00058" WHERE `AgentID`="00001"');
        } else {
            $agent_number = '00056'; // to 00056 - not really necessary to set here as usesd as number in update query
            $insertNew = mysqli_query($mysqli, 'UPDATE `agents` SET `AgentNumber` = "00056" WHERE `AgentID`="00001"');
        }   
    }

答案 1 :(得分:-1)

您的WHERE子句可能永远不会访问正确的记录,因为代码尝试设置WHERE AgentId = 1,同时他们在数据库中的ID类似于&#34; 0001&#34;。

这应该有效:

while($row = mysqli_fetch_assoc($result))
{
    $agent_number = $row['AgentNumber'];
    $agent_id = $row['AgentID'];
    // echo $agent_number  ."<br>";
    if ($agent_number == '00056') { 
        $agent_number++; // to 00057
        $insertNew = mysqli_query($conn, "UPDATE $agents SET AgentNumber=$agent_number WHERE AgentID=$agent_id");

(为清晰起见,省略了其余代码)