Jump to content

How come my update isn't working?


Skaterstu

Recommended Posts

Hi Guys,

 

I am designing a photo gallery at the moment, which allows the user to create a photo album, update photos to a database, and then a page is displayed showing thumbnails of pics uploaded with a text box beside it where they can input text for photo captions.

 

I am a little stuck. I have created an application (code displayed below), which allows the user to input captions for each photo, I then use SQL to update the photo database so that the caption info is inserted into each field where there is a photo. All works well, and I get no errors. But the caption information that I write into the text boxes does not get sent to the database. I am starting to think that maybe the caption data needs to be assigned to an array, is this correct? If so, please can someone help me out and I am a little unsure about how to achieve that I want.

 

I have pasted the code below.

 

Thanks

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<h1>Image Caption Page</h1>
<?php

// Includes
include('../includes/sessioncheck_expire.php');
include('../includes/connection.inc.php');

// Convert Session Variables to local variables
$album_id = $_SESSION['album_id'];
$album_name = $_SESSION['album_name'];

// Redirect Variable
$redirect = "../comments_sent.php";

// Check to see if the submit button has been clicked
if(isset($_POST['submit']) && !empty($_POST['submit'])){

  // Convert posted data to variables
  $photo_id = htmlspecialchars(strip_tags($_POST['photo_id']));
  $caption = htmlspecialchars(strip_tags($_POST['caption']));

  // SQL to update the captions field within the database
  $query = "UPDATE photo SET caption = '$caption' WHERE photo_id = '$photo_id'";
  // pass the query through mysql_query() and assign to a variable
  $query_rs = mysql_query($query) or die ('Error!  Could not perform query because: '.mysql_error());
  
  header("Location: $redirect");

}

// SQL
$query = "SELECT * FROM photo WHERE album_id='$album_id'";
// pass the query through mysql_query() and assign to a variable
$query_rs = mysql_query($query) or die ('Error!  Could not perform query because: '.mysql_error());
?>

<form name="Caption Form" method="post">

<table>

<?php
// While loop
while($row = mysql_fetch_assoc($query_rs)){

  $image = htmlspecialchars(strip_tags($row['image']));
  // 
  $thumbSize = getimagesize('../uploads/'.$album_name.'/thumbs/'.$image);
  
  ?>
  
  <tr>
  <td><input type="hidden" name="photo_id" value="<?php echo $photo_id; ?>" /></td>
  <td><img src="../uploads/<?php echo $album_name; ?>/thumbs/<?php echo $image; ?>" alt="<?php echo $thumbSize[3]; ?>" /></td>
  <td><textarea name="caption"></textarea></td> 
  </tr>
  
  <?php
}

?>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html> 

Link to comment
https://forums.phpfreaks.com/topic/73733-how-come-my-update-isnt-working/
Share on other sites

Thanks for the response.

 

I checked if the caption variable had data, and it did but only for one of the captions...  the rest of them were not echoed.  My form is dynamic in that if there are 4 photos then 4 text boxes named 'caption' will be generated.  Is it a problem that I am using 4 textboxes named caption within the same form?  Do I need to somehow create an array to enable the data for each photo to be updated to the correct photo_id?

 

Hmmm, very confused.  Any other help would be greatly appreciated.

 

Cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.