使用PHP无法更新表尝试了所有可能的组合语法

时间:2016-03-09 13:45:56

标签: php html mysql css

在你标记我的问题之前复制这是我的最后一招。我已经尝试了所有可能的语法没有错误但我无法更新表,虽然我可以插入表但不能更新已经存在的内容。 config.php用于与db。

建立连接
<html>
    <head>
        <title>
            Thank You FOR Entering Data !!
        </title>
    </head>
    <style>
        div {
            width:100%;
            position: absolute;
            bottom:0;
        }
        img {
            position: relative;
            display: block;
            float: right;
        }
    </style>
    <body background="wall.jpg">
        <font face="WenQuanYi Micro Hei Mono" color="white"><b>
            <h1>Thank You For Entering Data</h1>
            <?php
            include("config.php");
            // Data coming into form via POST
            if ($_POST) {
                $empide = $_POST["empid"];
                $name = $_POST["name"];
                $address = $_POST["address"];
                $pnumber = $_POST["pnumber"];
                $email = $_POST["email"];
                $gender = $_POST["gender"];
                if ($con) {
                    echo nl2br("empid= $empide \nNAME = $name\n Address = $address \n Phone Number = $pnumber \n Email = $email \n Gender = $gender  ");
                    echo $empide;
                    //$abc=mysqli_query($con,"INSERT INTO `employee` (`name`, `address`,    `pnumber`, `email`,`gender`) VALUES ('$name', '$address', '$pnumber', '$email','$gender') ;");  
                    $query = mysqli_query($con, "UPDATE 'employee' SET 'name' = '$name',      'address' = '$address', 'email' = '$email', 'pnumber' = '$pnumber',   'gender' = '$gender' WHERE 'employee'.'empid' = '$empide';");
                } else {
                    echo "Unable to make connection with your data base";
                }
            }
            ?>
            <right>
                <div>
                    <a href="index.php"><img src="home.png" width="110" height="110"></a>
                </div>
            </right>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

始终使用 mysqli_connect_errno() 来检测连接错误和

使用 mysqli_error() 来检测查询错误。

编写如下代码: -

// check connection 
if (mysqli_connect_errno()) {
     printf("Connect failed: %s\n", mysqli_connect_error());
     exit();
}
// You have added unnecessary semicolon and simple quote in field name in your query
$sql = "UPDATE employee SET name = '$name', address = '$address', email = '$email', pnumber = '$pnumber', gender = '$gender' WHERE empid = '$empide'";
$result=mysqli_query($con,$sql);
// $result will give you TRUE or FALSE
if (!result) {
    printf("Error: %s\n", mysqli_error($con));
}else{
    // success
}

希望它会对你有所帮助: - )

答案 1 :(得分:0)

您在变量周围使用单引号。在PHP中,即'$name',它将采用字符串$name而不是变量的值。 Update查询应该如下所示

$sql = 'UPDATE employee SET name="'.$name.'", address="'.$address.'" WHERE empid="'.$empide.'"';