编辑MySQL Row PHP

时间:2016-05-27 02:23:35

标签: php mysql

我尝试使用PHP编辑和更新行。

以下是代码:

的index.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <div class="header">
    <?php include 'header.php';?>
    </div>

    <center>
    <?php
    /* 
        VIEW.PHP
        Displays all data from 'players' table
    */

        // connect to the database
        include_once('../connection.php');

        // get results from database
        $query = "SELECT EmployeeName, DOB, Age, StreetAddress, City, State, ZipCode,
        Email, HomePhone, Wireless, JobTitle, id, HomeDept, Manager FROM headcount ORDER BY `headcount`.`EmployeeName` ASC";

        $response = @mysqli_query($dbc, $query);

        // display data in table
        echo "<p><b>View All</b> | <a href='../employees/rides.php'>Rides</a></p>";

        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>Employee Name</th> <th>Home Department</th> <th>Job Title</th> <th>Edit</th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysqli_fetch_array($response)){

            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['EmployeeName'] . '</td>';
            echo '<td>' . $row['HomeDept'] . '</td>';   
            echo '<td>' . $row['JobTitle'] . '</td>';   
            echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
            echo "</tr>"; 
        } 

        // close table>
        echo "</table>";
    ?>
    </center>

edit.php

<?php
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db("hwss") or die(mysql_error());

$UID = (int)$_GET['ID'];
$query = mysql_query("SELECT * FROM headcount WHERE id = '$UID'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $EmployeeName = $row['EmployeeName'];
        $DOB = $row['DOB'];
        $Age = $row['Age'];
        $email = $row['email'];
    }
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
email: <input type="text" name="ud_email" value="<?=$email;?>"><br>
Name: <input type="text" name="ud_EmployeeName" value="<?=$EmployeeName?>"><br>
DOB: <input type="text" name="ud_dob" value="<?=$DOB?>"><br>
Age: <input type="text" name="ud_Age" value="<?=$Age?>"><br>
<input type="Submit">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>
</body>
</html>

当我点击编辑时,显示&#34;未找到任何条目&#34;使用每个ID网址上的反向链接。我知道我还没有任何更新代码。我只是在添加其余内容之前尝试让它显示数据。

错误消息

Notice: Undefined index: ID in C:\xampp\htdocs\Scheduling\Employees\edit.php on line 5
No entry found. Go back

1 个答案:

答案 0 :(得分:2)

使用:$_GET['id']

$UID = (int)$_GET['id'];

而不是

$UID = (int)$_GET['ID'];