我正在尝试从mysql切换到PDO而不是如何解决这个问题我得到致命错误:在C:\ wamp \ www \ Systems \ insert_process.php上调用未定义的方法PDO :: prepared() 12看看是否有人可以给我一个样本或指出我正确的方向,所有教程都向我展示了如何插入数据,但没有提交表单。
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "systems_requests";
$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$add_member= $dbh->prepared("INSERT INTO requests(lanId, name, department, manager, request, request_description, request_comments, status, comments, compUser, compDt) Values (?,?,?,?,?,?,?,?,?,?)");
$add_member->bindParam(1, $_POST["lanId"]);
$add_member->bindParam(2, $_POST["name"]);
$add_member->bindParam(3, $_POST["department"]);
$add_member->bindParam(4, $_POST["manager"]);
$add_member->bindParam(5, $_POST["request"]);
$add_member->bindParam(6, $_POST["request_description"]);
$add_member->bindParam(7, $_POST["request_comments"]);
$add_member->bindParam(8, $_POST["status"]);
$add_member->bindParam(9, $_POST["comments"]);
$add_member->bindParam(10, $_POST["compUser"]);
$add_member->bindParam(11, $_POST["compDt"]);
$dbh->close();
$email = $_POST["emailaddress"];
$to = "";
$subject = "Systems Request";
$headers = "From: ";
$message = "LanID: " . $lanId . "
" ."User Name: ". $name ."
". "Department: " . $department . "
" ."Manager: ". $manager . "
". "User Request: " . $request . "
" ."User Request Description: ". $request_description . "
" ."User Request comments: ". $request_comments . "
" ."Status: " . $status . "
" ."Systems comments: ". $comments . "
" ."Completed by: ". $compUser ;
mail($to,$subject,$message,$headers);
echo ("<br> <a href='http://a0319p528/dc399Homepage/'> DC399Homepage </a>");
?>
<html>
<body>
<br><br><br>
<h1 align="center">Systems Request Confirmation</h1>
<p align="center">Thank you, <?php echo $_POST["name"]; ?><br><br>
Your request has been sent. Your request number is <?php echo $id;?><br>
Please write this number down or print this page out.</p>
<div align="center">
<h2>Request Information</h2>
Date Request: <?php $date = new DateTime();
echo $date->format('m/d/Y H:i:s') . "\n"; ?><br>
Manager: <?php echo $_POST["manager"]; ?><br>
Location: <?php echo $_POST["department"]; ?><br>
Request Issue: <?php echo $_POST["request"]; ?> <?php echo $_POST["request_description"]; ?><br>
Request Comments: <?php echo $_POST["request_comments"]; ?><br>
</div>
<div align="center">
<h2>Status Information</h2>
Status: <?php echo $_POST["status"]; ?><br><br><br><br>
</div>
<div align="center"><button onClick="window.print()">Print this page</button></div>
</body>
</html>
答案 0 :(得分:7)
您在原始问题下进行了编辑而未将其标记为编辑 如果有人问为什么(答案),这是按your originally posted question。
prepare
而非prepared
这就是您收到致命错误的原因。
$add_member= $dbh->prepare("INSERT INTO requests ...
另外,你没有执行
添加:
$add_member->execute();
后
$add_member->bindParam(11, $_POST["compDt"]);
根据文档
<强> http://php.net/pdo.prepared-statements 强>
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute(); // <=== right there
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
和
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
修改并根据您的评论:
“我现在收到此错误警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与C:\ wamp \ www \ Systems中的标记数不匹配\ insert_process.php在第25行“
你有11个绑定但只有10个值
(?,?,?,?,?,?,?,?,?,?)
添加一个。
(?,?,?,?,?,?,?,?,?,?,?)
答案 1 :(得分:4)
正确的方法名称为prepare,因此请更改为:
$add_member= $dbh->prepare("INSERT INTO requests(lanId, name, department, manager, request, request_description, request_comments, status, comments, compUser, compDt) Values (?,?,?,?,?,?,?,?,?,?)");
答案 2 :(得分:0)
是的,您必须准备函数来构建SQL查询,一旦查询准备就绪或构建正确,它将执行并生成结果。