[英]Using $_POST to insert into MySQL db w/ PDO Prepared Statements
我的业余时间正在做一个Pastebin风格的项目,直到最近才使我震惊,我需要为这些粘贴贴一个标题。 我正在使用PDO,我几乎不得不重做已经插入的整个php脚本,现在我不知道出了什么问题。 这是我的HTML正文的开头,paste.html:
<form action="insert.php" method="post">
Title: <input type="text" name="title">
<br>
Paste: <br> <input type="text" name="paste">
<input type="submit" value="insert">
</form>
这是我的insert.php:
require 'connection.php';
$paste = $_POST['paste'];
$title = $_POST['title'];
$sql = "INSERT INTO pasteinfo (title, paste) VALUES (:title, :paste)";
$stmt = $con->prepare($sql);
$stmt->bindParam(':paste', $paste);
$stmt->bindParam(':title', $title);
$stmt->execute();
$con = null;
我真的不知道这是怎么回事。 谢谢!
编辑:connection.php看起来像这样:
try {
$con = new pdo("mysql:host='';dbname='pastes'",'','','pasteinfo');
echo "connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
尝试做这样的事情:
$stmt->bindParam(':paste', $paste, PDO::PARAM_STR, 12);
$stmt->bindParam(':title', $title, PDO::PARAM_STR, 12);
第三个参数是数据类型,第四个参数是长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.