简体   繁体   English

通过PHP从MySQL编辑数据

[英]Editing data from MySQL via PHP

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. 我遇到了一个令人沮丧的问题,一个PHP脚本应该允许我编辑MySQL数据库中的各个行。

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: 那么,这是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 警告:mysql_fetch_array():提供的参数不是第12行/home4/lukaspl1/public_html/StudentEdit.php中的有效MySQL结果资源

Any ideas what's wrong, and how to fix it? 任何想法有什么不对,以及如何解决它?

Thank you in advance! 先感谢您!

Remove the mysql_close from here 从这里删除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();

The code should mysql_connect(localhost,$username,$password); 代码应该是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. 而且,您将只使用基于键的结果集..只需要mysql_fetch_assoc。 And another suggestion would be instead of $row[id]..replace it with $row['id']. 另一个建议是代替$ row [id] ..用$ row ['id']替换它。

StudentEdit.php: you forgot to call @mysql_select_db($database) or die( "Unable to select database"); StudentEdit.php:你忘了调用@mysql_select_db($database) or die( "Unable to select database"); before you executed the query 在执行查询之前

This part of the code is wrong: 这部分代码是错误的:

$student_id = $_GET[id];

the correct code is 正确的代码是

$student_id = $_GET['id'];

code from www.ransilu.info 代码来自www.ransilu.info

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

this code was missing 这段代码丢失了

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

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{
        ?>

I recommend doing this in studentEdit.php 我建议在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); 我在这里改变的两件事首先是逃避在url中传递的数据,其次是我添加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. 如果在sql语句中出现问题,您现在应该看到错误,希望您能够从那里修复它。

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']; ?>">

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM