4

I am running into a frustrating problem with a PHP script that's supposed to allow me to edit individual rows within my MySQL database.

This is the file where all of the rows from the database are displayed; it works just like it's supposed to.

<table cellpadding="10"> <tr> <td>ID</td> <td>First Name</td> <td>Last Name</td> <td>E-mail</td> <td>Phone</td> </tr> <?php $username="username here"; $password="password here"; $database="database name here"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM students"; $result=mysql_query($query); mysql_close(); while ($row=mysql_fetch_array($result)){ echo ("<tr><td>$row[id]</td>"); echo ("<td>$row[first]</td>"); echo ("<td>$row[last]</td>"); echo ("<td>$row[email]</td>"); echo ("<td>$row[phone]</td>"); echo ("<td><a href=\"StudentEdit.php?id=$row[id]\">Edit</a></td></tr>"); } echo "</table>"; ?> 

As you can see, each row has an "Edit" link that is supposed to allow the user to edit that individual student's data. Here, then, is StudentEdit.php:

<?php $username="username"; $password="password"; $database="database"; mysql_connect(localhost,$username,$password); $student_id = $_GET[id]; $query = "SELECT * FROM students WHERE id = '$student_id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); mysql_close(); ?> <form method="post" action="EditStudentData.php" /> <table> <tr> <td><input type="hidden" name="id" value="<? echo "$row[id]" ?>"></td> </tr> <tr> <td>First Name:</td> <td><input type="text" name="first" value="<? echo "$row[first]" ?>"></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="last" value="<? echo "$row[last]" ?>"></td> </tr> <tr> <td>Phone Number:</td> <td><input type="text" name="phone" value="<? echo "$row[phone]" ?>"></td> </tr> <tr> <td>E-mail:</td> <td><input type="text" name="email" value="<?echo "$row[email]" ?>"></td> </tr> </table> </form> 

When I execute this, however, I get the following error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home4/lukaspl1/public_html/StudentEdit.php on line 12

Any ideas what's wrong, and how to fix it?

Thank you in advance!

2
  • 2
    you forgot to select db before you executed the query in StudentEdit.php Commented Jul 16, 2012 at 6:55
  • try to use single quotes too: $row['last'] Commented Feb 15, 2019 at 21:42

9 Answers 9

3

Remove the mysql_close from here

mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM students"; $result=mysql_query($query); mysql_close(); 

The code should mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM students"; $result=mysql_query($query); 

And moreover,you are going to use only key based resultset.. simply have mysql_fetch_assoc. And another suggestion would be instead of $row[id]..replace it with $row['id'].

Sign up to request clarification or add additional context in comments.

Comments

2

StudentEdit.php: you forgot to call @mysql_select_db($database) or die( "Unable to select database"); before you executed the query

Comments

2

This part of the code is wrong:

$student_id = $_GET[id]; 

the correct code is

$student_id = $_GET['id']; 

code from expertsnote.com

Comments

1

Try...

echo ("<td><a href=\"StudentEdit.php?id=".$row['id']."\">Edit</a></td></tr>"); 

instead of

echo ("<td><a href=\"StudentEdit.php?id=$row[id]\">Edit</a></td></tr>"); 

Comments

1

this code was missing

$select_db = mysql_select_db("$db_name"); if (!$select_db) {echo "Error Selecting Database";} 

Comments

1

this is the cod for edit the details dynamically

