UPDATE is the bane of my existence

I know, but when I do that, only the PHP shows up. The html just disappears. I’ll try again:

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

<!DOCTYPE html>

<html lang="en" class="no-js">
<head>
    <meta charset = "utf-8"> <!-- must be within the first 512 characters of the html code and before any content  -->
    <title>Test</title>
    <meta name="description" content="">
    <meta name="author" content="">
</head>

<body class="master_list">
    <div id = "wrapper">
        <h1>List of Organizations for Editing</h1>
        <table>
            <tr>
                <th>First Name</th>
                <th>Address</th>
                <th>Phone</th>
            </tr>
            <?php
            // query for selected information for all organizations
            $query = "SELECT id, first_name, address, phone 
                            FROM pdo_test
                            ORDER BY first_name ASC";
                            
            $stmt = $db->query($query);

            $result = $stmt->fetchAll();
            ?>
            <?php
                foreach ($result as $row) {
                    $id = $row['id'];
                    echo '<tr>';
                    echo '<td>' . $row['first_name'] . '</td>';
                    echo '<td>' . $row['address'] . '</td>';
                    echo '<td>' . $row['phone']  . '</td>';
                    echo '<td><a href="edit_info.php?id=' . $id . '">Edit</a></td>';
                    echo '</tr>';
                }            
            ?>
        </table>
    </div><!-- end of #wrapper -->

</body>

</html>
  1. edit_info.php
<?php
session_start();
require_once('inc/db_connect.php');

if (isset($_GET['id'])) {

    $id = $_GET['id'];
    $_SESSION['id'] = $id;
    
    $edit_query = "SELECT id, first_name, address, phone 
                            FROM pdo_test
                            WHERE id = $id";
    $stmt = $db->prepare($edit_query);
    $stmt->execute();
                    
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    // print_r($result);
    $num_items = sizeOf($result);
    // echo $num_items;
    
    //Profile Information
    $first_name = $result['first_name'];
    $address = $result['address'];
    $phone = $result['phone'];
        
} else {
    header('location: edit_info.php');
    exit;
}
?>

<!DOCTYPE html>

<html lang="en" class="no-js">
<head>
    <meta charset = "utf-8"> <!-- must be within the first 512 characters of the html code and before any content  -->
    <title>Restore Canada | Edit Organization Info</title>
    <meta name="description" content="">
    <meta name="author" content="">
</head>

<body class="form_page">
    <div id = "wrapper">
        <h2>Edit Information for "<?php echo $first_name; ?>"</h2>
        <p>Return to the <a href="edit_master_list.php">list of organizations</a>.</p>
        <form action="process_edit.php" method="post">
            <table>
                <tr>    
                    <td><label for="first_name">First Name</label></td>
                    <td>
                        <input type="text" name="first_name" id="first_name" value="<?php echo $first_name; ?>">
                        <input type="hidden" name="id" id="info_id" value="<?php echo $id; ?>">
                    </td>
                </tr>
                <tr>        
                    <td><label for="address">Address</label></td>
                    <td><input type="text" name="address" id="address" value="<?php echo $address; ?>"></td>
                </tr>
                <tr>
                    <td><label for="phone">Phone</label></td>    
                    <td><input type="text" name="phone" id="phone" value="<?php echo $phone; ?>"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="submit" value="Edit Info"></td>
                </tr>
            </table>
        </form>
        
    </div><!-- end of #wrapper -->

</body>

</html>
  1. process_edit.php
<?php
session_start();
require_once('inc/db_connect.php');

// initialize extra variables

if (isset($_POST['id'])) {
    
    $id = $_POST['id'];
    $first_name = $_POST['first_name'];
    $address = $_POST['address'];
    $phone = $_POST['phone']; 
    
    echo $id . " :: " . $first_name . " :: " . $address . " :: " . $phone  . "<br />";
    
    $data = array(
                ':first_name' => $first_name, 
                ':address' => $address, 
                ':phone' => $phone, 
                ':id' => $id
                );
                
    // insert the data
    $query = "UPDATE pdo_test
                    SET first_name = ':first_name',
                    address = ':address',
                    phone = ':phone',
                    WHERE id = ':id'
                    ";
                    
    /*$query = "UPDATE pdo_test
                    SET first_name = $first_name,
                    address = $address,
                    phone = $phone,
                    WHERE id = $id
                    ";*/
                    
        $stmt = $db->prepare($query);
        echo $query . "<br />";
        // perform the database query
        $stmt->execute($data);
         var_dump($data);
        //header('location:edit_master_list.php?msg=yes_update');
        //exit();

} else {
    header('location:edit_master_list.php?msg=no_id');
    exit();
}
?>

This is what I’m getting. :unamused:

IMHO using backticks works better more consistently. eg.

```php
code and mark-up
```

I’ll try the backticks next time.

BTW, my problem has been solved. It’s really a stupid typo (though nobody else noticed it either - :laughing: ). I’ve been staring at the code for too long. There is a stray comma in the query before the WHERE clause.

That’s one reason why @r937 advocates using the leading comma format. eg.

$query = "UPDATE pdo_test
	SET first_name = ':first_name'
	, address = ':address'
	, phone = ':phone'
	,
WHERE id = ':id'
";

A lot easier to spot that way isn’t it?

1 Like

Actually I did notice it, but you kept saying there were additional inputs, so I figured you had edited some fields out. Quotes like this led me to think it was a copy/paste error. Especially when you said you weren’t getting any errors (you should have, that’s an invalid statement)

@Mittineague

That’s good advice. I’ll definitely try it.

@DaveMaxwell

No, the real thing had totally different fields. I just did something simpler so nobody would have to wade through so much code. But both the real thing and the sample had the same stray comma.

I really did spend a very, very long time trying to find the issue before I broke down and asked the Sitepoint experts.

I’m going to go back now and try to figure out why I wasn’t getting any errors on this. Thanks so much for your help.

THE FOLLOWING IS TOTALLY OFFTOPIC

1 Like

also offtopic

re: thread title

1 Like

:laughing: – It took me a while to figure out that first one. Good one!

But you don’t quote ‘:name_placeholders’ as shown in a number of these posts.

Yes, I ended up removing them just before I said I had solved my problem several posts ago. I had just added them as a last desperate attempt to figure out what I was doing wrong. That’s about when I should have left it alone and come back the next day with a fresher mind. Thank you.

The fact that you did not get an error message when you had a syntax issue is disturbing. Did you enable exception handling after constructing your pdo object?

Lack of error messages will make code development more challenging than it needs to be. So add that extra comma back in and do what you need to do to get an error message.

PDO has got three “error modes” if you want to have it display errors, then just after you connect to PDO you need to:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

That will have PDO display errors. On a live server you’ll instead want to do:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and log errors to a log file. In both cases $db is your PDO object

For future consideration - it’s nice of you to be considerate and abbreviate things, if you’re doing it to save the time of those who are helping you. But in cases like this, where it’s likely a syntax error or other similar problem you’re facing, recreating the issue is much, much more complicated than simply showing us what you’re working on, even if it is a long drawn out copy/paste. Unless it’s some sort of proprietary secret code you can’t show off. Otherwise, seeing the original code is definitely worth it.

I know some of my biggest frustrations end up being a misplaced comma or apostrophe or crap like that, and it takes me stepping away and looking at it later with fresh eyes, or screen–shotting or copying it to a colleague in order to get it solved - definitely know how that goes.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.