Php 来自表单的值不为';不能正确地显示在数据库中
我只想将信息从文本表单传输到数据库中,但该值在数据库中没有正确显示。以下是我所拥有的: 表单的HTML代码:Php 来自表单的值不为';不能正确地显示在数据库中,php,html,mysql,mysqli,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
<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.
?>
首先……您在$u POST和['textbox'之间有一个空格;
它应该是$\u POST['textbox']
但您还需要先清理数据,以便。。。
试试这个
但实际上,您应该使用PDO而不是不推荐使用的mysql函数。。。
谷歌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>
在$u POST和[textbox]之间有一个空格$_POST是一个数组。因此,数组[textbox],即:ArraySPACE[textbox]
您应该删除空格,然后研究如何使用准备好的语句。如果用户提交的数据没有经过清理,就不能直接使用。他使用的是mysqli,而不是mysql。您可以使用mysqli编写语句。如果有机会更改数据库引擎,他应该使用PDO。在OOP mysqli上使用PDO还有什么其他原因?好的,我尝试了第一个代码,但从那以后,它给了我一个错误,告诉我textbox是一个无效的索引。。。这是我以前没有得到的。同样感谢POD的建议,我会调查的。你以前没有得到任何东西,因为你在$u POST和['textbox']之间有一个空格,但是现在它是正确的,并且它说无效,有东西告诉我它没有从表单中传递……如果你清除process.php并把你得到的东西放在一个空白页上。。。这是什么意思?
$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>