4

我在使用 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 结果资源

任何想法出了什么问题,以及如何解决?

先感谢您!

4

9 回答 9

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].. 替换它。

于 2012-07-16T06:49:51.760 回答
2

StudentEdit.php:@mysql_select_db($database) or die( "Unable to select database");您在执行查询之前忘记调用

于 2012-07-16T06:57:56.170 回答
2

这部分代码是错误的:

$student_id = $_GET[id];

正确的代码是

$student_id = $_GET['id'];

来自expertsnote.com的代码

于 2015-02-17T13:38:52.587 回答
1

缺少此代码

$select_db = mysql_select_db("$db_name");
if (!$select_db) {echo "Error Selecting Database";}
于 2015-02-17T12:17:57.867 回答
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>");
于 2012-07-16T06:56:37.183 回答
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{
        ?>
于 2018-01-19T06:56:09.680 回答
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 语句中出现问题,您现在应该看到错误,希望您能够修复它那里。

于 2012-07-16T06:50:58.937 回答
0

对我来说看起来不正确的是您显示从数据库中检索到的值的方式:

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

它应该是

<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
于 2012-07-16T06:51:16.673 回答
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>
于 2018-10-09T16:11:41.577 回答