为什么此 SQL UPDATE 查询不适用于 WHERE 的变量?

-1 php sql mysqli sql-update

这是我在 Stack Overflow 上的第一篇文章。我知道这个问题之前已经被问过很多次了。我经历了很多答案,尝试了所有答案(显然正确的方法除外),但不知道该尝试什么了。

我有一个 SQL 表,其中每一行都有一个“编辑”按钮。单击它时,我将所选行的 id 传递给edit.php. 在那里,我得到它并根据用户从表单输入的 id 更新给定的行。第一列是 id ,它被设置为AUTO_INCREMENT

附带说明一下,无论我使用WHERE id=$id";还是使用,我都会遇到相同的错误WHERE id='$id'";

我认为最接近正确方法的代码如下,并在代码下方生成错误消息:

<html>
    <title>
        Video Archiv - New
    </title>
    
    <body>
        <?php
            include("connect.php"); 
            $id=$_GET['id'];
            echo "Details von Video #$id editieren:<br /><br />";
            
            if(isset($_POST['update']))
            {
                $sql =  "UPDATE VideoArchiv             
                        SET ('".$_POST["titel"]."','".$_POST["schauspieler"]."')
                        WHERE id=$id";

                        $result = mysqli_query($connect,$sql);

                if (mysqli_query($connect,$sql) === TRUE) 
                {
                    echo "Record updated successfully";
                } 
                else 
                {
                    echo "Error updating record: " . $connect->error;
                }
            }
            ?>

        <form action="edit.php" method="post"> 
            
            <label> Titel:</label><br/>
            <input type="text" name="titel" required><br/>

            <label>Schauspieler</label><br/>
            <input type="text" name="schauspieler" required><br/>
            <br />
            <button type="submit" name="update">Speichern</button>
                
        </form>

        <?php
            include("back.php");
        ?>
    </body>
</html> 
Run Code Online (Sandbox Code Playgroud)

错误信息:

错误更新记录:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 2 行的 '('a','d') WHERE id=9' 附近使用的正确语法

非常感谢您的帮助,并为重复的问题感到抱歉,但我真的找不到解决方案并且非常绝望。

更新:

以下代码给出了这个错误:

致命错误:未捕获的错误:在 /homepages/25/d72758610/htdocs/multimedia/edit.php:30 中的 bool 上调用成员函数 bind_param() 堆栈跟踪:#0 {main} 抛出在 /homepages/25/d72758610/ htdocs/multimedia/edit.php 第 30 行

<html>
    <title>
        Video Archiv - New
    </title>
    
    <body>
        <?php
            include("connect.php"); 
            $id=$_GET['id'];
            $title = $_POST["titel"];
            $schauspieler = $_POST["schauspieler"];

            if(empty($title))
            {
                echo "error";
            }
            elseif(empty($schauspieler))
            {
                echo "error";
            }
            else
            {
                $sql = "UPDATE users SET title=?, schauspieler=? WHERE id=?";
                $stmt= $connect->prepare($sql);
                $stmt->bind_param("ssi", $title, $schauspieler, $id);
                if($stmt->execute())
                {
                      echo "Succes";
                }
                else
                {
                  echo "something went wromg";
                }
            }
            ?>

        <form action="edit.php" method="post"> 
            
            <label> Titel:</label><br/>
            <input type="text" name="titel" required><br/>

            <label>Schauspieler</label><br/>
            <input type="text" name="schauspieler" required><br/>
            <br />
            <button type="submit" name="update">Speichern</button>
                
        </form>

        <?php
            include("back.php");
        ?>
    </body>
</html> 
Run Code Online (Sandbox Code Playgroud)

Dlk*_*Dlk 5

避免 sql 注入并使用最新代码非常简单,并且您的 SQL 语法有错误

这是一个例子:

   include("connect.php"); 
    $id=$_GET['id'];
    $title = $_POST["titel"];
    $schauspieler = $_POST["schauspieler"];

    if(empty($title)){
    echo "error";
    }elseif(empty($schauspieler)){
    echo "error";
    }else{

    $sql = "UPDATE VideoArchiv SET title=?, schauspieler=? WHERE id=?";
    $stmt= $connect->prepare($sql);
    $stmt->bind_param("ssi", $title, $schauspieler, $id);
    if($stmt->execute()){
      echo "Succes";
    }else{
      echo "something went wromg";
    }

    }
Run Code Online (Sandbox Code Playgroud)

查看更多:https : //phpdelusions.net/mysqli_examples/update

更新:第一个代码对你有用,但如果你仍然想使用程序方式,那么我们这个:

include("connect.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {

//Check if we get id 
$Testid = $_GET['id'];
if(empty($Testid)){
    echo "id is empty";
}else{
    $id = $_GET['id'];
}


$title = $_POST["titel"];
$schauspieler = $_POST["schauspieler"];

    if(empty($title )){
        echo "error". $title; 
    }elseif(empty($schauspieler)){
        echo "error". $schauspieler;
    }else{
       $sql = "UPDATE VideoArchiv SET title=?, schauspieler=? WHERE id=?";
       $stmt = mysqli_prepare($connect, $sql);
       mysqli_stmt_bind_param($stmt, 'ssi', $title, $schauspieler, $id);
       mysqli_stmt_execute($stmt); 
    }
}
Run Code Online (Sandbox Code Playgroud)

   include("connect.php"); 
    $id=$_GET['id'];
    $title = $_POST["titel"];
    $schauspieler = $_POST["schauspieler"];

    if(empty($title)){
    echo "error";
    }elseif(empty($schauspieler)){
    echo "error";
    }else{

    $sql = "UPDATE VideoArchiv SET title=?, schauspieler=? WHERE id=?";
    $stmt= $connect->prepare($sql);
    $stmt->bind_param("ssi", $title, $schauspieler, $id);
    if($stmt->execute()){
      echo "Succes";
    }else{
      echo "something went wromg";
    }

    }
Run Code Online (Sandbox Code Playgroud)