Forum Moderators: coopster

Message Too Old, No Replies

UPDATE problem

Not updating database, need help pls.

         

ksponline

10:28 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Hi,

I'm working on a little practice project (in anticipation of a larger one) to try to get the basics down on interacting with a database. The scenario is that someone can enter a user id and retrieve their previously entered data from a database to update it. It works as it should up to the update part now. I'm basing the code on what I've found in "Making Use of PHP."

Here is the code that's used to submit to the database to display the info to be changed:


<form name="form1" method="post" action="tribute_update.php">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr align="left">
<td colspan="2">Update Memorial</td>
</tr>
<?php
// Connect to the database
require_once ('../../Connections/mysql_connect.php');
// Get data
// $query = "SELECT * FROM users WHERE memID='" .$memID . "'";
$query = "SELECT * FROM users WHERE memID='" .$memID . "'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
// while (mysql_fetch_array($result))
?>
Date Submitted:
<input name="dateSubmitted" type="text" value="<?php echo $row['dateSubmitted']?>" size="30">

First Name:
<input name="firstName" type="text" value="<?php echo $row['firstName']?>" size="60">

Last Name:
<input name="lastName" type="text" value="<?php echo $row['lastName'];?>" size="60">

Email:
<input name="email" type="text" value="<?php echo $row['email'];?>" size="60">

Pet's Name:
<input name="petName" type="text" value="<?php echo $row['petName'];?>" size="60">

State:
<input name="state" type="text" value="<?php echo $row['state'];?>" size="15">

Memorial:
<textarea name="memorial" cols="45" rows="3"><?php echo $row['memorial'];?>
<?php }
?>
</table>
</form>

and here is what it submits to, with the update code:

<form name="form1" method="post" action="proc_tribute.php">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr align="left">
<td colspan="2">Update Memorial</td>
</tr>
<?php
// Connect to the database
require_once ('../../Connections/mysql_connect.php');
// Get data
// $query = "UPDATE users SET memorial='$memorial' WHERE memID='$memID'";
$query = "UPDATE users SET memorial=\"". $memorial ."\" WHERE memID=\"".$memID."\"";
$result = mysql_query($query);
if (!$result)
{
die(" Query could not be executed.<br>");
}
else
{
?>
<tr>
<td align="right"><?php echo mysql_affected_rows();?> updated successfully.</td>
<td><input name="dateSubmitted" type="text" value="<?php echo $row['dateSubmitted']?>" size="30"></td>
</tr>
< removed repeated form - same as above>

The form is displayed after the update, but the fields are empty and it indicates that 0 records have been updated.

Can anyone spot the problem, please, or tell me how I should go about this?

Thanks,
ksponline

[edited by: jatar_k at 10:55 pm (utc) on Mar. 4, 2004]
[edit reason] edited code dump [/edit]

Timotheos

11:18 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey there,

The problem may be that your $memID variable is not getting set? Maybe it's just not showing in your code snippet but that should be getting passed around with the $_POST, $_GET or $_SESSION arrays.

Tim

ksponline

12:24 am on Mar 5, 2004 (gmt 0)

10+ Year Member



I tried adding a hidden field to the display page (the one prior to the update page)

<td align="right"><input type="hidden" name="memID" value="<?php $_POST['memID'];?>"></td>

and put this on the page with the update code:

// Put variable into something usable
$memID=$_POST['memID'];

but it's not making any difference. I don't understand what I'm doing wrong, or what I should be doing instead...

Thanks,
ksponline

Timotheos

12:37 am on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Even with the hidden field you're not giving your memID a value. Typically there would be some sort of login or select member page before your display page so that the memID could be assigned.

ksponline

12:57 am on Mar 5, 2004 (gmt 0)

10+ Year Member



Hi,

Thanks...

There was a page previous to the others where the ID is entered. When I understand all this, then I'll incorporate a login system. I'm just making it as basic as possible for now.

Thanks again, I worked with it some more in order to pass the value as you said, and changing the hidden field to this format:

<td align="right"><input type="hidden" name="memID" value="<?php echo "$memID"?>"></td>

made the database update successful. Finally! The updated information didn't display as it should, but hopefully I can pinpoint that. The UPDATE is something new for me though.

Thanks so much again!
ksp