Mikell Posted July 8, 2010 Share Posted July 8, 2010 Im having a difficult time figuring out why this isn't working, my searches and wandering fellow forums arn't helping with the problem so I hope you awesome individuals will help a Newbie. When I submit the form I get this error. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sitename/public_html/admin/edit_avatars.php on line 71 Heres the whole code. <?php //including the database connection file mysql_connect('localhost', '####', '####'); mysql_select_db('######'); if(isset($_POST['update'])) { $id = $_POST['id']; $date=$_POST['date']; $author=$_POST['author']; $type=$_POST['type']; $thumb=$_POST['thumb']; if(empty($name) || empty($age) || empty($email)) { //if name field is empty if(empty($date)) { echo "<font color='red'>Name field is empty.</font><br/>"; } //if age field is empty if(empty($author)) { echo "<font color='red'>Age field is empty.</font><br/>"; } //if email field is empty if(empty($type)) { echo "<font color='red'>Email field is empty.</font><br/>"; } //if email field is empty if(empty($thumb)) { echo "<font color='red'>Email field is empty.</font><br/>"; } } else { //updating the table $result=mysql_query("UPDATE avatars SET date='$date',author='$author',type='$type',thumb='$thumb' WHERE id=$id") or die ('Error: '.mysql_error ()); header("Location: avatars.php"); } } ?> <?php $id = $_GET['id']; $result=mysql_query("select * from avatars where id=$id"); while($res=mysql_fetch_array($result)) { $date = $res['date']; $author = $res['author']; $type = $res['type']; $thumb = $res['thumb']; } ?> <form name="form1" method="post" action="edit_avatars.php"> <table border="0"> <tr> <td>Date</td> <td> <input type="text" name="date" value=<?php echo $date;?>> </td> </tr> <tr> <td>Creator</td> <td> <input type="text" name="author" value=<?php echo $author;?>> </td> </tr> <tr> <td>Type</td> <td> <input type="text" name="type" value=<?php echo $type;?>> </td> </tr> <tr> <td>URL</td> <td> <input type="text" name="thumb" value=<?php echo $thumb;?>> </td> </tr> <tr> <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>> </td> <td><input type="submit" name="update" value="Update"></td> </tr> </table> </form> Any help would be greatly appreciated...for I have been at this for weeks.... Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/ Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 That error usually indicates the query failed to execute. Echo your query string to make sure it contains the values you'd expect it to. And, add or die(mysql_error()) to the query execution. Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082814 Share on other sites More sharing options...
Mikell Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks....well I thought I fixed it haha but it gave me this error Parse error: syntax error, unexpected T_STRING in /home/sitename/public_html/admin/edit_avatars.php on line 54 Heres the current code <? include ("header.php"); ?> <?php //including the database connection file mysql_connect('localhost', '#####', '#####'); mysql_select_db('#####'); if(isset($_POST['update'])) { $id = $_POST['id']; $date=$_POST['date']; $author=$_POST['author']; $type=$_POST['type']; $thumb=$_POST['thumb']; if(empty($date) || empty($author) || empty($type) || empty($thumb)) { //if name field is empty if(empty($date)) { echo "<font color=\"red\">Date field is empty.</font><br/>"; } //if age field is empty if(empty($author)) { echo "<font color=\"red\">Creator field is empty.</font><br/>"; } //if email field is empty if(empty($type)) { echo "<font color=\"red\">Type field is empty.</font><br/>"; } //if email field is empty if(empty($thumb)) { echo "<font color=\"red\">Thumbnail field is empty.</font><br/>"; } } else { $result=mysql_query("UPDATE avatars SET date='$date',author='$author',type='$type',thumb='$thumb' WHERE id=$id") or die(mysql_error()) //redirectig to the display page. In our case, it is index.php header("Location: avatars.php"); } ?> <?php //getting id from url $id = $_GET['id']; $result=mysql_query("select * from avatars where id=$id"); or die(mysql_error()) while($res=mysql_fetch_array($result)) { $date = $res['date']; $author = $res['author']; $type = $res['type']; $thumb = $res['thumb']; } ?> <form name="form1" method="post" action="edit_avatars.php"> <table border="0"> <tr> <td>Date</td> <td> <input type="text" name="date" value=<?php echo $date;?>> </td> </tr> <tr> <td>Creator</td> <td> <input type="text" name="author" value=<?php echo $author;?>> </td> </tr> <tr> <td>Type</td> <td> <input type="text" name="type" value=<?php echo $type;?>> </td> </tr> <tr> <td>URL</td> <td> <input type="text" name="thumb" value=<?php echo $thumb;?>> </td> </tr> <tr> <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>> </td> <td><input type="submit" name="update" value="Update"></td> </tr> </table> </form> <? include ("footer.php"); ?> Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082819 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 There were several syntax errors, but here it is, al shiny and fixed up. Comments I added are in upper case . . . <?php include ("header.php"); // ELIMINATED REDUNDANT PHP OPEN/CLOSE TAGS //including the database connection file mysql_connect('localhost', '#####', '#####'); mysql_select_db('#####'); if(isset($_POST['update'])) { $id = $_POST['id']; $date=$_POST['date']; $author=$_POST['author']; $type=$_POST['type']; $thumb=$_POST['thumb']; if(empty($date) || empty($author) || empty($type) || empty($thumb)) { //if name field is empty if(empty($date)) { echo "<font color=\"red\">Date field is empty.</font><br/>"; } //if age field is empty if(empty($author)) { echo "<font color=\"red\">Creator field is empty.</font><br/>"; } //if email field is empty if(empty($type)) { echo "<font color=\"red\">Type field is empty.</font><br/>"; } //if email field is empty if(empty($thumb)) { echo "<font color=\"red\">Thumbnail field is empty.</font><br/>"; } } else { $result=mysql_query("UPDATE avatars SET date='$date',author='$author',type='$type',thumb='$thumb' WHERE id=$id") or die(mysql_error()); // WAS MISSING SEMI-COLON //redirectig to the display page. In our case, it is index.php header("Location: avatars.php"); } //getting id from url $id = $_GET['id']; $result=mysql_query("select * from avatars where id=$id") or die(mysql_error()); // SEMI-COLON WAS IN WRONG PLACE while($res=mysql_fetch_array($result)) { $date = $res['date']; $author = $res['author']; $type = $res['type']; $thumb = $res['thumb']; } } // WAS MISSING THIS CLOSING BRACKET ?> <form name="form1" method="post" action="edit_avatars.php"> <table border="0"> <tr> <td>Date</td> <td> <input type="text" name="date" value="<?php echo $date; ?>"> </td> </tr> <tr> <td>Creator</td> <td> <input type="text" name="author" value="<?php echo $author; ?>"> </td> </tr> <tr> <td>Type</td> <td> <input type="text" name="type" value="<?php echo $type; ?>"> </td> </tr> <tr> <td>URL</td> <td> <input type="text" name="thumb" value="<?php echo $thumb; ?>"> </td> </tr> <tr> <td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"> </td> <td><input type="submit" name="update" value="Update"></td> </tr> </table> </form> <?php include ("footer.php"); // OLD SHORT OPEN TAG <? REPLACED WITH CORRECT <?PHP TAG SYNTAX ?> Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082825 Share on other sites More sharing options...
Mikell Posted July 8, 2010 Author Share Posted July 8, 2010 Awesome thank you very much for pointing out the things that were wrong.. the comments helped alot. It updates beautifully now but for some reason I get this after I click update.. Warning: Cannot modify header information - headers already sent by (output started at /home/sitename/public_html/admin/header.php:14) in /home/sitename/public_html/admin/edit_avatars.php on line 61 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 does this have something to do with the header I'm calling in the beginning? Also ...all of the text field in the form are not echo'ing out what is already being stored in the database.. is it because the id isnt being passed through when clicking the edit button for the respected id? Thanks for you time ! I really appreciate it! Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082833 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 You can't send a redirect ( header("Location: avatars.php"); on line 39 ) if there has already been anything at all, even whitespace, output to the browser. To fix that, here's what I'd do. I stripped out my last comments and replaced them with current ones, in CAPS. Regarding the id not being passed, I think I had put the closing curly brace in the wrong place. Didn't notice you were using both POST and GET with that script. <?php //including the database connection file mysql_connect('localhost', '#####', '#####'); mysql_select_db('#####'); if(isset($_POST['update'])) { $id = $_POST['id']; $date=$_POST['date']; $author=$_POST['author']; $type=$_POST['type']; $thumb=$_POST['thumb']; if(empty($date) || empty($author) || empty($type) || empty($thumb)) { $errors = array(); //INITIALIZE AN ARRAY TO HOLD ANY ERRORS //if name field is empty if(empty($date)) { $errors[] = "<font color=\"red\">Date field is empty.</font><br/>"; // IF THERE'S AN ERROR, STORE IT IN THE ARRAY FOR LATER ECHO. } //if age field is empty if(empty($author)) { $errors[] = "<font color=\"red\">Creator field is empty.</font><br/>"; // SAME HERE . . . etc. } //if email field is empty if(empty($type)) { $errors[] = "<font color=\"red\">Type field is empty.</font><br/>"; } //if email field is empty if(empty($thumb)) { $errors[] = "<font color=\"red\">Thumbnail field is empty.</font><br/>"; } } else { $result=mysql_query("UPDATE avatars SET date='$date',author='$author',type='$type',thumb='$thumb' WHERE id=$id") or die(mysql_error()); //redirectig to the display page. In our case, it is index.php header("Location: avatars.php"); exit(); // ALWAYS CALL EXIT() AFTER A header() TO STOP THE SCRIPT FROM EXECUTING FURTHER. } } // MOVED if( isset($_POST[' . . .) CLOSING BRACE UP TO WHERE I BELIEVE IT SHOULD HAVE BEEN BEFORE . . . . : ) //getting id from url $id = $_GET['id']; $result=mysql_query("select * from avatars where id=$id") or die(mysql_error()); while($res=mysql_fetch_array($result)) { $date = $res['date']; $author = $res['author']; $type = $res['type']; $thumb = $res['thumb']; } include ("header.php"); // MOVE THE include() AFTER THE REDIRECT ?> <form name="form1" method="post" action="edit_avatars.php"> <table border="0"> <?php if( !empty($errors) ) { // IF THERE ARE ERRORS, LOOP THROUGH AND DISPLAY THEM echo "<tr><td>"; foreach($errors as $val) { echo $val; } echo "</td></tr>"; } ?> <tr> <td>Date</td> <td> <input type="text" name="date" value="<?php echo $date; ?>"> </td> </tr> <tr> <td>Creator</td> <td> <input type="text" name="author" value="<?php echo $author; ?>"> </td> </tr> <tr> <td>Type</td> <td> <input type="text" name="type" value="<?php echo $type; ?>"> </td> </tr> <tr> <td>URL</td> <td> <input type="text" name="thumb" value="<?php echo $thumb; ?>"> </td> </tr> <tr> <td><input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"> </td> <td><input type="submit" name="update" value="Update"></td> </tr> </table> </form><?php include ("footer.php"); ?> Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082840 Share on other sites More sharing options...
Mikell Posted July 8, 2010 Author Share Posted July 8, 2010 Once again, thank you! worked perfectly. After I posted my last post I had noticed the ... thread called HEADER ERRORS - READ HERE BEFORE POSTING THEM and read it but your comments made me understand it much better haha...but thanks once again your such a big help! Topic Solved~! Thanks Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082844 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 No problem at all. Glad to help out. Please don't forget to mark the thread as "Solved" (lower left corner of the page), if indeed it is. Link to comment https://forums.phpfreaks.com/topic/207087-not-updating-database-table-via-web-form/#findComment-1082845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.