简体   繁体   中英

Why is the database not updating in this?

I have this php:

<?php 
    $db= new PDO("mysql:host=example;dbname=example", "root", "example");
    $query= $db->prepare("SELECT yarnName, price, sale_price, cost, contents, onSale, yarnLink, yarnImage, activeFlag FROM yarn WHERE yarnId = :parameter");
    $query->bindParam(':parameter', $id, PDO::PARAM_STR);
    $query->execute();
    $id=$_POST['id'];
    $name=$_POST['name'];
    $price=$_POST['price'];
    $salePrice=$_POST['salePrice'];
    $cost=$_POST['cost'];
    $contents=$_POST['contents'];
    $onSale=$_POST['onSale'];
    $yarnLink=$_POST['yarnLink'];
    $image=$_POST['image'];
    $active=$_POST['active'];
    $attrUpdates= array($name,$price,$salePrice,$cost,$contents,$onSale,$yarnLink,$image,$active);
    $attrOriginal=$query->fetch();
    if(count($attrUpdates)==count($attrOriginal)){
        for($i=0; $i<count($attrUpdates); $i++){
            if($attrUpdate[$i]!=$attrOriginal[$i]&&$attrUpdate[$i]!=null){
                switch($i){
                    case 0:
                        $update=$db->prepare("UPDATE yarn SET yarnName = :parameter1 WHERE yarnId = :parameter2");
                    case 1:
                        $update=$db->prepare("UPDATE yarn SET price = :parameter1 WHERE yarnId = :parameter2");
                    case 2:     
                        $update=$db->prepare("UPDATE yarn SET sale_price = :parameter1 WHERE yarnId = :parameter2");
                    case 3:
                        $update=$db->prepare("UPDATE yarn SET cost = :parameter1 WHERE yarnId = :parameter2");
                    case 4:
                        $update=$db->prepare("UPDATE yarn SET contents = :parameter1 WHERE yarnId = :parameter2");
                    case 5:
                        $update=$db->prepare("UPDATE yarn SET onSale = :parameter1 WHERE yarnId = :parameter2");
                    case 6:
                        $update=$db->prepare("UPDATE yarn SET yarnLink = :parameter1 WHERE yarnId = :parameter2");
                    case 7:
                        $update=$db->prepare("UPDATE yarn SET yarnImage = :parameter1 WHERE yarnId = :parameter2");
                    case 8:
                        $update=$db->prepare("UPDATE yarn SET activeFlag = :parameter1 WHERE yarnId = :parameter2");
                }
                $query->bindParam(':parameter1', $attrUpdate[$i], PDO::PARAM_STR);
                $query->bindParam(':parameter2', $id, PDO::PARAM_STR);
                $query->execute();
            }
        }
    }
?>

and it is not updating the database. I'm not sure as to why this is, but if anyone tell me what's wrong, I'd greatly appreciate it. I don't think it's the post where it's going wrong, but it could be.

$query change to $update

$update->bindParam(':parameter1', $attrUpdate[$i], PDO::PARAM_STR);
$update->bindParam(':parameter2', $id, PDO::PARAM_STR);
$update->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM