Jump to content

Recommended Posts

Grr. Okay, this script is supposed to add a photo's description to the database. (I'm making a gallery). When I click submit, I get a 404 error and nothing is added.

	<?php
	switch ($_GET['page']) {
		case 'process':
			if(isset($_GET['submit'])) {
				function db_connect() {
					DEFINE('SQL_USER','blah');
					DEFINE('SQL_PASS','blah');
					DEFINE('SQL_HOST','localhost');
					DEFINE('SQL_DB','blah');
					mysql_connect(SQL_HOST, SQL_USER, SQL_PASS);
					mysql_select_db(SQL_DB);
				}
				db_connect();
				$name = mysql_real_escape_string($_POST['name']);
				$desc = mysql_real_escape_string($_POST['desc']);
				$album = (int)$_POST['album'];
				$alt = mysql_real_escape_string($_POST['alt']);
				$filename = $_SESSION['photo_filename'];
				if(empty($album)) { die('You must choose an existing album.'); }
				$query = mysql_query("INSERT INTO photos (name, description, album, alt, filename) VALUES ($name, $desc, $album, $alt, $filename");
				if($query == true) {
					echo '<p>Photo "'.$filename.'" was successfully added to the database. Photo is now available for public viewing.</p>';
				}
				mysql_close();
			}
		break;
		default:			

			if(isset($_SESSION['photo_filename'])) {
			?>
			<p>You have just uploaded the following photo: <?php echo $_SESSION['photo_filename']; ?></p>
			<p>Use the form below to edit the photo's details.</p>
			<form method="post" action="edit_photo?page=process">
			<table border="0">
			<tr><td>Name:</td><td><input type="text" id="name" name="name" size="100" maxlength="255" /></td></tr>
			<tr><td>Description:</td><td><input type="text" id="desc" name="desc" size="100" /></td></tr>
			<tr><td>Album:</td><td><select id="album" name="album">
							<?php
							function db_connect() {
								DEFINE('SQL_USER','blah');
								DEFINE('SQL_PASS','blah');
								DEFINE('SQL_HOST','localhost');
								DEFINE('SQL_DB','blah');
								mysql_connect(SQL_HOST, SQL_USER, SQL_PASS);
								mysql_select_db(SQL_DB);
							}
							db_connect();
							$query = mysql_query("SELECT * FROM albums");
							while(list($id, $name) = mysql_fetch_row($query)) {
							?>
								<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
							<?php
							}
							mysql_close();
							?>
							</select></td></tr>
			<tr><td>Alt:</td><td><input type="text" id="alt" name="alt" size="100" maxlength="255" /></td></tr>
			<tr><td> </td><td><input type="submit" id="submit" name="submit" value="Add Photo" /></td></tr>
			</table>
			</form>
	<?php
			}
		break;
	}
	?>

I know the code is redundant; at least the database function. Can anyone see what is wrong? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/85861-php-issue-mysql/
Share on other sites

