我正在使用一个api,它处理来自客户端的请求,然后从服务器(使用codeigniter 3开发)中获取响应,并将其转发回客户端。
但是,如果发生任何数据库错误,例如重复的id或null值,则模型类将无法处理该错误以显示正确的错误消息。我已经尝试了try catch块,但尚未成功。这是模型:
public function add() {
try {
$this->db->trans_start(FALSE);
$this->db->insert('users', $preparedData);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
throw new Exception("Database error:");
return false;
}
return TRUE;
} catch (Exception $e) {
log_message('error: ',$e->getMessage());
return;
}
}
Run Code Online (Sandbox Code Playgroud)
值得一提的是,我已将db_debug设置为FALSE。
任何帮助,将不胜感激。
在我的项目中,我想将div内容存储到DB.内容如下:
<span>name:</span><input type="text" id="inputid" value="John"><br />
Run Code Online (Sandbox Code Playgroud)
所以我选择了innerHTML来成功获取它们.使用Ajax,内容将存储到DB.
昨天,我发现应该调用stripslashes()来格式化内容,并且可以成功更新.
但今天striplashes()没有做任何事情.这是我的js代码:
var slcId = $('#slcStNum').val();
var tmTgDvHtml=document.getElementById("timeTagDiv").innerHTML;
$.ajax({
dataType:'html',
type:"POST",
url:"get_ajax_csc_div.php",
data:{htmlCnt:tmTgDvHtml,slcId:slcId},
success:function (data)
{
alert("test");
}
});
Run Code Online (Sandbox Code Playgroud)
这是html代码:
<div id="timeTagDiv"><span>name:</span><input type="text" id="inputid" value="John"><br /></div>
Run Code Online (Sandbox Code Playgroud)
这是get_ajax_csc_div.php代码
<?php
if(isset($_POST['htmlCnt']))
{
include("DB.php");
$htcnt=stripslashes(".$_POST['htmlCnt'].");
$sql="update IDC SET stprocess='$htcnt' where id='".$_POST['slcId']."';";
$sel = $conn->exec($sql);
}
?>
Run Code Online (Sandbox Code Playgroud)
我将dataType:'html'更改为dataType:'json',但它再次失败.谁能帮我?
我正在尝试使用 codeigniter 中的 mysql 检查表是否存在,以及该表中的记录是否可用。这是我尝试过的功能,但是 id 不起作用。
function isRowExist($table, $id)
{
if(mysqli_query("DESCRIBE {$table}")) {
$query = $this->db->query("SELECT * FROM {$table} WHERE id = '{$id}'");
}
return $query->num_rows();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。