在PDO中更新数据用户不起作用 [英] Update Data Users in PDO Not Work
问题描述
以前我是使用旧MySQL进行更新的,但是现在我尝试根据朋友的推荐将总数更改为PDO.
Previously I was using Old MySQL to update, but now I try to change the total to PDO on the recommendation of friends.
但是,在设置表单以更新有关用户的信息时我受到了限制.
However I am constrained when setting up the form to update information about users.
我使用以下代码:
public function runQuery($sql)
{
$aboutMe = $this->conn->prepare($sql);
return $aboutMe;
}
class Database
{
private $host = "localhost";
private $db_name = "...";
private $username = "...";
private $password = "...";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
if(isset($_POST['update']))
{
$utentang = strip_tags($_POST['txt_tentang']);
try
{
$aboutMe = $auth_user->runQuery("UPDATE users SET tentang=:ttg where id=:id");
$aboutMe->execute(array(':ttg'=>$utentang, ':id'=>$id));
$aboutMe->bindValue(':ttg', $utentang);
$tentangSaya=$aboutMe->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
};
HTML
<form method="post">
<div class="center">
<h2>Your About</h2>
<p><textarea name="txt_tentang" id="tentang"></textarea></p>
<input id="button" type="submit" value="Simpan" name="update"/>
</div>
</form>
上面的代码无法运行. 怎么了? PHP版本5.5
The above code does not run. What is wrong? PHP Version 5.5
推荐答案
怎么了?
坦率地说-几乎所有东西.从类结构到获取 update 查询结果的想法.
Frankly - nearly everything. From the class structure to the idea of fetching result of the update query.
似乎您正在使用一些非常不可靠的教程来学习PDO.让我提供一个我写的(唯一合适的)PDO教程,您可以从中轻松学习正确的方法.
It seems you are using some highly unreliable tutorial to learn PDO. Let me offer one I wrote, (The only proper) PDO tutorial, from which you will easy learn the proper way.
这是您的更新代码已固定:
Here is your update code fixed:
if(isset($_POST['update']))
{
$utentang = strip_tags($_POST['txt_tentang']);
$stmt = $auth_user->runQuery("UPDATE users SET tentang=:ttg where id=:id");
$stmt->execute(array(':ttg'=>$utentang, ':id'=>$id));
}
请注意,您不应在此处绑定,获取或捕获.
Note that you should neither bind, fetch or catch here.
这篇关于在PDO中更新数据用户不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!