我只是想编辑数据库中的一些特定列,它是我在PHP OPP中的第一次尝试之一,也是我在PHP中的第一次,所以请耐心等待
public function prepareEdit() {
$id = $_GET ['id'] ;
$con = new PDO (DB_HOST, DB_USER, DB_PASS);
$sql = "SELECT * FROM user WHERE id = $id";
$stmt = $con->prepare($sql);
$stmt->execute();
echo "<form method='post' action=''>";
while ($row = $stmt->fetch()){
echo "<input type='text' name='name' value=".$row['name'].">";
echo "<input type='text' name='email' value=".$row['email'].">";
echo "<input type='text' name='message' value=".$row['message'].">";
echo "<input type='text' name='phone' value=".$row['phone'].">";
echo "<input type='text' name='reason'>";
}
echo "<input type='submit' value='Odoslat' name='send'>";
}
public function edit(){
$correct = false;
try {
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "UPDATE user(name, email, phone, message) VALUES(:name, :email, :phone, :message)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "name", $this->name, PDO::PARAM_STR );
$stmt->bindValue( "email", $this->email, PDO::PARAM_STR );
$stmt->bindValue( "phone", $this->phone, PDO::PARAM_STR );
$stmt->bindValue( "message", $this->message, PDO::PARAM_STR );
$stmt->execute();
return "Formular upraveny";
}catch( PDOException $e ) {
return $e->getMessage();
}
}
这是我的edit.php文件
<?php
include_once("config.php");
if( !(isset( $_GET['id'] ) ) ) {
$WebsiteUsers = new WebsiteUsers();
$WebsiteUsers->renderAll();
}
else {
$WebsiteUsers = new WebsiteUsers();
$WebsiteUsers->prepareEdit();
if( !(isset( $_POST['send'] ) ) ) {
$WebsiteUsers = new WebsiteUsers();
$WebsiteUsers->storeFormValues( $_POST );
}
}
?>
我的数据形式没问题,我按下SUBMIT按钮发送它们,数据库中没有数据被更改,有人可以帮我解决吗?
答案 0 :(得分:0)
您的SQL不正确:
UPDATE user SET name = :name, email = :email, phone = :phone, message = :message WHERE user_id = 123
不要忘记修改上述SQL中的WHERE条件......