使用PHP和mySQL更新表数据

时间:2017-03-20 17:15:02

标签: php mysql

您好我正在尝试根据用户的输入进行更新现有数据的查询。我的员工表有一个自动递增的主键,称为StaffID,在我脑海中,我的查询应该是查找用户输入的员工ID并选择该数据。但事实并非如此,我不断在这个探索的底部得到错误。到目前为止,我有这个代码。

这是我的编辑代码。

digits = [] 
digits += str(12345)

这是我的更新代码。

    <?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

        // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

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



$sql ="SELECT * FROM staff WHERE StaffID = '$UID'"
     or die(mysqli_error());

if(mysqli_num_rows($sql)>=1){
    while($row = mysqli_fetch_array($query)) {
        $Name = $row['Name'];
        $surname = $row['Address'];
        $FBID = $row['Telephone'];
        $IMGNU = $row['BusinessID'];
    }
?>

<form action="update.php" method="post">

<input type="hidden" name="ID" value="<?=$UID;?>"><br>
Name: <input type="text" name="Name" value="<?=$Name?>"><br>
Address: <input type="text" name="Address" value="<?=$Address?>"><br>
Telephone: <input type="text" name="Telephone" value="<?=$Telephone?>"><br>
BusinessID: <input type="text" name="BusinessID" value="<?=$BusinessID?>"><br>

<input type="Submit">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

我现在收到这两个错误。

错误1

  <?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

        // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

    $StaffID= (int)$_POST["StaffID"];

   $Name = $conn->real_escape_string($_REQUEST['Name']);
$Address = $conn->real_escape_string($_REQUEST['Address']);
$Telephone= $conn->real_escape_string($_REQUEST['Telephone']);
$BusinessID = $conn->real_escape_string($_REQUEST['BusinessID']);


    $query="UPDATE staff
            SET Name = '$Name', Address = '$Address', FBID = '$Telephone' 
            WHERE ID='$StaffID";


mysqli_query($query)or die(mysqli_error());
if(mysqli_affected_rows()>=1){
    echo "<p>($ud_ID) Record Updated<p>";
}else{
    echo "<p>($ud_ID) Not Updated<p>";
}
?>

错误2

Undefined index: StaffID in C:\Users\Greg\Documents\UAMP\UwAmp\www\php-project\editStaff.php on line 26

0 个答案:

没有答案