我遇到一个令人沮丧的问题,一个PHP脚本应该允许我编辑MySQL数据库中的各个行。
这是显示数据库中所有行的文件;它的工作原理就像它应该的那样。
<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>";
?>
如您所见,每行都有一个“编辑”链接,该链接应该允许用户编辑该学生的个人数据。那么,这是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>
但是,当我执行此操作时,收到以下错误消息:
警告:mysql_fetch_array():提供的参数不是第12行/home4/lukaspl1/public_html/StudentEdit.php中的有效MySQL结果资源
任何想法有什么问题,以及如何解决?
提前谢谢!
答案 0 :(得分:3)
从这里删除mysql_close
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();
代码应该是mysql_connect(localhost,$ username,$ password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
而且,您将仅使用基于键的结果集..只需要mysql_fetch_assoc。 另一个建议是代替$ row [id] ..用$ row ['id']替换它。
答案 1 :(得分:2)
StudentEdit.php:您在执行查询前忘记致电@mysql_select_db($database) or die( "Unable to select database");
答案 2 :(得分:2)
答案 3 :(得分:1)
...试
echo ("<td><a href=\"StudentEdit.php?id=".$row['id']."\">Edit</a></td></tr>");
而不是
echo ("<td><a href=\"StudentEdit.php?id=$row[id]\">Edit</a></td></tr>");
答案 4 :(得分:1)
此代码丢失
$select_db = mysql_select_db("$db_name");
if (!$select_db) {echo "Error Selecting Database";}
答案 5 :(得分:1)
这是用于动态编辑细节的鳕鱼
<?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{
?>
答案 6 :(得分:0)
我建议在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();
我在这里改变的两件事首先是逃避在url中传递的数据,其次我添加了or die(mysql_error() . ' ' . $query);
如果在sql语句中出现问题,你现在应该看到错误,希望你能够从那里解决它。
答案 7 :(得分:0)
对我来说看起来不正确的是显示从数据库中检索的值的方式:
<input type="hidden" name="id" value="<? echo "$row[id]" ?>">
应该是
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
答案 8 :(得分:0)
此代码提供了添加,搜索,编辑和删除选项的选项。以为可以在一个代码中看到所有选项。
$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>