基本上,由于某种原因,我的 INSERT 查询的 SQL 处理了两次,我不知道为什么。
这是我的表格:
<form class="form-horizontal" method="POST" action="">
<input id="email" name="email" class="text-style" type="email">
<input id="password" name="password" class="text-style" type="password">
<input id="secret" name="secret" class="text-style" type="text">
<select class="text-style" id="console" name="console">
<option value="XBOX">Xbox 360</option>
<option value="PS3">PS3</option>
</select>
<select class="text-style" id="status" name="status">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
<button class="btn btn-success pull-right" name="add_account">Add Account</button>
</form>
这是我处理表单的PHP:
if(isset($_POST['add_account'])) {
$data = array(
'email' => base64_encode($_POST['email']),
'password' => base64_encode($_POST['password']),
'secret' => base64_encode($_POST['secret']),
'console' => $_POST['console'],
'status' => $_POST['status'],
'date_added' => date("y-m-d h:i:s"),
'last_check' => date("y-m-d h:i:s")
);
$result = $DB->insert('accounts',$data);
if($result) {
if(!in_array("Account added successfully.", $msgs))
array_push($msgs,"Account added successfully.");
} else {
array_push($msgs,"^Failed to add account.");
}
}
然后是$DB->insert
功能
public function insert($table=null,$array_of_values=array()) {
if ($table===null || empty($array_of_values) || !is_array($array_of_values)) return false;
$fields=array(); $values=array();
foreach ($array_of_values as $id => $value) {
$fields[]=$id;
if (is_array($value) && !empty($value[0])) $values[]=$value[0];
else $values[]="'".mysql_real_escape_string($value,$this->connection)."'";
}
$s = "INSERT INTO $table (".implode(',',$fields).') VALUES ('.implode(',',$values).')';
if ($this->runSQL($s)) return true;
return false;
}
这是runSQL
功能:
public function runSQL($sqlQuery,$possibleError = 0) {
if($sqlQuery=="") {return false;}
mysql_query($sqlQuery);
if(mysql_error()){
return false;
}
return true;
}