表单中的值不会正确显示在数据库中

时间:2013-06-07 21:43:49

标签: php html mysql mysqli

我只想将信息从文本格式转移到数据库中,但该值未正确显示在数据库中。这就是我所拥有的:

HTML代码,表单格式为:

<form method="post" action="process.php">

    <input type="text" maxlength="150" name="textbox">
    <input type="submit" name="submit">

</form>

process.php

<?php
$con=mysqli_connect($host, $username, $password, $database);

$sql = mysqli_query($con,"INSERT INTO notes (User, Note)
VALUES ('test', '$_POST [textbox]')");

// I also tried writing $_POST ['textbox'] instead; didn't make a difference.

?>

但是,数据库中的输出如下:

用户:测试
注意:数组[文本框]

我如何更正Note栏中的值(i-e使其成为表格中输入的值)?

3 个答案:

答案 0 :(得分:3)

首先......你在$ _POST和['textbox']之间有一个空格; 它应该只是$ _POST ['textbox'] ...

但您还需要首先清理数据,以便...... 试试这个

$input = mysqli_real_escape_string($_POST['textbox']);

$sql = mysqli_query($con,"INSERT INTO notes (User, Note)
VALUES ('test', '$input')");

但实际上你应该使用PDO而不是弃用的mysql_ *函数...... Google PDO,并学会做好准备好的陈述。

这是PDO ......

 $conn = new PDO("mysql:host=$host;dbname=$database",$username,$password);

 $user = 'Test';
 $note = $_POST['textbox'];

 $sql = "INSERT INTO notes (User, Note) VALUES (:user,:note)";
 $q = $conn->prepare($sql);
 $q->execute(array(':user'=>$user,
              ':note'=>$note));

...编辑 我也注意到你的输入没有关闭,每个结尾应该有一个/ ...

<form method="post" action="process.php">

    <input type="text" maxlength="150" name="textbox" />
    <input type="submit" name="submit" />

</form>

答案 1 :(得分:0)

$ _POST和[textbox]之间有空格。 $ _POST是一个数组。因此,Array [textbox],即:ArraySPACE [textbox]

您应该删除空格,然后查看使用预准备语句。您不应该直接使用用户提交的数据而不先清理它。

答案 2 :(得分:-1)

$sql = mysqli_query($con,"INSERT INTO notes (User, Note)
VALUES ('test', '{$_POST['textbox']}')");