Altec Posted January 13, 2008 Share Posted January 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/ Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 One error is you need single quotes around your variables for your INSERT statement. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-438271 Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 If you're getting a 404 error then you should check your form's action attribute is pointing to an existing file. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-438278 Share on other sites More sharing options...
nikefido Posted January 13, 2008 Share Posted January 13, 2008 action="edit_photo?page=process" should have the .php extension on the file name, no? Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-438282 Share on other sites More sharing options...
Altec Posted January 14, 2008 Author Share Posted January 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-438444 Share on other sites More sharing options...
Fyorl Posted January 14, 2008 Share Posted January 14, 2008 What's your error reporting level? If errors are turned off then any parse errors you have will just result in a blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-439194 Share on other sites More sharing options...
revraz Posted January 14, 2008 Share Posted January 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-439197 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2008 Share Posted January 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85861-php-issue-mysql/#findComment-439210 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.