0
我的問題是,當我嘗試更新我的數據庫表(保留)它使所有字段爲空。 即時通訊使用三個文件的配置來連接。這是config.php文件。不能更新DB與PDO/SQL它使字段爲空
<?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 ";
}
?>
你的輸入字段不應該在表單內嗎? – 2014-09-28 07:55:17
oh my god im stupid thx – 2014-09-28 08:02:50
添加了一個簡短的答案,以便問題不會未答覆。 – 2014-09-28 08:20:46