I have code that works fine with mysql, but switching to mysqli doesn't work anymore. The data is displayed in the table using the selection button correctly, and it looks like it works when I get a saving gif (this is the way Ajax is called), but then the update instruction does not update the database.
The part that doesn't seem to work is 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));
?>
On the home page
<?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>
By connecting db_controller.php to the database and processing the creation of the result set (information about connecting to the database is in a file that is simply not listed here):
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;
}
}
?>
Now I changed dbcontroller to the following to simplify and try to throw errors, but not getting anything
<?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;
}
}
?
<P →