在PHP中使用表单值更新MySQL DB行

在PHP中使用表单值更新MySQL DB行,php,mysql,Php,Mysql,我有一个简单的html表单页面,需要更新2列值,其中“Some_DB_Table.”id=用户输入的id号。然而,我对如何使用准备好的语句来避免SQL注入有点困惑 我的代码: HTML: 我的PHP: <?php include('inc.php'); //Get Table Options. if (isset($_POST['sgNumber'])) { $sgNumber = $_POST['sgNumber']; if (isset($_POST

我有一个简单的html表单页面,需要更新2列值,其中“Some_DB_Table.”id=用户输入的id号。然而,我对如何使用准备好的语句来避免SQL注入有点困惑

我的代码:

HTML:

我的PHP:

    <?php 


include('inc.php');


//Get Table Options.
if (isset($_POST['sgNumber'])) {
    $sgNumber = $_POST['sgNumber'];

    if (isset($_POST['stageSelect'])) {
        $stageSelect=$_POST['stageSelect'];
    }
    if (isset($_POST['floorNotes'])) {
        $floorNotes=$_POST['floorNotes'];
    }

    //connect  to the database 
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if(mysqli_connect_errno() ) {
        printf('Could not connect: ' . mysqli_connect_error());
        exit();
    }

    $conn->select_db($dbname);

    if(! $conn->select_db($dbname) ) {
        echo 'Could not select database. '.'<BR>';
    }

    $sql= "UPDATE invoices SET productionstage = ".$stageSelect.", floornotes = ".$floorNotes." WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $sgNumber);
    $stmt->execute();
    $stmt->store_result();     

    if(mysqli_query($conn, $stmt)){
        echo "".$sgnumber." Updated Successfully!";
    } else {
        echo "ERROR: Could not update ".$sgnumber."".mysqli_error($conn)."";
    }


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//Free the result variable. 
 $result->free();


//Close the Database connection.
 $conn->close();

}//End If statement

?>

这是正确的/有什么建议吗

谢谢大家!

如果仔细阅读,您会发现需要指定每个参数的类型。通常这没什么大不了的:

 $stmt = $conn->prepare(
   "UPDATE invoices SET productionstage=?,floornotes=? WHERE id = ?"
 );
 $stmt->bind_param('ssi', $stageSelect, $floorNotes, $sgNumber);
尽量避免为语句创建中间变量。这通常会导致意外运行错误查询的情况。

如果仔细阅读,您会发现需要指定每个参数的类型。通常这没什么大不了的:

 $stmt = $conn->prepare(
   "UPDATE invoices SET productionstage=?,floornotes=? WHERE id = ?"
 );
 $stmt->bind_param('ssi', $stageSelect, $floorNotes, $sgNumber);

尽量避免为语句创建中间变量。这通常会导致您无意中运行错误的查询。

由于您正在准备内容并进行
bind_param
调用,我不确定您在为所有值(而不仅仅是一些任意值)添加占位符时是如何完全失手的。
$stageSelect
在那里做什么?用
替换所有内容并绑定这些值。@tadman这样做:$sql=“更新发票集productionstage=?,floornotes=?其中id=?”,然后执行$stmt->bind_param('i',$sgNumber,$stageSelect,$floornotes)?我不知道如何在更新表时使用bind_param部分来避免SQL注入,而不是仅仅从表中选择数据。由于您正在准备事情并进行
bind_param
调用,我不确定您在为所有值而不仅仅是一些任意值添加占位符时怎么会完全失手。什么是
$stageSelect
在那里做什么?用
替换所有内容并绑定这些值。@tadman这样做:$sql=“更新发票集productionstage=?,floornotes=?”?其中id=?,然后执行$stmt->bind_param('i',$sgNumber,$stageSelect,$floorNotes);?我不确定如何在更新表时使用bind_param部分来避免SQL注入,而不是仅仅从表中选择数据。因此,我像这样测试了更新,我的productionstage列更新正确,但floornotes没有。floornotes列类型是text,因此我将参数绑定类型设置为's'类似于'ssi',实际文本只显示'test',但当我重新执行搜索时,字段返回空,我可以在数据库中看到该行的列也是空的。我也没有收到任何错误…列productionstage的数据类型是“varchar”。我可能对param_bind和文本的列类型有问题吗?非常感谢!我能够解决这个问题。事实证明,我的代码编辑器和服务器之间存在通信困难,导致我的一些编辑无法在服务器上进行。修复后,一切都很好。所以我像这样测试了更新,我的productionstage列更新正确,但floornotes没有。floornotes列类型是text,因此我将参数绑定类型设置为's'类似于'ssi',实际文本只显示'test',但当我重新执行搜索时,字段返回空,我可以在数据库中看到该行的列也是空的。我也没有收到任何错误…列productionstage的数据类型是“varchar”。我可能对param_bind和文本的列类型有问题吗?非常感谢!我能够解决这个问题。事实证明,我的代码编辑器和服务器之间存在通信困难,导致我的一些编辑无法在服务器上进行。修好后,一切正常。
 $stmt = $conn->prepare(
   "UPDATE invoices SET productionstage=?,floornotes=? WHERE id = ?"
 );
 $stmt->bind_param('ssi', $stageSelect, $floorNotes, $sgNumber);