我已尝试过在此社区和其他网站上找到的所有内容。但我仍未能完成我的目标。
我想要实现的目标:
在一个页面上,我有一个带按钮的输入框。当我填写de ID号码时,我想从mysql中获取与此ID号码相关联的所有信息....由于某种原因,它不起作用。任何提示或提示?
<?php
$servername = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "xxxxxx";
try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT id, voornaam FROM sollicitatie_form WHERE id= "echo ($_POST['zoek'])"";
// $stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->bindParam(':voornaam', $voornaam, PDO::PARAM_STR);
$stmt->exec($sql);
$result = $stmt->fetchAll();
foreach ($result as $row){
echo "{$row['voornaam']}";
}
}
// use exec() because no results are returned
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" /></meta>
<head>
<title>Sollicitatie Formulier</title>
<link href="styletest.css" rel="stylesheet">
</head>
<body>
<div class="form">
<div class="tab-content">
<h1>Sollicitatie Formulier</h1>
<form method="post" enctype="multipart/form-data" >
<div class="top-row">
<div class="field-wrap">
<div class="field-wrap">
<input type="text" name="zoek" value="">
<input type="submit" name="submit" value="zoek">
</div> <!-- /field-wrap-->
</form>
</div><!-- /tab-content-->
</div> <!-- /form -->
</body>
</html>
答案 0 :(得分:3)
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
if(isset($_POST['submit'])):
try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, voornaam FROM sollicitatie_form WHERE id= :voornaam and city = :city");
// $stmt->bindParam(':id', $id, PDO::PARAM_STR);
//$stmt->bindParam(':voornaam', $voornaam, PDO::PARAM_STR);
$stmt->bindParam(':voornaam',$_POST['zoek'], PDO::PARAM_STR);
$stmt->bindParam(':city',$_POST['city'], PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row){
echo "{$row['voornaam']}";
}
}
// use exec() because no results are returned
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
endif;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" /></meta>
<head>
<title>Sollicitatie Formulier</title>
<link href="styletest.css" rel="stylesheet">
</head>
<body>
<div class="form">
<div class="tab-content">
<h1>Sollicitatie Formulier</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" >
<div class="top-row">
<div class="field-wrap">
<div class="field-wrap">
<input type="text" name="zoek" value="">
<input type="text" name="city" value="">
<input type="submit" name="submit" value="zoek">
</div> <!-- /field-wrap-->
</div>
</div>
</form>
</div><!-- /tab-content-->
</div> <!-- /form -->
</body>
</html>
如果要获取数据,则必须使用SELECT语句而不是INSERT语句。请检查这个答案,希望它能帮到你。
修改强>
请再试一次,检查是否显示错误。
最终编辑
现在检查它是否正常运行,我只是在我的本地服务器上运行它。
更新了2个参数
答案 1 :(得分:2)
首先需要prepare
语句,使用select
(不是插入),在查询中使用占位符,以及execute
准备好的对象。类似的东西:
$stmt = $conn->prepare("select id, voornaam FROM sollicitatie_form WHERE id= :voornaam";
// $stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->bindParam(':voornaam', $voornaam, PDO::PARAM_STR);
$stmt->execute();
应该这样做。我经常使用的另一种语法是:
$stmt = $conn->prepare("select id, voornaam FROM sollicitatie_form WHERE id= ?";
$stmt->execute(array($voornaam)); //or array(':voornaam' =>$voornaam) if you prefer named placeholders
也不是echo
用于输出。您可以将变量连接或放入双引号,然后您的PHP字符串将具有值。
另一个例子:
echo "{$row['voornaam']}"
可以是:
echo $row['voornaam'];