Jump to content

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
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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