我一直试图将mysql语句切换到PDO和Prepared Statements,但只是没有得到它。我有一个表单,用户输入数据和这篇文章到一个MySQL数据库。这我相信我已经想通了,因为它实际上工作。这是代码。从MySQL数据填充表使用php pdo准备语句
$stmt = $dbh->prepare("INSERT INTO info(lakeid, name, size, depth, access, sports, beach, atlantic,
bluegill, brook, brown, catfish, coho, crappie, king, lake, largemouth, muskie,
pike, perch, pumkinseed, rainbow, rock, smallmouth, steelhead, sturgeon, walleye,
comments)
VALUES (:lakeid, :name, :size, :depth, :access, :sports, :beach, :atlantic, :bluegill,
:brook, :brown, :catfish, :coho, :crappie,:king, :lake, :largemouth, :muskie, :pike,
:perch, :pumkinseed, :rainbow, :rock, :smallmouth, :steelhead, :sturgeon, :walleye,
:comments)");
$stmt->bindParam(':lakeid', $lakeid, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':size', $size, PDO::PARAM_STR);
$stmt->bindParam(':depth', $depth, PDO::PARAM_STR);
$stmt->bindParam(':access', $access, PDO::PARAM_STR);
$stmt->bindParam(':sports', $sports, PDO::PARAM_STR);
$stmt->bindParam(':beach', $beach, PDO::PARAM_STR);
$stmt->bindParam(':atlantic', $atlantic, PDO::PARAM_STR);
$stmt->bindParam(':bluegill', $bluegill, PDO::PARAM_STR);
$stmt->bindParam(':brook', $brook, PDO::PARAM_STR);
$stmt->bindParam(':brown', $brown, PDO::PARAM_STR);
$stmt->bindParam(':catfish', $catfish, PDO::PARAM_STR);
$stmt->bindParam(':coho', $coho, PDO::PARAM_STR);
$stmt->bindParam(':crappie', $crappie, PDO::PARAM_STR);
$stmt->bindParam(':king', $king, PDO::PARAM_STR);
$stmt->bindParam(':lake', $lake, PDO::PARAM_STR);
$stmt->bindParam(':largemouth', $largemouth, PDO::PARAM_STR);
$stmt->bindParam(':muskie', $muskie, PDO::PARAM_STR);
$stmt->bindParam(':pike', $pike, PDO::PARAM_STR);
$stmt->bindParam(':perch', $perch, PDO::PARAM_STR);
$stmt->bindParam(':pumkinseed', $pumkinseed, PDO::PARAM_STR);
$stmt->bindParam(':rainbow', $rainbow, PDO::PARAM_STR);
$stmt->bindParam(':rock', $rock, PDO::PARAM_STR);
$stmt->bindParam(':smallmouth', $smallmouth, PDO::PARAM_STR);
$stmt->bindParam(':steelhead', $steelhead, PDO::PARAM_STR);
$stmt->bindParam(':sturgeon', $sturgeon, PDO::PARAM_STR);
$stmt->bindParam(':walleye', $walleye, PDO::PARAM_STR);
$stmt->bindParam(':comments', $comments, PDO::PARAM_STR);
$stmt->execute();
我的问题已经得到这个信息打印出来,在原来的网页放回到表中。我已经尝试了几个教程,但没有一个似乎在帮助。对此有任何建议都会有所帮助。 谢谢你一堆。
这是用于将用户输入打印到表格中的编辑代码。我再次对PDO不太了解,但试图防止注射。这实际上将所有正确的输入打印到表格中。还有什么我应该担心的吗? 感谢您的回复。
$stm = $dbh->prepare("SELECT * FROM info WHERE lakeid = ?");
$stm->execute(array("$counties"));
$data = $stm->fetchAll();
Print "<table border cellpadding=5>";
Print "<tr><th>Name:</th> <th>Size:</th> <th>Depth:</th> <th>Boat Access:</th> <th>All Sports:</th> <th>Public Beach:</th> <th>Fish Species:</th> <th>Comments:</th></tr>";
foreach($data as $inform )
{
Print "<tr>";
Print " <td>" .$inform['name']."</td> ";
Print "<td>".$inform['size'] . " </td>";
Print " <td>".$inform['depth'] . "</td> ";
Print " <td>".$inform['access'] . "</td> ";
Print " <td>".$inform['sports'] . "</td> ";
Print " <td>".$inform['beach'] . " </td>";
Print" <td>".$inform['atlantic'] ."\n". $inform['bluegill']. "\n". $inform['brook']. "\n". $inform['brown'].
"\n". $inform['catfish']. "\n". $inform['coho']. "\n". $inform['crappie']. "\n". $inform['king']. "\n". $inform['lake'].
"\n". $inform['largemouth']. "\n". $inform['muskie']. "\n". $inform['pike']. "\n". $inform['perch']. "\n". $inform['pumkinseed'].
"\n". $inform['rainbow']. "\n". $inform['rock']. "\n". $inform['smallmouth']. "\n". $inform['steelhead']. "\n". $inform['sturgeon'].
"\n". $inform['walleye']. "</td>";
Print " <td>".$inform['comments'] . "</td> "; "</tr>";
}
Print "</table>";
你可以添加你试过的代码吗? – andrewsi 2013-04-20 23:07:45
我相信如果我明白你将不得不运行一个选择查询ob页面你想显示结果在哪里你将把id作为lastInsertId() - http://php.net/manual/en/ pdo.lastinsertid.php – Dinesh 2013-04-20 23:09:40
刚刚添加了代码 – karlh 2013-04-20 23:18:18