Php 在PDO中更新数据用户不工作
以前我使用旧的MySQL进行更新,但现在我尝试根据朋友的推荐将总数改为PDO 但是,我在设置表单以更新用户信息时受到限制 我使用以下代码:Php 在PDO中更新数据用户不工作,php,pdo,Php,Pdo,以前我使用旧的MySQL进行更新,但现在我尝试根据朋友的推荐将总数改为PDO 但是,我在设置表单以更新用户信息时受到限制 我使用以下代码: public function runQuery($sql) { $aboutMe = $this->conn->prepare($sql); return $aboutMe; } class Database { private $host = "localhost"; private $db_name
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
你的关于
上述代码不运行。
怎么了?
PHP版本5.5
怎么了
坦率地说,几乎所有的事情。从类结构到获取更新查询结果的思想
似乎您正在使用一些非常不可靠的教程来学习PDO。让我举一个我写的例子,从中你将很容易学会正确的方法
以下是已修复的更新代码:
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));
}
请注意,您不应在此处绑定、获取或捕获
$aboutMe->execute(数组('ttg'=>$id',id'=>$id))
,在代码看起来有点混乱之后无需绑定value(对我来说)runQuery方法属于$auth\u user
,但$this->conn
属于数据库???
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));
}