我不确定在向最终用户提供可读反馈时最好使用哪种方法。我已经阅读了一些论坛,但并没有真正变得更聪明(或者我还没有理解)
我想在插入/更新失败时、成功时以及提供自定义反馈时(例如检查项目是否已存在)提供反馈。
对于 INSERT、UPDATE、DELETE、DROP 等,查询返回 TRUE 或 FALSE。 因此,我的结果属性 $this->query_result 应该始终为 true 或 false。
我的问题:
- 动态地向用户显示反馈 表单提交后(提交给同一个 页)
- $this->query_result 为真 返回一个字符串
我添加了代码来查看我在做什么(做错了)
这些是我用来连接/查询数据库的函数:
public function connect()
{
if (!($this->conn = mysql_connect($this->host, $this->username, $this->pwd))) {
die("Error connecting to DB by user = " . $this->username);
}
$this->db = mysql_select_db($this->dbname,$this->conn)
or die("Unable to connect to database " . $this->dbname);
}
private function query($sql)
{
$this->query_result = mysql_query($sql, $this->conn)or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");
if (!$this->query_result){
die("database query failed.");
} else {
return $this->query_result;
}
}
这是我的问题:我正在提供有关数据访问层 (DAL) 的反馈,请参见例如这个:
public function addNewPerson($formData)
{
$sql = "INSERT INTO my_table(`name`, `email`, `www`)";
$sql .= " VALUES('".
$formData['name']."','".
$formData['email']."','".
$formData['www']."','");
$this->query($sql);
return $this->query_result;
}
通过返回一个文本字符串,返回结果永远为真。 根据我的阅读,我可能应该有一个处理错误/反馈的功能。
这就是我目前在我的模板中使用反馈所做的事情:
if (isset($_POST['form_submit']))
{
if (isset($_POST['person_submit'])) {
$formData = $sl->getFormData();
$result = $myDB->addNewPerson($formData);
if ($result == true)
{
echo '<script type="text/javascript" language="JavaScript">
jQuery("#contentArea .messageWindow1").show(500);
jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow1").hide(500); });
</script>';
} else {
echo '<script type="text/javascript" language="JavaScript">
jQuery("#contentArea .messageWindow2").show(500);
jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow2").hide(500); });
</script>';
}
}
}
<div id="contentArea">
<div class="messageWindow1"> <span class="msg"><?php echo $labelResult ?></span></div>
<div class="messageWindow2"> <span class="msg"><?php echo $labelResult ?></span></div>
</div>
最佳答案
我会使用 PHP5 的内置异常处理来捕获错误和可能的验证错误。例如:
class DatabaseException extends Exception {}
class ValidatorException extends Exception {}
public function connect()
{
if (!($this->conn = mysql_connect($this->host, $this->username, $this->pwd))) {
throw new DatabaseException("Error connecting to DB by user = " . $this->username);
}
if(!($this->db = mysql_select_db($this->dbname,$this->conn))) {
throw new DatabaseException("Unable to connect to database " . $this->dbname);
}
}
//....
public function addNewPerson($formData)
{
$sql = "INSERT INTO my_table(`name`, `email`, `www`)";
$sql .= " VALUES('".
$formData['name']."','".
$formData['email']."','".
$formData['www']."','");
//If less than 2 characters, do not insert data.
if (strlen($formData['name']) < 2)
throw new ValidatorException( "Person not saved. Name field was to short or empty.");
//If person already exists
if($this->isPersonInList($formData['name']))
throw new ValidatorException( "Person already exists!");
//Process query
$this->query($sql);
return $this->query_result;
}
在调用脚本中
try {
$formData = $sl->getFormData();
$result = $myDB->addNewPerson($formData);
} catch (DatabaseException $e) {
// display $e->getMessage()
} catch (ValidatorException $e) {
//display $e->getMessage()
}
您的脚本需要指出的其他一些事情。
- 最好使用PDO并准备好 声明。
- 您还可以使用以下方法来确定字符串长度是否为 遇见了。
$arr = 'Shoan'; var_dump(isset($arr[10])); //false var_dump(isset($arr[2])); //true
- 过滤sql的输入 之前的注入(inject)/XSS 攻击 将其插入您的数据库或 在您的应用程序中使用它。
关于php - 需要一些关于 PHP/mysql 错误处理的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/975452/