Why does this SQL UPDATE query not work with a variable for WHERE?

后端 未结 4 1975
悲&欢浪女
悲&欢浪女 2020-12-20 09:40

this is my first post here at Stack Overflow. I know the question has been asked many times before. I went through many answers, tried all of them (except the correct approa

4条回答
  •  猫巷女王i
    2020-12-20 10:18

    Very simple to avoid sql injections and use up to date codes and You have an error in your SQL syntax.

    Here is an example :

       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";
        }
    
        }
    

    See more on : https://phpdelusions.net/mysqli_examples/update

    UPDATE : First code will work for you, but if you still want to use procedural way then us this :

    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); 
        }
    }
    

    ">




提交回复
热议问题