doo6568 2016-09-05 19:23
浏览 98
已采纳

PHP / MySQL在数据库中创建空白记录

I have a user input form(HTML) that is supposed to take the information and insert it into a MySQL database via PHP. The PHP apparently executes and echoes "Your registration has completed successfully". A record is created in the database but the columns are blank(I have removed my server, database, and password from the PHP code).

HTML:

<!DOCTYPE html>

<head>

<link rel="stylesheet" type="text/css" href="css/styles.css">
<title>User Portal</title>

</head>

    <div class="inputContainer">

        <header>
            User Information Portal
        </header>

            <form action="php/userPost.php" method="post">

                <label for=firstName">First Name</label>
                <input type="text" id=firstName" name="fname">

                <br><br>

                <label for="lastName">Last Name</label>
                <input type="text" id="lastName" name="lname">

                <br><br>

                <label for="eMail">Email</label>
                <input type="text" id="eMail" name="email">

                <br><br>

                <label class="labelRole" for="userRole">Role -</label><br>
                <input type="radio" id="userRole" name="role" value="Instructor"> Instructor

                <input class="submitButton" type="submit" name="submit" value="Register">

            </form>

    </div>
    </body>

PHP:

<?php

$sname = "server-name";
$uname = "username";
$pword = "password";
$dbname = "web_tech_test";
$conn = new mysqli($sname, $uname, $pword, $dbname);



if ($conn->connect_error) {
    die("Connection failure: " . $conn->connect_error);
}

$fname = !empty($_POST['firstName']);
$lname = !empty($_POST['lastName']);
$email = !empty($_POST['eMail']);
$role = isset($_POST['userRole']);


$sql = "INSERT INTO users (first_name, last_name, email, role)
VALUES ('$fname', '$lname', '$email', '$role')";

if ($conn->query($sql) === TRUE) {
    echo "Your registration has completed successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

This creates a new record in the DB but all the columns are blank. Any ideas why this may be happening?

  • 写回答

2条回答 默认 最新

  • douhao2856 2016-09-05 19:24
    关注
    $fname = !empty($_POST['firstName']);
    $lname = !empty($_POST['lastName']);
    $email = !empty($_POST['eMail']);
    $role = isset($_POST['userRole']);
    

    this code returns a boolean, not a string value...

    Use !empty() just for validation

    example

    if(empty($_POST['eMail'])) {
         die("Email cannot be empty");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误