Php 尝试更新数据库时引发异常错误

Php 尝试更新数据库时引发异常错误,php,mysql,exception,prepared-statement,Php,Mysql,Exception,Prepared Statement,我创建了一个可以编辑用户信息的页面。我只对一个名为“users”的db表执行一个prepared语句,在users db表中有一个名为group的列,我可以在其中为用户分配不同的权限级别。我正在将所选用户的当前数据输出到此页面上,因此如果用户的组级别为1,则在选项字段中将显示1。然后我有4个其他选项,我列出了2、3、4和5处的其他值。每当我尝试提交表单时,我的用户类就会出现异常错误“更新时出现问题!”。下面有更新函数,其中异常错误为 public function update($fields

我创建了一个可以编辑用户信息的页面。我只对一个名为“users”的db表执行一个prepared语句,在users db表中有一个名为group的列,我可以在其中为用户分配不同的权限级别。我正在将所选用户的当前数据输出到此页面上,因此如果用户的组级别为1,则在选项字段中将显示1。然后我有4个其他选项,我列出了2、3、4和5处的其他值。每当我尝试提交表单时,我的用户类就会出现异常错误“更新时出现问题!”。下面有更新函数,其中异常错误为

public function update($fields = array(), $id = null) {

    if(!$id && $this->isLoggedIn()) {
        $id = $this->data()->id;
    }

    if(!$this->_db->update('users', $id, $fields)) {
        throw new Exception('There was a problem updating!');
    }
}
我相信我准备好的声明和表格是合理的,而不是可能的集团选择领域。我希望当前组号显示在选项框中,如果我不需要更新它,我希望能够单击“提交”,并且没有任何问题,我希望能够更新组整数

这是我不确定它是否正确或使我能够这样做的部分

<div class="field">
    <label for="group">Group</label>
    <select name="group" required>
        <option value=''><?php echo htmlentities($group); ?></option>
        <option value="1">Bench</option>
        <option value="2">Spectator</option>
        <option value="3">Team Member</option>
        <option value="4">Commissioner</option>
        <option value="5">Creator</option>
    </select>
</div>  
添加了插入准备语句的新代码。仍然没有发送

    if(isset($_POST['submit'])){
    $firstname = Input::get('firstname');
    $lastname = Input::get('lastname');
    $email = Input::get('email');
    $username = Input::get('username');
    $group = 1;




$con = mysqli_connect("localhost","root","","db");
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
        $stmt2 = $con->prepare("INSERT INTO users (firstname, lastname, email, username, `group`) VALUES (?, ?, ?, ?, ?");
        if ( false===$stmt2 ) {
        // Check Errors for prepare
            die('User Request prepare() failed: ' . htmlspecialchars($con->error));
        }
        $stmt2->bind_param('sssssi', $firstname, $lastname, $email, $username, $group);
        if ( false===$stmt2 ) {
        // Check errors for binding parameters
            die('User Request bind_param() failed: ' . htmlspecialchars($stmt2->error));
        }
        $stmt2->execute();
        if ( false===$stmt2 ) {
            die('User Request execute() failed: ' . htmlspecialchars($stmt2->error));
        }
    }



//Prepared statement that gets user info    
$con = mysqli_connect("localhost","root","","db");
/* check connection */
   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("SELECT firstname, lastname, email, username, `group` FROM users WHERE id = ?");
    if ( false===$stmt ) {
  // Check Errors for prepare
  die('prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param("i", $_GET['id']);
    if ( false===$stmt ) {
      // Check errors for binding parameters
      die('bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
$stmt->execute();
    if ( false===$stmt ) {
      die('execute() failed: ' . htmlspecialchars($stmt->error));
    }
    //Check errors for execute
//if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}
$stmt->bind_result($firstname, $lastname, $email, $username, $group);
$stmt->store_result();

if ($stmt->fetch()) { ?>
正在尝试使用try/catch

public function update($fields = array(), $id = null) {

    if(!$id && $this->isLoggedIn()) {
        $id = $this->data()->id;
    }
    //echo $this->_db->update('users', $id, $fields);

    //if(!$this->_db->update('users', $id, $fields)) {
        //throw new Exception('There was a problem updating!');
    //}
    try {
        if(!$this->_db->update('users', $id, $fields)) {
            throw new Exception('There was a problem updating!');
        }
    }
       catch(Exception $e) {
    echo "An error occurred";
    throw $e;
        }
        finally {
                    //This overrides the exception as if it were never thrown
            return "\nException erased";
            }
            try {
            echo asdf();
        }
        catch(Exception $e) {
            echo "\nResult: " . $e->getMessage();
    }
我得到了这个错误

An error occurred
Warning: Cannot modify header information - headers already sent by (output started at /home4/db/public_html/example.com/classes/User.php:46) in /home4/db/public_html/example.com/classes/Redirect.php on line 14
我的用户类的第45行是

        echo "An error occurred";

试着在if语句
$this->\u db->update('users',$id,$fields)
之外执行这一行,看看会发生什么错误。我从代码中得到这个语法错误<代码>意外的'->'(代码应该在函数内部。
公共函数更新($fields=array(),$id=null){if(!$id&&$this->isLoggedIn()){$id=$this->data()->id;}echo$this->\u db->update('users',$id,$fields);}
它通过了,但我的数据库中没有任何变化。它不会给我一个错误。我会将代码添加到我的任务中,就像在我的用户类中一样。哪里有
update
SQL/db交互?尝试在if语句
$this->\u db->update('users',$id$fields)之外执行这一行
并查看您得到了什么错误我得到了该代码的语法错误…
意外'->'(
代码应该在您的函数中。
公共函数更新($fields=array(),$id=null){if(!$id&&$this->isLoggedIn()){$id=$this->data()->id;}echo$this->\u db->update($users',$id,$fields)}
它通过了,但我的数据库中没有任何更改。它不会给我一个错误。我会将代码添加到我的任务中,就像在我的用户类中一样。更新的SQL/db交互在哪里?
An error occurred
Warning: Cannot modify header information - headers already sent by (output started at /home4/db/public_html/example.com/classes/User.php:46) in /home4/db/public_html/example.com/classes/Redirect.php on line 14
        echo "An error occurred";