2015-04-06 72 views
1

我想在脚本中包含页面以更改用户个人资料的详细信息。我就是这么做的,在班上user.php的我这包括:编辑个人资料 - PDO

// Update profile 
    public function update($email,$gender,$location) { 
     try { 
     $stmt = $this->_db->prepare('UPDATE members SET email = ?, gender = ?, location = ? WHERE memberID = ? '); 
     $stmt->execute(array($email,$gender,$location,$_SESSION['memberID'])); 
     return $stmt->fetch(); 
     } catch(PDOException $e) { 
      echo '<p class="bg-danger">'.$e->getMessage().'</p>'; 
     } 
    } 

虽然,例如,页面account.php我就是这么做的:

if (isset($_POST['submit'])) { 
// new data 
$email = $_POST['email']; 
$gender = $_POST['gender']; 
$location = $_POST['location']; 
    $id = $_SESSION['memberID']; 

// query 
if ($user->update($email,$gender,$location,$id)); { 
    redirect('account.php'); 
} 
} 

而且,

<form action="account.php" method="POST"> 
Email<br> 
<input type="text" name="email" value="<?php echo $_SESSION['email'] ?>" /><br> 
Gender<br> 
<input type="text" name="gender" value="<?php echo $_SESSION['gender'] ?>" /><br> 
Location<br> 
<input type="text" name="location" value="<?php echo $_SESSION['location'] ?>" /><br> 
<input type="submit" name="submit" value="Save" /> 
</form> 

使用PDO中的连接了解它的方式,但是,我尝试了很多选项,但总是效果不佳。

+0

欢迎来到Stack Overflow!这个问题在信息上有点短暂。你的问题是什么? –

+0

你好,我寻求帮助。我写的代码工作不正常,它不更新数据。那它呢?你可以帮帮我吗? – Marco

+0

它的更新语句,' - > fetch()'根本没有意义,使用' - > rowCount()> 0'而不是 – Ghost

回答

0

在你的班级里的方法: 公共功能更新($ email,$ gender,$ location);

它不接受$ id参数。

所以解决方案可以是: a。使用对象的id,而不是使用$ _SESSION ['memberID']。

public function update($email,$gender,$location) { 
    try { 
    $stmt = $this->_db->prepare('UPDATE members SET email = ?, gender = ?, location = ? WHERE memberID = ?'); 
    $stmt->execute(array($email,$gender,$location,$this->id); 
    return true; 
    } catch(PDOException $e) { 
     echo '<p class="bg-danger">'.$e->getMessage().'</p>'; 
    } 
    return false; 
} 

b。接收函数中的id并使用它。如果是这种情况,最好使用静态方法。

public static function update($email,$gender,$location,$id) { 
    try { 
    $stmt = $this->_db->prepare('UPDATE members SET email = ?, gender = ?, location = ? WHERE memberID = ?'); 
    $stmt->execute(array($email,$gender,$location,$id); 
    return true; 
    } catch(PDOException $e) { 
     echo '<p class="bg-danger">'.$e->getMessage().'</p>'; 
    } 
    return false; 
} 

所以称之为,取决于所使用的策略。也不要在类模型的方法中执行echo,只需指定错误消息属性并让调用者执行输出。

希望它有帮助。

相关问题