我正在代码中使用文本区域将数据插入数据库。但我的代码中有一些困惑。当我单击提交按钮时,文本区域中的文本消失并且不保存在数据库中。 这是我的守则。 表单编码:
<form action="testing.php" method="post">
<textarea name="text1" rows="10" cols=59></textarea><br>
<input type="submit" name="submit" value="Add Record">
<input type="reset" name="reset" value="Clear Text">
</form>
PHP 编码:
<?php
$text1=mysql_real_escape_string($_POST['text1']);
// connection
$con=mysql_connect("localhost","root","") or die("connection error");
mysql_select_db("test") or die("database error");
//query...
$qry="INSERT INTO mytest(text1) values($text1)";
if(mysql_query($qry))
{
echo "Record have been saved...";
}
else
{
echo "Not Saved";
}
?>
最佳答案
您需要将其用引号引起来。另外,如果您同时使用一些调试功能会更好。
$qry = sprintf("INSERT INTO mytest (text1) VALUES ('%s') ", $text1);
if (mysql_query($qry)) {
echo "Record have been saved...";
} else {
echo "There was an error:".mysql_error();
}
Note: The mysql extension is deprecated. Use mysqli or PDO instead.
关于php - 如何使用php中的textarea向数据库中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454623/