suttercain Posted April 8, 2007 Share Posted April 8, 2007 Hello and happy "Zombie Jesus" day to everyone, I am attempting my first update database, via a form, page. I have all the PHP code and HTML code and the connection to the database is working. But for some reason when I look at the page it's white. Not even the HTML is showing up. Here is my entire code: <html> <head> <title>ADMIN: Edit News</title> <style type="text/css"> <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; } .style3 {font-size: 12px} --> </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head> <body> <?php $story_id = $_POST['story_id']; require ('get_connected.php'); $query = "SELECT * FROM news WHERE story_id='story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $title = mysql_result($result, $i, 'title'); $body = mysql_result($result, $i, 'body'); $story_date = mysql_result($result, $i, 'story_date'); $author = mysql_result($result, $i, 'suthor'); $image = mysql_result($result, $i, 'image'); $image_text = mysql_result($result, $i, 'image_text'); $source = mysql_result($result, $i, 'source'); ?> <form action="edit_news_complete.php" method="post"> <table width="840" border="0" cellspacing="0" cellpadding="0"> <tr> <td><?php include ('header.html'); ?></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Use this form to submit news instantaneously to the Superman Database!</strong></td> </tr> <tr> <td><span class="style3">Please follow the directions that appear underneath the form input fields. An error can mess up the look of the news story when users are viewing it. </span></td> </tr> <tr> <td><input type="hidden" name="ud_story_id" value="<? echo $story_id; ?>"></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Title:</strong></td> </tr> <tr> <td><input name="ud_title" type="text" value="<?php echo $title; ?>" size="42" maxlength="40"></td> </tr> <tr> <td><span class="style3">The tiltle must be 40 Characters or less. Spaces counts as a character. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Story Body:</strong></td> </tr> <tr> <td><textarea name="ud_body" rows="15" cols="60" ><?php echo $body; ?></textarea></td> </tr> <tr> <td><span class="style3">Seperate paragraphs with a <p> tag. This will add a double spacing between paragraph. Please no other HTML code. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Story Date:</strong></td> </tr> <tr> <td><input type="text" name="ud_story_date" value="<?php echo $story_date; ?>"></td> </tr> <tr> <td><span class="style3">Please format the date like such: March 23rd, 2005 </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Author:</strong></td> </tr> <tr> <td><input type="text" name="ud_author" value="<?php echo $author; ?>"></td> </tr> <tr> <td><span class="style3">Credit the writer of the story for their work </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Image Name: </strong></td> </tr> <tr> <td><select name='ud_image'> <?php $dir = "../images/news"; $dir = opendir($dir); while (false !== ($image = readdir($dir))){ if (in_array($image, array(".", ".."))) continue; ?> <option value='<?php echo $image; ?>'><?php echo $image; ?></option> <?php } ?> </select></td> </tr> <tr> <td><span class="style3">Select an available photo or <a href="picture_upload.php" target="_parent">upload one</a>. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Image Description: </strong></td> </tr> <tr> <td><input name="ud_image_text" type="text" value="<?php echo $image_text; ?>"></td> </tr> <tr> <td><span class="style3">This is the text that will appear below the image in the news story. Use this field to describe who or what is in the picture. Be as detailed as possible in one or two sentences. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Source:</strong></td> </tr> <tr> <td><p> <input type="text" name="ud_source" value="<?php echo $source ?>"> </p></td> </tr> <tr> <td> <p class="style3">Please enter the source of this story. Starting at www. DO NOT enter http://. Example entry www.supermandatabase.com </p></td> </tr> <tr> <td></td> </tr> </table> <br><input type="submit" value="Update"> </form> <?php ++$i; } $ud_story_id = $_POST['ud_story_id']; $ud_title = $_POST['ud_title']; $ud_body = $_POST['ud_body']; $ud_story_date = $_POST['ud_story_date']; $ud_author = $_POST['ud_author']; $ud_image = $_POST['ud_image']; $ud_source = $_POST['ud_source']; $ud_image_text = $_POST['ud_image_text']; require ('get_connected.php'); $query = "UPDATE news SET title = '$ud_title', body = '$ud_body', story_date = '$ud_story_date', author = '$ud_author', image = '$ud_image', source = '$ud_source', image_text = '$ud_image_text' WHERE story_id = '$ud_story_id'"; ?> </body> </html> I understand there may be errors, since this is my first shot at it, but I don't understand why I am getting a blank page... shouldn't I at least be seeing the from? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
esukf Posted April 8, 2007 Share Posted April 8, 2007 Check you have error reporting turned on in your php.ini or add <?php error_reporting(E_ALL); ?> on the very top of your script and see if there are any errors. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 8, 2007 Author Share Posted April 8, 2007 Okay, I ran the error reporting... (I have to remember that) and I got this: Notice: Undefined index: story_id in D:\wamp\www\Superman\edit_news.php on line 17 Notice: Undefined index: ud_story_id in D:\wamp\www\Superman\edit_news.php on line 159 Notice: Undefined index: ud_title in D:\wamp\www\Superman\edit_news.php on line 160 Notice: Undefined index: ud_body in D:\wamp\www\Superman\edit_news.php on line 161 Notice: Undefined index: ud_story_date in D:\wamp\www\Superman\edit_news.php on line 162 Notice: Undefined index: ud_author in D:\wamp\www\Superman\edit_news.php on line 163 Notice: Undefined index: ud_image in D:\wamp\www\Superman\edit_news.php on line 164 Notice: Undefined index: ud_source in D:\wamp\www\Superman\edit_news.php on line 165 Notice: Undefined index: ud_image_text in D:\wamp\www\Superman\edit_news.php on line 166 I know that it saying I have not defined the variables... but I am not sure why it's saying that. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 8, 2007 Author Share Posted April 8, 2007 Does anyone know of a good tutorial that can show me how to UPDATE MySQL via a web based form? Thanks. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 8, 2007 Share Posted April 8, 2007 here's what you're missing: if(isset($_POST)){ $ud_story_id = $_POST['ud_story_id']; $ud_title = $_POST['ud_title']; $ud_body = $_POST['ud_body']; $ud_story_date = $_POST['ud_story_date']; $ud_author = $_POST['ud_author']; $ud_image = $_POST['ud_image']; $ud_source = $_POST['ud_source']; $ud_image_text = $_POST['ud_image_text']; require ('get_connected.php'); $query = " UPDATE news SET title = '$ud_title', body = '$ud_body', story_date = '$ud_story_date', author = '$ud_author', image = '$ud_image', source = '$ud_source', image_text = '$ud_image_text' WHERE story_id = '$ud_story_id' "; mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 8, 2007 Author Share Posted April 8, 2007 Hi Boo, Thanks for the reply, I ran the if statement: if (isset($_POST)) { $ud_story_id = $_POST['ud_story_id']; $ud_title = $_POST['ud_title']; $ud_body = $_POST['ud_body']; $ud_story_date = $_POST['ud_story_date']; $ud_author = $_POST['ud_author']; $ud_image = $_POST['ud_image']; $ud_source = $_POST['ud_source']; $ud_image_text = $_POST['ud_image_text']; require ('get_connected.php'); $query = "UPDATE news SET title = '$ud_title', body = '$ud_body', story_date = '$ud_story_date', author = '$ud_author', image = '$ud_image', source = '$ud_source', image_text = '$ud_image_text' WHERE story_id = '$ud_story_id'"; mysql_query($query); } ?> But unfortunately I got the same error as lsited above. Is there something else I may be missing. Again thanks for the help. SC Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 8, 2007 Share Posted April 8, 2007 do this: function sanitize($array){ foreach($array as $key => $val){ if(is_array($val)){ sanitize($val); } $val = mysql_real_escape_string(strip_tags(trim($val))); } return $array; } if(isset($_POST) && !empty($_POST)){ sanitize($_POST); $query = " UPDATE news SET title = '". $_POST['ud_title'] ."', body = '". $_POST['ud_body'] ."', story_date = '". $_POST['ud_story_date'] ."', author = '". $_POST['ud_author'] ."', image = '". $_POST['ud_image'] ."', source = '". $_POST['ud_source'] ."', image_text = '". $_POST['ud_image_text'] ."' WHERE story_id = '". $_POST['ud_story_id'] ."' "; mysql_query($query) OR die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 8, 2007 Author Share Posted April 8, 2007 Hi boo, I ran that code above and got theis error: Notice: Undefined index: story_id in D:\wamp\www\Superman\edit_news.php on line 17 Thanks for taking the time to help me with this. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 8, 2007 Share Posted April 8, 2007 at the top of your code, replace this: $story_id = $_POST['story_id']; require ('get_connected.php'); $query = "SELECT * FROM news WHERE story_id='story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); with this: if(isset($_POST) && !empty($_POST)){ $story_id = $_POST['story_id']; } require ('get_connected.php'); $query = "SELECT * FROM news WHERE story_id='$story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); EDIT: by the way that's why your page wasn't displaying any HTML. because you had 'WHERE story_id='story_id', instead of WHERE story_id='$story_id'... just fyi Quote Link to comment Share on other sites More sharing options...
esukf Posted April 8, 2007 Share Posted April 8, 2007 You can generally ignore notice warning, change error reporting to error_reporting(E_ALL ^ E_NOTICE); Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 Hi Boo, I just got back and tried this code. I ran the code you have above and am still getting the same error: Notice: Undefined index: story_id in D:\wamp\www\Superman\edit_news.php on line 17 Should I be doing something like?: $ud_story_id = isset($_POST['ud_story_id']) ? $_POST['ud_story_id'] : ''; Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 Hi guys, thanks for the advice. I made the change suggested above and ran this code <?php if(isset($_POST) && !empty($_POST)){ $story_id = $_POST['story_id']; } require ('get_connected.php'); $query = "SELECT * FROM news WHERE story_id='story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $title = mysql_result($result, $i, 'title'); $body = mysql_result($result, $i, 'body'); $story_date = mysql_result($result, $i, 'story_date'); $author = mysql_result($result, $i, 'suthor'); $image = mysql_result($result, $i, 'image'); $image_text = mysql_result($result, $i, 'image_text'); $source = mysql_result($result, $i, 'source'); ?> <form action="edit_news_complete.php" method="post"> <table width="840" border="0" cellspacing="0" cellpadding="0"> <tr> <td><?php include ('header.html'); ?></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Use this form to submit news instantaneously to the Superman Database!</strong></td> </tr> <tr> <td><span class="style3">Please follow the directions that appear underneath the form input fields. An error can mess up the look of the news story when users are viewing it. </span></td> </tr> <tr> <td><input type="hidden" name="ud_story_id" value="<? echo $story_id; ?>"></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Title:</strong></td> </tr> <tr> <td><input name="ud_title" type="text" value="<?php echo $title; ?>" size="42" maxlength="40"></td> </tr> <tr> <td><span class="style3">The tiltle must be 40 Characters or less. Spaces counts as a character. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Story Body:</strong></td> </tr> <tr> <td><textarea name="ud_body" rows="15" cols="60" ><?php echo $body; ?></textarea></td> </tr> <tr> <td><span class="style3">Seperate paragraphs with a <p> tag. This will add a double spacing between paragraph. Please no other HTML code. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Story Date:</strong></td> </tr> <tr> <td><input type="text" name="ud_story_date" value="<?php echo $story_date; ?>"></td> </tr> <tr> <td><span class="style3">Please format the date like such: March 23rd, 2005 </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Author:</strong></td> </tr> <tr> <td><input type="text" name="ud_author" value="<?php echo $author; ?>"></td> </tr> <tr> <td><span class="style3">Credit the writer of the story for their work </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Image Name: </strong></td> </tr> <tr> <td><select name='ud_image'> <?php $dir = "../images/news"; $dir = opendir($dir); while (false !== ($image = readdir($dir))){ if (in_array($image, array(".", ".."))) continue; ?> <option value='<?php echo $image; ?>'><?php echo $image; ?></option> <?php } ?> </select></td> </tr> <tr> <td><span class="style3">Select an available photo or <a href="picture_upload.php" target="_parent">upload one</a>. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Image Description: </strong></td> </tr> <tr> <td><input name="ud_image_text" type="text" value="<?php echo $image_text; ?>"></td> </tr> <tr> <td><span class="style3">This is the text that will appear below the image in the news story. Use this field to describe who or what is in the picture. Be as detailed as possible in one or two sentences. </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><strong>Source:</strong></td> </tr> <tr> <td><p> <input type="text" name="ud_source" value="<?php echo $$source ?>"> </p></td> </tr> <tr> <td> <p class="style3">Please enter the source of this story. Starting at www. DO NOT enter http://. Example entry www.supermandatabase.com </p></td> </tr> <tr> <td></td> </tr> </table> <br><input type="submit" value="Update"> </form> <?php ++$i; } if (isset($_POST)) { $ud_story_id = isset($_POST['ud_story_id']) ? $_POST['ud_story_id'] : ''; $ud_title = isset($_POST['ud_title']) ? $_POST['ud_title'] : ''; $ud_body = isset($_POST['ud_body']) ? $_POST['ud_body'] : ''; $ud_story_date = isset($_POST['ud_story_date']) ? $_POST['ud_story_date'] : ''; $ud_author = isset($_POST['ud_author']) ? $_POST['ud_author'] : ''; $ud_image = isset($_POST['ud_image']) ? $_POST['ud_image'] : ''; $ud_source = isset($_POST['ud_source']) ? $_POST['ud_source'] : ''; $ud_image_text = isset($_POST['ud_image_text']) ? $_POST['ud_image_text'] : ''; require ('get_connected.php'); $query = "UPDATE news SET title = '$ud_title', body = '$ud_body', story_date = '$ud_story_date', author = '$ud_author', image = '$ud_image', source = '$ud_source', image_text = '$ud_image_text' WHERE story_id = '$ud_story_id'"; mysql_query($query) OR die(mysql_error()); } ?> </body> </html> I get two diffrent results based on changing this the WHERE If I run this: $query = "SELECT * FROM news WHERE story_id='story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); I get the blank white page. If I run this: $query = "SELECT * FROM news WHERE story_id='$story_id'"; $result = mysql_query($query); $num = mysql_num_rows($result); I get this error: Notice: Undefined variable: story_id in D:\wamp\www\Superman\edit_news.php on line 20 Any additional suggestions? Thanks again guys. SC Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 Got it. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 I knew it was too good to be true. So I am still getting the error... but I got the form to display and populate. Error: Notice: Undefined index: story_id in D:\wamp\www\Superman\edit_news.php on line 17 I split the code into two pages. edit_news.php and edit_news_submitted.php I tried to submit the changes to the form even with the error being displayed and it did not update the record as I knew it wouldn't. Here is the code edit_news.php: I omitted the html table code for easier reading [code] <?php error_reporting(E_ALL); ?> <?php if(isset($_POST) && !empty($_POST)){ $story_id = $_POST['story_id']; } require ('get_connected.php'); $query = "SELECT * FROM news WHERE story_id='2'"; $result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; while ($i<$num) { $title = mysql_result($result, $i, 'title'); $body = mysql_result($result, $i, 'body'); $story_date = mysql_result($result, $i, 'story_date'); $author = mysql_result($result, $i, 'author'); $image = mysql_result($result, $i, 'image'); $image_text = mysql_result($result, $i, 'image_text'); $source = mysql_result($result, $i, 'source'); ?> <form action="edit_news_submitted.php" method="post"> <?php include ('header.html'); ?> <input type="hidden" name="ud_story_id" value="<?php echo $story_id; ?>"> <input name="ud_title" type="text" value="<?php echo $title; ?> <textarea name="ud_body" rows="15" cols="60" ><?php echo $body; ?> <input type="text" name="ud_story_date" value="<?php echo $story_date; ?>"> <input type="text" name="ud_author" value="<?php echo $author; ?>"> <input name="ud_image" type="text" value="<?php echo $image; ?>"> <input name="ud_image_text" type="text" value="<?php echo $image_text; ?>"> <input type="text" name="ud_source" value="<?php echo $source ?>"> <br><input type="submit" value="Update"><br> No Preview is given when "Updating" a News Story. You should verifiy the changes are correct. </form> <?php ++$i; } ?> </body> </html> Here is the code for edit news submitted: <?php error_reporting(E_ALL); ?> <html> <head> <title>ADMIN: Edit News</title> <?php if (isset($_POST)) { $ud_story_id = isset($_POST['ud_story_id']) ? $_POST['ud_story_id'] : ''; $ud_title = isset($_POST['ud_title']) ? $_POST['ud_title'] : ''; $ud_body = isset($_POST['ud_body']) ? $_POST['ud_body'] : ''; $ud_story_date = isset($_POST['ud_story_date']) ? $_POST['ud_story_date'] : ''; $ud_author = isset($_POST['ud_author']) ? $_POST['ud_author'] : ''; $ud_image = isset($_POST['ud_image']) ? $_POST['ud_image'] : ''; $ud_source = isset($_POST['ud_source']) ? $_POST['ud_source'] : ''; $ud_image_text = isset($_POST['ud_image_text']) ? $_POST['ud_image_text'] : ''; require ('get_connected.php'); $query = "UPDATE news SET title = '$ud_title', body = '$ud_body', story_date = '$ud_story_date', author = '$ud_author', image = '$ud_image', source = '$ud_source', image_text = '$ud_image_text' WHERE story_id = '$ud_story_id'"; mysql_query($query) OR die(mysql_error()); echo "Thank you"; } ?> </body> </html> I have been scratching my head for hours on this one. Can anyone suggest a remedy? Thanks.[/code] Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 If I change if(isset($_POST) && !empty($_POST)){ $story_id = $_POST['story_id']; } to if(isset($_POST) && !empty($_POST)){ $story_id = $_POST['ud_story_id']; } The error goes away, but when I submit the changes no update to the database occurs. Thanks again for any help you can provide. SC Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 9, 2007 Share Posted April 9, 2007 the problem stems from your variables not being set. this: $ud_story_id = isset($_POST['ud_story_id']) ? $_POST['ud_story_id'] : ''; $ud_title = isset($_POST['ud_title']) ? $_POST['ud_title'] : ''; $ud_body = isset($_POST['ud_body']) ? $_POST['ud_body'] : ''; $ud_story_date = isset($_POST['ud_story_date']) ? $_POST['ud_story_date'] : ''; $ud_author = isset($_POST['ud_author']) ? $_POST['ud_author'] : ''; $ud_image = isset($_POST['ud_image']) ? $_POST['ud_image'] : ''; $ud_source = isset($_POST['ud_source']) ? $_POST['ud_source'] : ''; $ud_image_text = isset($_POST['ud_image_text']) ? $_POST['ud_image_text'] : ''; is unnecessary. as long as you sanitize user input (which is why i wrote that function for you), you shouldn't spend recources redefining variables. just use the $_POST variable names. this is also a good idea because it makes your code less confusing, instead of adding a step in the process that will make it difficult to debug your code. to see which variables in your $_POST array are being defined, put this line at the bottom of your code: echo "<pre>". print_r($_POST) ."</pre>\n"; this will help you see which variables don't have a NULL value, and which variables are actually making their way through the sql query. Quote Link to comment 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.