Diether Posted December 9, 2012 Share Posted December 9, 2012 Good day Guys, Im a beginner in php and currently making simple add, edit and delete. i have this error message when i click the edit button. Please help me to solve this. thanks in advance Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\simpleProgram\edit.php on line 13 i use this code : index.php <?php include_once ("dbcon.php")?> <html> <head><h1>simple add and edit</h1></head> <title>My simple program</title> <body> <form method = "post" action = "add.php"> <table > <tr><td>Title:</td> <td><input type = "text" name = "title" ></td></tr> <tr><td>Author:</td><td><input type = "text" name = "author" ><td/></tr> <tr><td>Publisher Name:</td> <td><input type = "text" name = "publisherName" ></td></tr> <tr><td>Copyright Year:</td> <td><input type = "text" name = "copyrightYear" ></td></tr> <tr><td></td><td><input type = "submit" name = "submit" value = "add"></td></tr> </table> </form> </br> </br> <table border = "1"> <tr><td>id</td> <td>title</td> <td>author</td> <td>Publisher Name</td> <td>Copyright Year</td></tr> <?php $query = mysql_query("SELECT * FROM books") or die (mysql_error()); while($myBookRows = mysql_fetch_array($query)){ ?> <tr> <td><?php echo $myBookRows['BookID']; ?></td> <td><?php echo $myBookRows['Title'] ; ?> </td> <td><?php echo $myBookRows['Author']; ?> </td> <td><?php echo $myBookRows['PublisherName'] ;?></td> <td><?php echo $myBookRows['CopyrightYear'] ;?></td> <td><a href="delete.php<?php echo '?id='.$myBookRows['BookID']; ?>">delete</a></td> <td><a href="edit.php<?php echo '?id='.$myBookRows['BookID']; ?>">Edit</a></td> </tr> <?php } ?> </table> </body> </html> edit.php <?php include_once("dbcon.php") ; $id = $_GET['id']; ?> <head><h1>simple add and edit</h1></head> <title>My simple program</title> <body> <form method = "post" > <table> <?php $query = ("SELECT * FROM books WHERE BOOKID = '$id' ") or die (mysql_error()); $myBookRows= mysql_fetch_array($query); ?> <html> <tr><td>Title:</td> <td><input type = "text" name = "title" value = "<?php echo $myBookRows['Title'] ?>"></td></tr> <tr><td>Author:</td> <td><input type = "text" name = "author" value = "<?php echo $myBookRows['Author']?>"></td></tr> <tr><td>Publisher Name:</td> <td><input type = "text" name = "publisherName" value "<?php echo $myBookRows['PublisherName'] ?> "> </td> </tr> <tr><td>Copyright Year:</td> <td><input type = "text" name ="copyrightYear" value "<?php echo $myBookRows['CopyrightYear'] ?> "> </td></tr> <tr><td></td> <td> <input type = "submit" name = "submit" value = "save" > </td></tr> </table> </form> </html> <?php if (isset($_POST['submit'])){ $title = $_POST['title']; $author = $_POST['author']; $publisherName = $_POST['publisherName']; $copyrightYear = $_POST['copyrightYear']; mysql_query("UPDATE books SET Title = '$title', Author = '$author', PublisherName = '$publisher',copyrightYear = '$copyrightYear' WHERE BOOKID = '$id'"); header('location : index.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/ Share on other sites More sharing options...
Pikachu2000 Posted December 9, 2012 Share Posted December 9, 2012 You aren't executing the query, you're just passing the variable containing the query string (as would be implied by "string given") to mysql_fetch_array(). Quote Link to comment https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/#findComment-1398406 Share on other sites More sharing options...
Diether Posted December 9, 2012 Author Share Posted December 9, 2012 at pikachu2000,, can you tell me what will be the best solution to my problem. i cant move on,, almost an hour Quote Link to comment https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/#findComment-1398407 Share on other sites More sharing options...
MDCode Posted December 9, 2012 Share Posted December 9, 2012 You need to use mysql_query() first Quote Link to comment https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/#findComment-1398410 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 You can do like this: $query = "SELECT * FROM books WHERE BOOKID = '$id'"; $result = mysql_query($query); if ($result) { // Query successful if (mysql_num_rows($result) > 0) { // At least one row returned // Loop through rows while ($myBookRow = mysql_fetch_array($query)) { $some_column_value = $myBookRow['some_column']; } } else { // No rows returned } } else { // Query not successful } Quote Link to comment https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/#findComment-1398416 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.