通过PHP编辑SQL记录



我正在尝试使用PHP脚本在SQL表中编辑记录。我能够加载一个表格,该表格可以让我编辑文本,但是当我按提交时,它不会保存新的记录名称。它只是将我重定向到没有编辑的表页面。

(包括db(

<?php
function renderForm($ArtistName = '', $error = '', $ArtistID = '')
{
    // ...
}
if (isset($_GET['ArtistID'])) {
    if (isset($_POST['submit'])) {
        if (is_numeric($_GET['ArtistID'])) {
            $ArtistID = $_GET['ArtistID'];
            $ArtistName = htmlentities($_POST['ArtistName'], ENT_QUOTES);
            if ($ArtistName == '') {
                $error = 'ERROR: Please fill in all required fields!';
                renderForm($ArtistName, $error, $ArtistID);
            } else {
                if ($stmt = $conn->prepare("UPDATE Artists SET ArtistName = ? WHERE ArtistID=?")) {
                    $stmt->bind_param("s", $ArtistName);
                    $stmt->execute();
                    $stmt->close();
                } else {
                    echo "ERROR: could not prepare SQL statement.";
                }
            }
        }
    }
}

谢谢!

php doc说:

参数标识符。对于使用命名占位持有人的准备好的语句,这将是表单:name的参数名称。对于使用问号占位符的准备好的语句,这将是参数的1个位置。

在代码中,您使用的是"s"作为bindParam

的参数
<?php
function renderForm($ArtistName = '', $error = '', $ArtistID = '')
{
    //
}

if (isset($_GET['submit'])){
    if (isset($_POST['ArtistID'])){
        if (is_numeric($_GET['ArtistID'])){
            $ArtistID = $_GET['ArtistID'];
            $ArtistName = htmlentities($_POST['ArtistName'], ENT_QUOTES);
            if ($ArtistName == ''){
                $error = 'ERROR: Please fill in all required fields!';
                renderForm($ArtistName, $error, $ArtistID);
            }else{
                if ($stmt = $conn->prepare("UPDATE Artists SET ArtistName = ? WHERE ArtistID=?")){
                    $stmt->bind_param(1, $ArtistName);
                    $stmt->bind_param(2, $ArtistID, PDO::PARAM_INT);
                    $stmt->execute();
                    $stmt->close();
                }else{
                    echo "ERROR: could not prepare SQL statement.";
                }
            }
        }
    }
}
?>

最新更新