PDO UPDATE not updating...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

PDO UPDATE not updating...

Post 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(); 
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PDO UPDATE not updating...

Post 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?
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: PDO UPDATE not updating...

Post 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();
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PDO UPDATE not updating...

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: PDO UPDATE not updating...

Post 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?
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: PDO UPDATE not updating...

Post 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.
Post Reply