使用php更新数据库

时间:2016-09-16 19:41:00

标签: php mysql

我刚刚在HTML中制作了一个表单,接受用户输入和一个mysql数据库来存储它们,现在在php文件中一切顺利没有错误但是问题是数据永远不会显示在数据库中,这里是php文件:

<?php
if(isset($_POST["submitacc"])){

    $servernm = "localhost";
    $serverusrnm = "root";
    $serverpass = "2003";
    $db = "blue";

    $conn = new mysqli($servernm, $serverusrnm, $serverpass, $db);

    if($conn ->connect_error){
        die("connection failed".$conn->connect_error);
    }

    $fnm = $_POST["fnm"];
    $lnm = $_POST["lnm"];
    $mail = $_POST["mail"];
    $pass = $_POST["pass"];
    $age = $_POST["age"];
    $gender = $_POST["gender"];
    if(isset($_POST["gender"])&&$_POST["gender"]=="male"){
        $gender = "male";
    }else {
        $gender = "female";
    }


$mysql="update createacc set fnm = '$fnm', lnm = '$lnm', mail = '$mail', passwod = '$pass', age = '$age', gender = '$gender' ";

if($conn->query($mysql)== true){
    echo "record updated";
}else {
    echo "error updating record".$conn->error;
}
$conn->close();
}
?>

1 个答案:

答案 0 :(得分:0)

使用mysqli_query()而不是query()。还要在$ mysql变量中使用WHERE子句。 WHERE子句指定应更新的记录或记录。如果省略WHERE子句,则所有记录都将更新!

示例:

if(mysqli_query($conn , $mysql)){

    echo "Records were updated successfully.";

} else {

    echo "ERROR: Could not able to execute $mysql. " . mysqli_error($conn);

}