无法编辑我的MySQL数据库

时间:2017-03-20 19:03:08

标签: php

我想让我的用户编辑数据库中的行。我正在跟进一个教程,但我似乎无法让它发挥作用。我迷失了,因为我不知道为什么我的变量在我明显拥有时(我的头脑中)没有被宣布。

     <?php
    // including the database connection file
    include_once("connect.php");

    if(isset($_POST['update']))
    {    
        $StaffID = $_POST["StaffID"];

        $Name=$_POST["Name"];
        $Address=$_POST['Address'];
        $Telephone=$_POST['Telephone'];  
        $BusinessID=$_POST['BusinessID'];

         $result = mysqli_query($conn, "UPDATE staff SET Name='{$Name}',Address='{$Address}',Telephone='{$Telephone}', BusinessID='{$BusinessID}' WHERE StaffID = $StaffID");

        //redirectig to the display page. In our case, it is index.php
        header("Location: HomePHP.php");
    }
}
?>
<?php
//getting id from url
$StaffID = $_GET['StaffID']; //this is what is giving me the error even though it is exactly how it appears in my database

//selecting data associated with this particular id
$result = mysqli_query($conn, "SELECT * FROM staff WHERE StaffID=$StaffID");

while($res = mysqli_fetch_array($result))
{


    $Name = $res['Name'];
    $Address = $res['Address'];
    $Telephone = $res['Telephone'];
    $BusinessID = $res['BusinessID'];
}
?>

    <html>
    <head>    
        <title>Edit Data</title>
    </head>

    <body>
        <a href="select.php">Home</a>
        <br/><br/>

        <form name="form1" method="post" action="select.php">
            <table border="0">
                <tr> 
                    <td>Name</td>
                    <td><input type="text" name="name" value="<?php echo $Name;?>"></td>
                </tr>
                <tr> 
                    <td>Age</td>
                    <td><input type="text" name="age" value="<?php echo $Address;?>"></td>
                </tr>
                <tr> 
                    <td>Email</td>
                    <td><input type="text" name="email" value="<?php echo $Telephone;?>"></td>
                </tr>
                  <tr> 
                    <td>BusinessID</td>
                    <td><input type="text" name="email" value="<?php echo $BusinessID;?>"></td>
                </tr>
                <tr>
                    <td><input type="hidden" name="id" value=<?php echo $_GET['StaffID'];?>></td>
                    <td><input type="submit" name="update" value="Update"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

1)在BusinessID='$BusinessID'不能,(最后一次分配)之后,SQL出错了。

2)未定义SQL的$id