我创建了一个可以编辑用户信息的页面。我只是从一个名为'users'的db表中执行一个预准备语句,在user 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>
这是我准备好的陈述和表格......
<?php
$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()) { ?>
<form action="" method="post">
<div class="field">
<label for="firstname">First Name</label>
<input type="text" name="firstname" class="inputbar" value="<?php echo htmlentities($firstname); ?>" required>
</div>
<div class="field">
<label for="lastname">Last Name</label>
<input type="text" name="lastname" class="inputbar" value="<?php echo htmlentities($lastname); ?>" required>
</div>
<div class="field">
<label for="email">Email</label>
<input type="email" class="inputbaremail" name="email" value="<?php echo htmlentities($email); ?>" required>
</div>
<div class="field">
<label for="username">Username</label>
<input type="text" class="inputbar" name="username" value="<?php echo htmlentities($username); ?>" required>
</div>
<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>
<!--<input type="text" class="inputbar" name="group" value="<?php //echo htmlentities($group); ?>" required>-->
</select>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<label for="signinButton">
<input type="submit" id="signinButton" value="Update">
</label>
</form>
<?php } else { ?>
User <?php echo htmlentities($_GET['id']); ?> not found.
<?
} ?>
有没有人看到任何阻止这种情况发生的事情?
更新:
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!');
//}
//if(!$this->_db->update('users', $id, $fields)) {
// throw new Exception('There was a problem updating!')->getMessage();
//}
//}
添加了插入预备声明的新代码。仍然没有发送
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";