PHP没有使用简单的表单向数据库发送值

时间:2016-03-25 19:20:07

标签: php mysql database pdo

您好我正在使用PHPStorm,我正在尝试使用php将数据发送到我的数据库。 提交表单时,我的数据库会创建一个新的id,设置为自动递增,但值为空!

这是我在名为create_account.php

的文件中的html表单
<!DOCTYPE>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>NABI| Find the perfect music teach today!</title>
        <link rel="stylesheet" href="css/normalize.css">
        <meta name="viewport" content="width=device-width, initial-scale      =1.0, user-scalable=no">
</head>
<body>
<form method="post" action="info.php">
    <input type="text" name="name" id="name">
    <input type="submit" value="send">

</form>
</body>
</html>

这是我的info.php文件

<?php
    $host = "localhost";
    $user = "root";
    $db = "nabi_data";
    $pass = "";
    $name = filter_input(INPUT_POST, 'name');
    try {
        $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "INSERT INTO just_name (name)
    VALUES ('$name')";
        // use exec() because no results are returned
        $conn->exec($sql);
        echo "New record created successfully";
    } catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
$conn = null;
?>

这就是我的数据库表的样子

just_name

4 个答案:

答案 0 :(得分:1)

  

&#34;谢谢你@Parene,但这就是我设置$ name = $ _POST [&#39; name&#39;]; ---注意:未定义的索引:第6行的C:\ Users \ Vanessa \ PhpstormProjects \ nabi \ info.php中的名字--- - Vanessa Charles 5分钟前&#34;

$name = filter_input(INPUT_POST, 'name'); if(!empty($name)){ // do something as in "enter" it in the database $name = $name; $sql = "INSERT INTO just_name (name) VALUES ('$name')"; // ... rest of your query here } 您正在获取未定义的索引,因为您没有检查它是否为空。

你需要使用条件语句,同时检查它是否为#34;空&#34;。

name

修改

将您的代码修改为此内容并将其完全复制/粘贴为show并在清除缓存时重新加载。

我还在<?php $host = "localhost"; $user = "root"; $db = "nabi_data"; $pass = ""; if(!empty($_POST['name'])){ $name = $_POST['name']; } else { echo "Something went wrong."; } try { $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO just_name (`name`) VALUES ('$name')"; // use exec() because no results are returned $conn->exec($sql); echo "New record created successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; ?> 列添加了刻度,有时候会有所帮助。

<?php
    $host = "localhost";
    $user = "root";
    $db = "nabi_data";
    $pass = "";

    if(!empty($_POST['name'])){
       $name = $_POST['name'];
    }
    else {
       echo "Something went wrong.";
    }

    try {
        $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare("INSERT INTO just_name (`name`) VALUES (:name)");
        $stmt->bindParam(':name', $name);
        $stmt->execute();
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
$conn = null;
?>

虽然应该使用准备好的声明。

<强>即。作为准备好的声明:

C

答案 1 :(得分:0)

如果你输入一个echo,它会返回名称值吗?

...

尝试使用$ _POST获取名称字段内容[&#39; name&#39;];

答案 2 :(得分:0)

为避免出现“未定义索引”错误,您可能需要执行以下操作:

if ($_POST and !empty($_POST['name'])) {
    $host = "localhost";
    $user = "root";
    $db = "nabi_data";
    $pass = "";

    $name = filter_input(INPUT_POST, 'name');

    // debugging step to check to see what you're getting back from the filter_input call (or write it to a log)
    var_dump($name); exit;

    if ($name != '') {
        try {
            $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $sql = "INSERT INTO just_name (name) VALUES ('$name')";
            $conn->exec($sql);
            echo "New record created successfully";
        } catch (PDOException $e) {
            echo $sql . "<br>" . $e->getMessage();
        }
    }
    else {
        // filtered name was blank; raise an error and/or redirect them to create_account.php
    }

    $conn = null;
}
else {
    // handle this case; maybe just redirect back to create_account.php
}

最大的关键是检查$_POST中是否存在“名称”(也可通过$_REQUEST全局变量访问);您可以通过isset()empty()完成此操作。

(是的,正如其他人所建议的那样,检查$name之后filter_input调用后获得的价值将是一件非常棒的事情,可以验证您是否还没有。)

答案 3 :(得分:0)

问题是您使用的是PhpStorm内置的Web服务器,它有POST问题。  从PHP 5.4.0开始,CLI SAPI提供了一个内置的Web服务器,通过选择“PHP内置Web服务器”类型的运行/调试配置来尝试使用它。

或者手动尝试:

cd ~/www/your_project
php -S localhost:8000

这是manual page