0

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.

Christian Grabowski
  • 2,782
  • 3
  • 32
  • 57
  • What have you done to troubleshoot this? – John Conde Jan 27 '14 at 02:17
  • echo the variables that have the posted values, log any database errors and used the web console to observe the http requests. That last one isn't really much, but it says the post was a success. – Christian Grabowski Jan 27 '14 at 02:21
  • You'll need to start with some systematic debugging to narrow it down. Look at [How to squeeze an error message out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) . – Michael Berkowski Jan 27 '14 at 02:22
  • By default, PDO errors silently (except for the initial connection, which throws an exception). Set it up to throw exceptions all the time. – Michael Berkowski Jan 27 '14 at 02:23
  • a try-catch statement on the query->execute() can show your problem more than sure – HellBaby Jan 27 '14 at 02:23
  • @HellBaby Not in the code's current state. `PDO::ERRMODE_EXCEPTION` must first be set. – Michael Berkowski Jan 27 '14 at 02:23
  • What is the value of `$id` here: `$query->bindParam(':parameter', $id, PDO::PARAM_STR);` – Tony Jan 27 '14 at 02:24
  • the value of $id is 2, which is correct. I'm a little confused with all the setting PDO error stuff, and I'll try the try catch. – Christian Grabowski Jan 27 '14 at 02:49
  • I am getting this `Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent.` when the file input is left blank, which then the post does not work. – Christian Grabowski Jan 27 '14 at 02:59
  • The error tells you exactly what you need to know. In order for files to be uploaded by a file input, and metadata appear in `$_FILES` in PHP, you must set the enctype correctly in your form. `
    `
    – Michael Berkowski Jan 27 '14 at 03:25
  • Yeah I got that, I just didn't know if that would stop things or not. – Christian Grabowski Jan 27 '14 at 03:55

1 Answers1

0

$query change to $update

$update->bindParam(':parameter1', $attrUpdate[$i], PDO::PARAM_STR);
$update->bindParam(':parameter2', $id, PDO::PARAM_STR);
$update->execute();
JOE LEE
  • 1,058
  • 1
  • 6
  • 6
  • Please always point out what you're changing and why. "try this" answers aren't always as helpful as they seem when the problem is subtle as an incorrect variable used. – Michael Berkowski Jan 27 '14 at 02:28
  • oh yeah, I meant to write that actually, I just copied that query part from an old script, thanks for pointing it out. – Christian Grabowski Jan 27 '14 at 02:51