无法使用PDO/SQL更新数据库,使字段为空
我的问题是,当我尝试更新DB表hold时,它会使所有字段都为空。 我使用了三个配置文件来连接。这是config.php文件无法使用PDO/SQL更新数据库,使字段为空,sql,database,pdo,Sql,Database,Pdo,我的问题是,当我尝试更新DB表hold时,它会使所有字段都为空。 我使用了三个配置文件来连接。这是config.php文件 <?php $hostname='localhost'; $username='root'; $password='kv5772'; try { $db = new PDO("mysql:host=$hostname;dbname=gade",$username,$password); $db->setAttribute(PDO::ATTR_
<?php
$hostname='localhost';
$username='root';
$password='kv5772';
try {
$db = new PDO("mysql:host=$hostname;dbname=gade",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
echo 'Connected to Database<br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
然后是一个打印页面print.php。带有删除按钮。它起作用了。但是编辑没有
<?php include "config.php" ?>
<?php
$sql = "SELECT * FROM hold";?>
<table>
<tr>
<td>hold</td>
<td>leder</td>
<td>telefon</td>
<td>mail</td>
</tr>
<?php
foreach($db->query($sql) as $row){
?>
<tr>
<td><input name="hold" type="text" placeholder="<?php echo "{$row['hold']}";?>"/></td>
<td><input name="leder" type="text" placeholder="<?php echo "{$row['leder']}";?>"/></td>
<td><input name="telefon" type="text" placeholder="<?php echo "{$row['telefon']}";?>"/></td>
<td><input name="email" type="text" placeholder="<?php echo "{$row['email']}";?>"/></td>
<td>
<form action="formupdatecode.php" method="post">
<input type="hidden" name="action" value="edit" />
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
<input type="submit" value="edit" />
</form>
</td>
<td>
<form action="slet.php" method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
<input type="submit" value="delete" />
</form>
</td>
</tr>
</table>
<?php
}
$db = null;
?>
编辑按钮连接到formupdatecode.php
<?php include "config.php" ?>
<?php
$sql = "UPDATE hold SET hold = :hold,
leder = :leder,
telefon = :telefon,
email = :email
WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->bindParam(':hold', $_POST['hold'], PDO::PARAM_STR);
$stmt->bindParam(':leder', $_POST['leder'], PDO::PARAM_STR);
$stmt->bindParam(':telefon', $_POST['telefon'], PDO::PARAM_STR);
$stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
if($stmt->execute()){
echo "Successfully redigeret hold";
}// End of if profile is ok
else{
print_r($stmt->errorInfo()); // if any error is there it will be posted
$msg=" Database problem, please contact site admin ";
}
?>
HTML代码的输入标记位置有问题
您的编辑/输入字段位于表单外部,不会包含在回服务器的帖子中。您的输入字段不应该位于表单内部吗?哦,我的天哪,我很笨,我添加了一个简短的答案,这样问题就不会一直没有答案。