Page 1 of 1

PDO UPDATE not updating...

Posted: Sun Jan 19, 2014 4:49 pm
by me!
Can anayone give me a reason that this will not update the db?

It runs with no errors but will not update. The ID is correct and POST data is valid.

Code: Select all

// update the db   
        $sql = "UPDATE customers 
                SET     business_contact = :business_contact,
			business_name = :business_name,
			first_name = :first_name,
			last_name = :last_name,
			address = :address,
			city = :city,
			state = :state,
			zip = :zip,
			phone = :phone,
			cell_phone = :cell_phone,
			e_mail= :e_mail,
			m_address = :m_address,
			m_city = :m_city,
			m_state = :m_state,
			m_zip = :m_zip,
			m_name = :m_name
                WHERE   id = :id";
        
        $stmt = $db->prepare($sql);                                 
        $stmt->bindParam(':business_contact', $_POST['business_contact']);      
        $stmt->bindParam(':business_name', $_POST['business_name']);   
        $stmt->bindParam(':first_name', $_POST['first_name']);
        $stmt->bindParam(':last_name', $_POST['last_name']);
        $stmt->bindParam(':address', $_POST['address']);
        $stmt->bindParam(':city', $_POST['city']);
        $stmt->bindParam(':state', $_POST['state']);
        $stmt->bindParam(':zip', $_POST['zip']);
        $stmt->bindParam(':phone', $_POST['phone']);
        $stmt->bindParam(':cell_phone', $_POST['cell_phone']);
        $stmt->bindParam(':e_mail', $_POST['e_mail']);
        $stmt->bindParam(':m_address', $_POST['m_address']);
        $stmt->bindParam(':m_city', $_POST['m_city']);
        $stmt->bindParam(':m_state', $_POST['m_state']);
        $stmt->bindParam(':m_zip', $_POST['m_zip']);
        $stmt->bindParam(':m_name', $_POST['m_name']);
        $stmt->bindParam(':id', $customer_id);  
        $stmt->execute(); 

Re: PDO UPDATE not updating...

Posted: Sun Jan 19, 2014 9:16 pm
by Celauran
Looks OK at a glance. Have you checked the value of $stmt after calling prepare? Have you checked what execute returns? Have you checked that error reporting is turned on?

Re: PDO UPDATE not updating...

Posted: Mon Jan 20, 2014 12:13 am
by me!
I narrowed it down a bit, if I remove the third item "first_name" this will work, but with it nothing happens.

Code: Select all

	// update the db   
        $sql = "UPDATE  customers 
                SET     business_name = :business_name, 
                        business_contact = :business_contact, 
                        first_name = :first_name
                WHERE   id = :id";        
        $stmt = $db->prepare($sql);                                  
        $stmt->bindParam(':business_name', $_POST['business_name']);
        $stmt->bindParam(':business_contact', $_POST['business_contact']);
        $stmt->bindParam(':first_name', $_POST['first_name']);
        $stmt->bindParam(':id', $customer_id);   
        $stmt->execute();
As far as error reporting, I must not be doing that correct either, since I get no errors even with a wrong colum name.
I tried this but nothing... ??

Code: Select all

try
{
    // update the db   
        $sql = "UPDATE  customers 
                SET     business_name = :business_name, 
                        business_contact = :business_contact, 
                        first_name = :first_name
                WHERE   id = :id";        
        $stmt = $db->prepare($sql);                                  
        $stmt->bindParam(':business_name', $_POST['business_name']);
        $stmt->bindParam(':business_contact', $_POST['business_contact']);
        $stmt->bindParam(':first_name', $_POST['first_name']);
        $stmt->bindParam(':id', $customer_id);   
        $stmt->execute();
}
catch(PDOException $exception)
{
    echo "Failed: " . $exception->getMessage();
}

Re: PDO UPDATE not updating...

Posted: Mon Jan 20, 2014 2:53 am
by social_experiment

Code: Select all

<?php
 // to turn on error reporting for PDO
 $pdoObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 // where $pdoObj is your instance of the PDO class
?>
further reading
/pdo.error-handling

Re: PDO UPDATE not updating...

Posted: Mon Jan 20, 2014 7:39 pm
by me!
social_experiment wrote:

Code: Select all

<?php
 // to turn on error reporting for PDO
 $pdoObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 // where $pdoObj is your instance of the PDO class
?>
further reading
/pdo.error-handling
I think I have that already. This is what is in the include file that all pages call for the db connection

Code: Select all

// connect to the db with PDO
try {
    $db = new PDO($db_dsn, $db_user, $db_pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}
My understanding was that this and this alone should spit out an error or warning if I have one?

Re: PDO UPDATE not updating...

Posted: Mon Jan 20, 2014 8:40 pm
by me!
I got it!
It was NOT the PHP code at all, the problem was that the db was configured for none of the rows to be NULL, so when an individual is updated there was no business name, and when a business was updated there is no first or last name, but the db expected a value.

only took two days.... :roll:

I still dont know why I did not get an error message.