Page 1 of 1

can't update my database

Posted: Wed Jan 30, 2008 8:34 am
by darthems

Code: Select all

<?php 
// include other files
require_once("functions.php");?>
<?php require_once("session.php");?>
<?php require_once("scripts/connection.php");?>
 
<?php 
// get the client's name to display on top of page
    if (isset($_GET['row'])){
        $client_title = $_GET['row'];
        } else {
        $client_title = "";
         }
        $client_name = get_client($client_title);
            ?>
 
<!-- begin form -->
<form name= "UserEdit" action="testt.php" method="POST">
 
<!-- print client name at top of page-->
<?php echo $client_name['company'];?>
<br/>
 
<!-- get all client information from database   -->
<?php
$query = "SELECT * FROM members ORDER BY id ASC";
$result = mysql_query($query);
while ($row= mysql_fetch_array($result)) { 
 
/* print list of clients in sidebar.  The client "id" is added to the url*/
echo "<a href = \"testt.php?row=" . urlencode($row['id']) . "\">{$row['company']}</a><br/>";
}
 
 // --------------- THIS  IS THE PART WHERE I AM HAVING TROUBLE -------------//
 
 // update client information in database
    // 1. get $_POST values from the form below
    if (isset($_POST['submit'])) {
            $username = mysql_prep($_POST['username']);
            $password = mysql_prep($_POST['password']);
            $content = mysql_prep($_POST['content']);
            $id= $_POST['$client_name[id]']; 
     //2. database query    
            $query = "UPDATE members SET 
                username = '{$username}',
                password = '{$password}',
                content = '{$content}'
                WHERE id = '{$id}'";
                $update_result = mysql_query($query);
                if (mysql_affected_rows() == 1)  {
                //success
                } else { //failed
                }
            
            } else {
     //Errors/
     }
    ?>  
    
<!--html form. Form will dynamically displaying content from database-->
Username: <input type="text" name="username" size="30" value="<?php echo $client_name['username']; ?>"/>
<br/>
Password: <input type="text" name="password"  size="30" value=""/>
<br/>
Content:
<textarea cols="50" rows="30"/>
        <?php echo $client_name['content'];?>
</textarea>
<br/>
<input type="submit" name="submit" value="Save Changes" />
</form>

This script dynamically-generates a list of our clients. When you click each
client's name, the value of "id" that corresponds with the client's name is added to the url.
An HTML form dynamically displays content for the client based upon the "id" value
that comes through the url. That part of the script is working fine.

However,

When someone makes changes to the content and submits the form, I would like
the script to update that content in the database.


Right now, it's not updating anything. It just displays the following error:
Notice: Undefined index: $client_name[id] in testt.php on line 42

I don't understand PHP well enough yet to understand what I've done wrong.
I have guessed that I'm seeing this error because the form is not sending any value
for "id" to the script that updates the database.

How do I capture the "id" value that's coming to the form through the url and then
send that value back to the block of code that updates the database so that the script
knows which row to update?

I've tried a few things such as putting an invisible input field into the form, but I'm not
getting anywhere.

Can anyone explain what I'm doing wrong?


BTW, this is what my database looks like:
there is a table called "members"
it has five columns: id, username, password, company, content

Re: can't update my database

Posted: Wed Jan 30, 2008 11:14 am
by Christopher
Instead of the "if (mysql_affected_rows() == 1) {" check, you might want to try "if (mysql_error()) {" and then print the error message to see what the problem is.

Re: can't update my database

Posted: Wed Jan 30, 2008 12:07 pm
by darthems
When I do that, I get the same error message that I had been getting before:

Undefined index: $client_name[id] in testt.php on line 42



eta: When I delete the following, the whole script works relatively fine. The database does get updated.

Code: Select all

$id=$POST['$client_name[0]'];
and

Code: Select all

WHERE id='{$id}'";
The trouble is, when I take those two bits of code out, the script ends up updating every row in the database with the same information! There must be a way to isolate just one row.

Re: can't update my database

Posted: Wed Jan 30, 2008 12:34 pm
by Christopher
That probably should be:

Code: Select all

# <textarea name="client_name" cols="50" rows="30"/>
 
// snip
 
#             $id= $_POST['client_name'];
 

Re: can't update my database

Posted: Wed Jan 30, 2008 1:11 pm
by darthems
Now, the error message says:

Undefined index: $content in testt.php on line 41

If I change the "name" attribute of the textarea from "content" to "client_name," how will I get the information that the user entered into the textarea to go into the "content" column in my database?

Re: can't update my database

Posted: Wed Jan 30, 2008 2:03 pm
by Christopher
Whatever it is named, it just needs to be the same in both places.