sql >> Database >  >> RDS >> Mysql

MySQL-update, lege velden overslaan met PDO

Zoiets zou moeten werken

.
.
.
$q = array();
if(trim($_POST["b"]) !== ""){
    $q[] = "Program = :program";
}
if(trim($_POST["c"]) !== ""){
    $q[] = "Descr = :descr";
}
if(trim($_POST["d"]) !== ""){
    $q[] = "Cost = :cost";
}
if(sizeof($q) > 0){//check if we have any updates otherwise don't execute
    $query = "UPDATE links SET " . implode(", ", $q) . " WHERE Id= :id";
    $stmt = $dbh->prepare($query);
    $stmt->bindParam(":id", $_POST["a"]);
    if(trim($_POST["b"]) !== ""){
        $stmt->bindParam(":program", $_POST["b"]);
    }
    if(trim($_POST["c"]) !== ""){
        $stmt->bindParam(":descr", $_POST["c"]);
    }
    if(trim($_POST["d"]) !== ""){
        $stmt->bindParam(":cost", $_POST["d"]);
    }
    $stmt->execute();
}
.
.
.


  1. MySQL-query gebruiken om rijen te doorkruisen om een ​​recursieve boom te maken

  2. Selecteer vorige rij mysql?

  3. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:communicatieverbinding mislukt

  4. Waarde verhogen in MySQL-updatequery