UPDATE is the bane of my existence

I don’t know why, but I always struggle to UPDATE databases with PHP/MySQL.

I am working on a project right now where there are no issues with the database connection, SELECT from the database, or INSERT into the database (I’m using PDO).

But I can’t get my UPDATE to function at all. Could you please take a look at this dummy example below and tell me if I am making errors with the code, or if I am on the right track? This is just one of several different versions I’ve tried. I’m getting a bit tired of spinning my wheels on this one. :blush:

<?php
session_start();
require_once('inc/db_connect.php');

if (isset($_GET['id'])) {
    
    $data = array(
                'first_name' => $_POST['first_name'], 
                'address' => $_POST['address'], 
                'phone' => $_POST['phone'], 
                'id' => $_GET['id']
                );
                
    // update the data
    $query = "UPDATE information
                    SET first_name = :first_name,
                    address = :address,
                    phone = :phone,
                    WHERE id = :id
                    ";
    $stmt = $db->prepare($query);
    echo $query;
    // perform the database query
    $stmt->execute($data);
    
    header('location:edit_info.php?msg=y');
    exit();
} 
?> 

Thanks.

Because you’re using POST for first_name, address and phone, and GET for the id?

Okay, but in one version, I just used variables. I’ll try that one again and get back to you.

 $data = array(
                ':first_name' => $_POST['first_name'], 
                ':address' => $_POST['address'], 
                ':phone' => $_POST['phone'], 
                ':id' => $_GET['id']
                );

Shouldn’t it be :first_name instead of first_name?

Okay. I didn’t know that. Everywhere I look this up, there is something different. So it becomes quite confusing. I’ll try that,
and I switched from passing the ID through the URL to using a hidden input field. Does it really matter, though, whether I use $_POST or $_GET in the array instead of plain variables?

This is what I have now:

<?php
session_start();
require_once('inc/db_connect.php');

if (isset($_POST['id'])) {
    
    $first_name = $_POST['first_name'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $id = $_POST['id'];
    
    $data = array(
                ':first_name' => $first_name, 
                ':address' => $address, 
                ':phone' => $phone, 
                ':id' => $id
                );
                
    // update the data
    $query = "UPDATE information
                    SET first_name = :first_name,
                    address = :address,
                    phone = :phone,
                    WHERE id = :id
                    ";
    $stmt = $db->prepare($query);
    echo $query;
    // perform the database query
    $stmt->execute($data);
    
    header('location:edit_info.php?msg=y');
}
?>

It’s still not working.

It should work anyway… Even if you use a $_GET for the id, it should work as long as your form tag looks similar to:

<form action="form.php?id=123" method="POST">

But, IMO, it’s cleaner to always use POST as much as you can. You should really use GET and pass variables in query string when you want somebody to be able to bookmark the page… but even then, using a ‘friendly URL’ is beter… :smile:

Can you post the code where you have your HTML form etc… ?

This was just a shortened sample of my syntax - the project I’m working on has 32 entries. I’ll set up a test form for this one first, then post it. (Or do you want the original?)

Just to eliminate the blatantly obvious…are you getting the echo of $query? If you are, are you SURE the id exists on the information table?

$first_name => $_POST['first_name'];
    $address => $_POST['address'];
    $phone => $_POST['phone'];
    $id => $_POST['id'];

As far as I know, those should be just “=” operators if you’re defining those variables here. So right now, “$id” (and the other variables) are going to all be null…

It shouldn’t matter, GET vs POST, as long as you’re getting values, but I’d either hard code them for testing or use whatever you’re sure is coming in correctly. Maybe echo them to make sure they’re good values.

Also as @DaveMaxwell said, are you getting that $query echo ok, and you’re sure the id exists (after correcting the above?)

echo and die() are your friends. Make sure that you’re getting all the right values coming in at the start, and then at the end too.

just “=” operators if you’re defining those variables

I had that in the real thing, this was just a matter of rushing too much with my copying and pasting in the sample.

When I echo the query I get, for example, the value of first_name to be :first_name, instead of the actual value.

only for updates. you should be using GET when not making any changes.

Also the OP has neglected to VALIDATE the data before moving it out of the tainted GET and POST arrays and so is allowing GARBAGE to be inserted into the database.

You will - prepared statements don’t include the values. If your code works like your sample above, and you’re getting to the echo, then there’s definitely a value in $_POST[‘id’].

If you want to see what’s in the $data array, you can use var_dump($data),

Guess that brings back question #2 from above. Are you SURE the id being passed is on the table? Is the id being passed the one you’re expecting?

I’ll have every intention of fixing that, but I would like to solve my UPDATE issue first. One step at a time, please.

Guess that brings back question #2 from above. Are you SURE the id
being passed is on the table? Is the id being passed the one you’re
expecting?

I’m getting this when I echo out the variables, and the query.

2 :: Steve :: Toronto :: 432-6543
UPDATE pdo_test
SET first_name = :first_name,
	address = :address,
	phone = :phone,
WHERE id = :id

does this id exist?

What error code is the database call returning (if any)?

Assuming that 2 is the id and 432-6543 is the phone…

That’s not the id. The id was the ‘2’ and yes it exists.

my var_dump() gave me this:

array(4) {
  [":first_name"]=>
  string(5) "Steve"
  [":address"]=>
  string(7) "Toronto"
  [":phone"]=>
  string(8) "432-6543"
  [":id"]=>
  string(1) "2"
}

I’m using ‘try … catch’ with my database connection using

catch(PDOException $e) {
    echo $e->getMessage();
}

and it doesn’t seem to be returning any errors.

Wouldn’t hurt to check. But if other queries work than at least we know the connection is sound, if nothing else…

I would like to post my three files here, but the code tags aren’t allowing the html parts to show up.

Post your code in the textbox. Then highlight all the code. Then when the code is highlighted, in the bar above the textbox, you see some editing items. Like “B” and “I”. Click the button that looks like </>

That formats for code.