Here is what I have so far:

	<?php
	switch ($_GET['page']) {
		case 'process':
			function db_connect() {
				DEFINE('SQL_USER','blah');
				DEFINE('SQL_PASS','blah');
				DEFINE('SQL_HOST','localhost');
				DEFINE('SQL_DB','blah');
				mysql_connect(SQL_HOST, SQL_USER, SQL_PASS);
				mysql_select_db(SQL_DB);
			}
			db_connect();
			$name = mysql_real_escape_string($_POST['name']);
			$desc = mysql_real_escape_string($_POST['desc']);
			$album = (int)$_POST['album'];
			$alt = mysql_real_escape_string($_POST['alt']);
			if(isset($_GET['filename'])) {
				$filename = $_GET['filename'];
			} else {
				$filename = basename($_SESSION['photo_filename']);
			}
			if(empty($album)) { die('You must choose an existing album.'); }
			$query = mysql_query("INSERT INTO photos (name, description, album, alt, filename) VALUES ('$name', '$desc', '$album', '$alt', '$filename'");
			if($query == true) {
				echo '<p>Photo "'.$filename.'" was successfully added to the database. Photo is now available for public viewing.</p>';
			}
			mysql_close();
		break;
		case 'editbyname':
			if(isset($_POST['filename'])) {
				$filename = $_POST['filename'];
			?>
				<p>Use the form below to edit the photo's details.</p>
				<form method="post" action="edit_photo.php?page=process&filename=<?php echo $filename; ?>">
				<table border="0">
				<tr><td>Name:</td><td><input type="text" id="name" name="name" size="50" maxlength="255" /></td></tr>
				<tr><td>Description:</td><td><input type="text" id="desc" name="desc" size="50" /></td></tr>
				<tr><td>Album:</td><td><select id="album" name="album">
								<?php
								function db_connect() {
									DEFINE('SQL_USER','blah');
									DEFINE('SQL_PASS','blah');
									DEFINE('SQL_HOST','localhost');
									DEFINE('SQL_DB','blah');
									mysql_connect(SQL_HOST, SQL_USER, SQL_PASS);
									mysql_select_db(SQL_DB);
								}
								db_connect();
								$query = mysql_query("SELECT * FROM albums");
								while(list($id, $name) = mysql_fetch_row($query)) {
								?>
									<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
								<?php
								}
								mysql_close();
								?>
								</select></td></tr>
				<tr><td>Alt:</td><td><input type="text" id="alt" name="alt" size="50" maxlength="255" /></td></tr>
				<tr><td> </td><td><input type="submit" id="submit" name="submit" value="Add Photo" /></td></tr>
				</table>
				</form>
			<?php
				}
		break;
		default:			

			if(isset($_SESSION['photo_filename'])) {
			?>
			<p>You have just uploaded the following photo: <?php echo $_SESSION['photo_filename']; ?></p>
			<p>Use the form below to edit the photo's details.</p>
			<form method="post" action="edit_photo.php?page=process">
			<table border="0">
			<tr><td>Name:</td><td><input type="text" id="name" name="name" size="50" maxlength="255" /></td></tr>
			<tr><td>Description:</td><td><input type="text" id="desc" name="desc" size="50" /></td></tr>
			<tr><td>Album:</td><td><select id="album" name="album">
							<?php
							function db_connect() {
								DEFINE('SQL_USER','blah');
								DEFINE('SQL_PASS','blah');
								DEFINE('SQL_HOST','localhost');
								DEFINE('SQL_DB','blah');
								mysql_connect(SQL_HOST, SQL_USER, SQL_PASS);
								mysql_select_db(SQL_DB);
							}
							db_connect();
							$query = mysql_query("SELECT * FROM albums");
							while(list($id, $name) = mysql_fetch_row($query)) {
							?>
								<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
							<?php
							}
							mysql_close();
							?>
							</select></td></tr>
			<tr><td>Alt:</td><td><input type="text" id="alt" name="alt" size="50" maxlength="255" /></td></tr>
			<tr><td> </td><td><input type="submit" id="submit" name="submit" value="Add Photo" /></td></tr>
			</table>
			</form>
	<?php
			}
			else {
			?>
			<p>Type in the filename of the photo you want to edit (not added to the database yet) <strong>or</strong> type in a photo id.</p>
			<form method="post" action="edit_photo.php?page=editbyname">
			Filename: <input type="text" id="filename" name="filename" size="50" value="example.jpg" /> <input type="submit" value="Edit" />
			</form>
			<?php
			}
		break;
	}
	?>

When you go to edit a photo the script does nothing. There is no output although here should be. I can't find anything.

Link to comment
https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-438444
Share on other sites

There is only output if

 

if($query == true) {

echo '<p>Photo "'.$filename.'" was successfully added to the database. Photo is now available for public viewing.</p>';

}

 

Other than that, there is no output, which means $query == false, so put a mysql_error() after your query to see what's wrong with it.

 

 

When you go to edit a photo the script does nothing. There is no output although here should be. I can't find anything.

Link to comment
https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-439197
Share on other sites

This is along the same lines as what revraz just posted -

 

In real programming, where you want the code to work no matter what, just about every if() {} statement needs an else {} statement to tell you when the if() test failed and what values caused it to fail.

Link to comment
https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-439210
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.