can't update my database
Posted: Wed Jan 30, 2008 8:34 am
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