为什么PDO不在wamp中工作?我认为我的php代码很好,但我无法从MySQL数据库中插入和获取数据,我该如何解决?
<?php
$db = new PDO('mysql:host=localhost;dbname=ajaxdata','root','');
$page = isset($_GET['p'])?$_GET['p']:'';
if($page=='add') {
$name = $_POST['nm'];
$email = $_POST['em'];
$phone = $_POST['pn'];
$address = $_POST['ad'];
$stmt = db->prepare("INSERT INTO info VALUES('',?,?,?,?)");
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$email);
$stmt->bindParam(3,$phone);
$stmt->bindParam(4,$address);
if($stmt->execute()){
echo "Success add data";
} else {
echo "Fail add data";
}
} else if($page=='edit'){
} else if($page=='del'){
} else{
$stmt = $db->prepare("SELECT * FROM info");
$stmt->execute();
while($row = $stmt->fetch()){
?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['phone'] ?></td>
<td><?php echo $row['address'] ?></td>
<td>
<button class="btn btn-warning">Edit</button>
</td>
</tr>
<?php
}
}
?>
我不知道如何从wamp中修复此错误,所以请告诉我该怎么做。
答案 0 :(得分:1)
您在调用db对象variable
时错过了美元符号:
$stmt = db->prepare("INSERT INTO info VALUES('',?,?,?,?)");
// ^
必须如下:
$stmt = $db->prepare("INSERT INTO info VALUES('',?,?,?,?)");