<?php include('db.php'); $id=$_REQUEST['id']; $query="SELECT * FROM `camera details` WHERE id='".$id."'"; $result=mysqli_query($db,$query) or die(mysqli_error()); $row1=mysqli_fetch_assoc($result); if(isset($_POST['submit'])&&(isset($_POST['new'])&&($_POST['new'])==1)) { $id=$_REQUEST['id']; foreach($_POST as $key=>$values) { if($key!="submit"){ $names[]=$key; $val[]= "'".$values."'"; if($key!="new"){ $k[] = "`".$key."` = '".$values."'"; } } } $output=implode(",",(array)($k)); //$v=implode(",",(array)($val)); // `name` = 'san' $query="UPDATE `camera details` SET $output WHERE id='".$id."'"; $output=mysqli_query($db,$query) or die(mysqli_error($db)); if($output) { header('location:cameralist.php'); } } else{ ?> 

Comments

0

I recommend doing this in studentEdit.php

$student_id = mysql_real_escape_string($_GET[id]); $query = "SELECT * FROM students WHERE id = '$student_id'"; $result = mysql_query($query) or die(mysql_error() . ' ' . $query); $row = mysql_fetch_array($result); mysql_close(); 

Two things I've changed here is firstly to escape the data being passed in the url and secondly I've added or die(mysql_error() . ' ' . $query); If something is going wrong in the sql statement you should now see the error and hopefully you'll be able to fix it from there.

Comments

0

What looks incorrect to me is the way you are displaying the value retrieved from the database:

<input type="hidden" name="id" value="<? echo "$row[id]" ?>"> 

It should be

<input type="hidden" name="id" value="<?php echo $row['id']; ?>"> 

Comments

0

This code gives the option to add, search, edit and delete options. Thought it might to see all the options in one code.

 $searchedUsername = ""; $searchedEmail = ""; //registration (Add) function if ( isset($_POST['stdregister'])){ $username = $_POST['stdusername']; $password = $_POST['stdpassword']; $email = $_POST['stdemail']; $hashedPassword = md5($password); $connection = mysqli_connect("localhost","root","","std"); $query = "INSERT INTO student VALUES ('$username','$hashedPassword','$email')"; if ( mysqli_query($connection,$query) == 1 ){ echo "Successfully saved"; } else{ echo "<p style='color: #f00;'>There is an error</p>"; } mysqli_close($connection); } //delete function if ( isset($_POST['stddelete'])){ $username = $_POST['stddelusername']; $connection = mysqli_connect("localhost","root","","std"); $query = "DELETE FROM student WHERE username LIKE '$username'"; mysqli_query($connection,$query); echo mysqli_error($connection); mysqli_close($connection); } //update function if ( isset($_POST['stdupdate'])){ $username = $_POST['stdusername']; $stdpass = md5($_POST['stdpassword']); $stdemail = $_POST['stdemail']; $connection = mysqli_connect("localhost","root","","std"); $query = "UPDATE student SET password='$stdpass', email='$stdemail' WHERE username LIKE '$username'"; mysqli_query($connection,$query); echo mysqli_error($connection); mysqli_close($connection); } if ( isset($_POST['stdsearch']) ){ $searchUsername = $_POST['stdeditusername']; $connection = mysqli_connect("localhost","root","","std"); $query = "SELECT * FROM student WHERE username LIKE '$searchUsername' "; $result = mysqli_query($connection, $query); while( $row = mysqli_fetch_array($result) ){ $searchedUsername = $row['username']; $searchedEmail = $row['email']; } } ?> <html> <head> </head> <body> <h1>Student Registration</h1> <form name="stdregistration" action="forms.php" method="post"> <label>Username :</label> <input name="stdusername" required="required" type="text" /><br /><br /> <label>Password :</label> <input name="stdpassword" type="password" /><br /><br /> <label>E-mail :</label> <input name="stdemail" type="email" /><br /><br /> <input name="stdregister" type="submit" value="Save" /> </form> <h2>Delete Students</h2> <form name="stddeletion" action="forms.php" method="post"> <label>Select the Username :</label> <select name="stddelusername" required> <option value="">Select One</option> <?php $connection2 = mysqli_connect("localhost","root","","std"); $query2 = "SELECT username FROM student"; $result = mysqli_query($connection2,$query2); while( $row = mysqli_fetch_array($result) ){ echo "<option value='".$row['username']."'>".$row['username']."</option>"; } mysqli_close($connection2); ?> </select> <input name="stddelete" type="submit" value="Delete" /> </form> <h2>Edit Students</h2> <form name="stdedition" action="forms.php" method="post"> <label>Select the Username :</label> <select name="stdeditusername" required> <option value="">Select One</option> <?php $connection2 = mysqli_connect("localhost","root","","std"); $query2 = "SELECT username FROM student"; $result = mysqli_query($connection2,$query2); while( $row = mysqli_fetch_array($result) ){ echo "<option value='".$row['username']."'>".$row['username']."</option>"; } mysqli_close($connection2); ?> </select> <input name="stdsearch" type="submit" value="Search" /> </form> <form name="stdedit" action="forms.php" method="post"> <label>Username :</label> <input name="stdusername" required="required" type="text" readonly value="<?php echo $searchedUsername; ?>" /><br /><br /> <label>Password :</label> <input name="stdpassword" type="password" /><br /><br /> <label>E-mail :</label> <input name="stdemail" type="email" value="<?php echo $searchedEmail; ?>" /><br /><br /> <input name="stdupdate" type="submit" value="Update" /> </form> </body> </html> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.