参见英文答案 > When to use single quotes, double quotes, and back ticks in MySQL 12个
我有一个代码,与MysqL工作正常,但已切换到MysqLi它不再运行.数据在选择按钮的表格中正确显示,它看起来像我得到保存gif(因为正在调用Ajax),但是更新语句不会更新数据库.
似乎没有工作的部分是saveedit.PHP:
<?PHP
require_once("dbcontroller.PHP");
$db_handle = new DBController();
$column=$_POST['column'];
$value=$_POST['value'];
$id=$_POST['id'];
$sql = "UPDATE PHP_interview_questions SET `$column` = '$value' WHERE id=$id)";
$result = MysqLi_query ($conn, $sql) or die(MysqLi_error ($dbc));
?>
主页是
<?PHP
require_once("dbcontroller.PHP");
$db_handle = new DBController();
$sql = "SELECT * from PHP_interview_questions";
$faq = $db_handle->runQuery($sql);
?>
<html>
<head>
<title>PHP MysqL Inline Editing using jQuery Ajax</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function showEdit(editableObj) {
$(editableObj).css("background","#FFF");
}
function savetoDatabase(editableObj,column,id) {
$(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
$.ajax({
url: "saveedit.PHP",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data){
$(editableObj).css("background","#FDFDFD");
}
});
}
</script>
</head>
<body>
<table class="tbl-qa">
<thead>
<tr>
<th class="table-header" width="10%">Q.No.</th>
<th class="table-header">Question</th>
<th class="table-header">Answer</th>
</tr>
</thead>
<tbody>
<?PHP
foreach($faq as $k=>$v) {
?>
<tr class="table-row">
<td><?PHP echo $k+1; ?></td>
<td contenteditable="true" onBlur="savetoDatabase(this,'question','<?PHP echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?PHP echo $faq[$k]["question"]; ?></td>
<td contenteditable="true" onBlur="savetoDatabase(this,'answer','<?PHP echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?PHP echo $faq[$k]["answer"]; ?></td>
</tr>
<?PHP
}
?>
</tbody>
</table>
</body>
</html>
使用db_controller.PHP连接到数据库并处理结果集创建(数据库连接详细信息位于此处未发布的文件中):
function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = MysqLi_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
MysqLi_select_db($conn, $this->database);
}
function runQuery($query) {
$conn = MysqLi_connect($this->host,$this->user,$this->password);
MysqLi_select_db($conn, $this->database);
$result = MysqLi_query($conn, $query)or die(MysqLi_error($conn));
while($row=MysqLi_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = MysqLi_query($conn, $query);
$rowcount = MysqLi_num_rows($result);
return $rowcount;
}
}
?>
我现在已经将dbcontroller更改为以下内容以简化并尝试抛出错误但仍然没有得到任何结果
<?PHP
class DBController {
private $host = "***********";
private $user = "***********";
private $password = "**********";
private $database = "************";
function __construct() {
$conn = MysqLi_connect($this->host,$this->user,$this->password,$this->database) OR die (MysqLi_connect_error());
}
function runQuery($query) {
$conn = MysqLi_connect($this->host,$this->user,$this->password);
MysqLi_select_db($conn, $this->database);
$result = MysqLi_query($conn, $query)or die(MysqLi_error($conn));
while($row=MysqLi_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = MysqLi_query($conn, $query);
$rowcount = MysqLi_num_rows($result);
return $rowcount;
}
}
?
>
解决方法:
$sql = "UPDATE PHP_interview_questions SET $column = '$value' WHERE id=$id)";
或者,如果列中有特殊字符,请使用反引号来逃避它们.
$sql = "UPDATE PHP_interview_questions SET `$column` = '$value' WHERE id=$id)";
如果id是一个数值,你也不需要单个qoutes.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。