Forum Moderators: coopster

Message Too Old, No Replies

Trouble with updating database with PHP form

No errors, but database doesn't update!

         

HBird

5:40 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



I've made a form that populates from a database, then is supposed to update the database when the form is submitted. The form is on the same page as the action code (editphoto.php).

When I hit the Update button, there are no errors and it prints the success message, but the database did not actually get updated.

Also - notably - the link in the success message doesn't contain the $id like it's supposed to (it prints "viewphoto?id="). Is the $id not being passed for some reason?

Can anyone see the problem? I've looked and looked and can't figure it out!

// if the "Update" button has been pushed, update database with new values
if(isset($_POST['update']))
{
include("conn.php");
$query = "UPDATE Table SET site='$site', city='$city', state='$state', country='$country', headline='$headline', caption='$caption', keywords='$keywords' WHERE id='$id'";
if (!mysql_query($query)) die('Error: ' . mysql_error());
else {echo "<h1>Success!</h1> <p>Photo successfully updated. View the <a href=\"viewphoto?id=$id\">updated photo page</a>.</p>";}
mysql_close();
}

// or, if button not pushed yet, query database and fill fields with existing data
else {
include("conn.php");
$id = mysql_real_escape_string($_GET['id']);
$query="SELECT * FROM Photos WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
if ($num!=0) include("variables.php");

// "else" bracket stays open for the following form
?>

<h1>Edit Photo Metadata</h1>

<div id="metatable">
<form method="post" action="editphoto.php">
<input type="hidden" name="id" value="<?php echo $id;?>">
<table border="1" style="border-collapse:collapse; border-width:thin; border: 1px dotted #CCC" bordercolor="#CCCCCC" cellpadding="3">
<tr>
<th width="85">Photo ID:</th>
<td><?php echo $id; ?></td>
</tr>
<tr>
<th>Site name:</th>
<td valign="top"><input type="text" name="site" value="<?php echo $site; ?>" /></td>
</tr>
<tr>
<th>City:</th>
<td><input type="text" name="city" value="<?php echo "$city"; ?>" /></td>
</tr>

<tr>
<th>State/Region:</th>
<td><input type="text" name="state" value="<?php echo "$state";?>" /></td>
</tr>

<tr>
<th>Country:</th>
<td><input type="text" name="country" value="<?php echo "$country"; ?>" /></td>
</tr>
<tr>
<th>Headline:</th>
<td><input style="font-size:11px; font-family:Arial, Helvetica, sans-serif" type="text" name="headline" value="<?php echo "$headline"; ?>" /></td>
</tr>
<tr valign="top">
<th>Caption:</th>
<td><textarea name="caption" cols=60 rows=4><?php echo $caption; ?></textarea></td>
</tr>
<tr valign="top">
<th>Keywords:</th>
<td><textarea name="keywords" cols=60 rows=4><?php echo $keywords; ?></textarea></td>
</tr>
</table>
<br />
<input class="button" style="width:150px;" type="submit" name="update" value="Update This Photo">
</form>
</div>

dreamcatcher

6:42 pm on Nov 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its because you have register globals OFF. You need to reference all your post vars using $_POST.

dc

HBird

8:55 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



That did it! Thanks!