Jump to content

[Help]Expects Parameter 1 To Be Resource


Diether

Recommended Posts

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');

}
?>

Link to comment
https://forums.phpfreaks.com/topic/271790-helpexpects-parameter-1-to-be-resource/
Share on other sites

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
}

Archived

This topic is now archived and is closed to further replies.

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