Member Avatar for Member #743307

Hi All.

I am having some trouble with a script which allows a user to upload a photo. When the page first loads it checks if the user has reached their maximum photo capacity. If they have then it will redirect them to another page. If they can upload more photos it will keep them on the page. Before I added this the user could select which album the photo goes into by using a drop down box. Now when I load the page, there is nothing in the drop down box. Here is the code and if anyone could help, I would be very grateful.
Thanks in advance.
Cameron

<?php
require("check_session.php");

$id = $_SESSION['id'];

$sql = mysql_query("SELECT * FROM users WHERE username='".$_SESSION['username']."'");
			$sql_fetch = mysql_fetch_assoc($sql);
			$row_max = $sql_fetch['max_photos'];
			$row_id = $sql_fetch['id'];

$check = mysql_query("SELECT * FROM items WHERE userid='$row_id'");
			
			
			$check_num = mysql_num_rows($check);
			
			


if($check_num>$row_max)
{
	echo '<meta http-equiv="Refresh" Content="0; URL=maxphotos.php">';

}




?><form enctype="multipart/form-data" action="upload_picture.php" method="POST"> 
<table width="1019" border="0">
  <tr>
    <td><b>Note: Make sure you check wich album you are inserting the image into by looking at the 'Insert into album' drop down menu.</b></td>
  </tr>
</table>
<table width="1020" border="0">
  <tr>
    <td width="418">Please choose a picture to upload</td>
    <td width="16">:</td>
    <td width="497"><input name="uploaded" type="file" /></td>
    <td width="55">&nbsp;</td>
    <td width="12">&nbsp;</td>
  </tr>
  <tr>
    <td>Image Name</td>
    <td>:</td>
    <td><input type="text" name="imgname" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Description</td>
    <td>:</td>
    <td><textarea name="description"></textarea></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Insert into album</td>
    <td>&nbsp;</td>
    <td><select name="albumid">
      <?php
$id = $_SESSION['id'];

$rs = mysql_query("SELECT * FROM albums WHERE userid='$id'");

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n  ";
}
?>
    </select></td>
    <td><input name="upload" type="submit" id="upload" value="Upload" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><b>Uploading process may take 1-2 mins depending on file size.</b></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><p>&nbsp;</p>
    <p><b><a href="index.php">Back</a></b></p></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<br />
<?php

		
		include("design/footer.php");
		?>

Dani AI

Generated

Thread follow-up: the immediate symptom was fixed when the extra characters after each <option> were removed (see ). 's advice to echo the query and run it in the database is a good first step — that confirms the WHERE clause and session value are correct before hunting markup problems.

Checklist for reliable debugging and to avoid the same issue later:

  • Inspect the generated HTML (View Source) to see whether option tags are present and whether any unexpected characters, unclosed tags or control bytes (UTF-8 BOM) appear inside the <select>.
  • Confirm $_SESSION['id'] exists (call session_start() earlier) and that the SQL returns rows; running the echoed SELECT in the DB is a fast verification.
  • Turn on errors during development: enable full error reporting so failed queries are visible.
  • Check that the query resource/success is tested (report any DB error text) and column names in the table match the code (id, name).
  • Avoid sending output before doing a redirect; use header("Location: ...") and exit when redirecting (no previous output).

A safer, more robust pattern for populating the select uses prepared statements and HTML-escaping for labels. Example using mysqli (prepared + escape):

<?php
$mysqli = new mysqli('host','user','pass','db');
$stmt = $mysqli->prepare('SELECT id, name FROM albums WHERE userid = ?');
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->bind_result($aid, $aname);
?>
<select name="albumid">
<?php while ($stmt->fetch()): ?>
  <option value="<?php echo htmlspecialchars($aid, ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($aname, ENT_QUOTES, 'UTF-8'); ?></option>
<?php endwhile; ?>
</select>
<?php $stmt->close(); ?>

Notes and links: confirm session handling with session_start, use header for redirects, escape output with htmlspecialchars, and prefer mysqli/PDO over the deprecated mysql_* functions (see PHP manual for migration).

Recommended Answers

All 4 Replies

Echo query below and run in sql window.
Does it returning result??

echo "SELECT * FROM albums WHERE userid='".$_SESSION['id']."'";
Member Avatar for Member #743307

Echo query below and run in sql window.
Does it returning result??

echo "SELECT * FROM albums WHERE userid='".$_SESSION['id']."'";

Yes it is returning the ID of the user currently logged in.

remove newline. check are matched with id,name.

echo "<option value=\"".$row['id']."\">".$row['name']."</option>";
Member Avatar for Member #743307

remove newline. check are matched with id,name.

echo "<option value=\"".$row['id']."\">".$row['name']."</option>";

It works. Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.