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

后端 未结 4 1948
悲&欢浪女
悲&欢浪女 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条回答
  • 2020-12-20 10:07

    The issue that you have is the fact your code does not use the SET correctly, you currently have the following;

    $sql =  "UPDATE VideoArchiv             
        SET ('".$_POST["titel"]."','".$_POST["schauspieler"]."')
            WHERE id=$id";
    

    Which is used like you'd do an INSERT

    To rectify the immediate issue, simply change to;

    $sql =  "UPDATE VideoArchiv             
            SET field1 = '".$_POST["titel"]."',
                field2 = '".$_POST["schauspieler"]."'
            WHERE id=$id";
    

    But this odes leave you open to SQL injection attacks, to do a quick and easy fix on this, something as simple as the following would be helpful;

    $id = mysqli_real_escape_string($connect, $_POST["id"]);
    $titel = mysqli_real_escape_string($connect, $_POST["titel"]);
    $schauspieler = mysqli_real_escape_string($connect, $_POST["schauspieler"]);
    
    $sql =  "UPDATE VideoArchiv             
            SET field1 = '{$titel}',
                field2 = '{$schauspieler}'
            WHERE id=$id";
    

    I'd suggest reading into prepared statements as this would be a lot safer however

    I know this has had the right answer to the question at hand prior to this post, but none have mentioned injection and how to resolve (even a soft way like here)

    0 讨论(0)
  • 2020-12-20 10:15

    The following query can be used:

    UPDATE VideoArchiv SET columnname1 = '".$_POST["titel"]."', columnname2 = '".$_POST["schauspieler"]."' WHERE id=$id
    
    0 讨论(0)
  • 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); 
        }
    }
    

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    
    	<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>

    0 讨论(0)
  • 2020-12-20 10:27

    Column Names are not givn in query

    UPDATE table_name SET column_name1 = expr1, column_name2 = expr2, ... [WHERE condition];

    So, your query will be something like this and check column names in database:

    $sql =  "UPDATE VideoArchiv             
                    SET titel='".$_POST["titel"]."',schauspieler='".$_POST["schauspieler"]."'
                    WHERE id=$id";
    

    Note: This is sql vulnerable, so please add mysql real escape function (https://www.php.net/manual/en/function.mysql-real-escape-string.php) or convert it to pdo.

    0 讨论(0)
提交回复
热议问题