Created
December 5, 2014 15:51
-
-
Save dennisdegryse/17246e604947e52e44de to your computer and use it in GitHub Desktop.
ajax_table.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// [dennis] read suggestions on your class name in the ajax.php comments | |
class ajax_table { | |
public function __construct(){ | |
$this->dbconnect(); | |
} | |
private function dbconnect() { | |
$conn = mysqli_connect("localhost","root","","ajax_table") | |
or die ("<div style='color:red;'><h3>Could not connect to MySQL server</h3></div>"); | |
return $conn; | |
} | |
function getRecords(){ | |
// [dennis] reuse your dbconnect() function | |
$conn = $this->dbconnect(); | |
$this->res = mysqli_query($conn,"select * from info"); | |
if(mysqli_num_rows($this->res)){ | |
while($this->row = mysqli_fetch_assoc($this->res)){ | |
$record = array_map('stripslashes', $this->row); | |
$this->records[] = $record; | |
} | |
return $this->records; | |
} | |
//else echo "No records found"; | |
} | |
function save($data){ | |
// [dennis] reuse your dbconnect() function | |
$conn = $this->dbconnect(); | |
if(count($data)){ | |
$values = implode("','", array_values($data)); | |
mysqli_query($conn,"insert into info (".implode(",",array_keys($data)).") values ('".$values."')"); | |
// [dennis] don't build a query like a string, but define prepared statements. See: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php | |
if(mysqli_insert_id($conn)) return mysql_insert_id($conn); | |
return 0; | |
} | |
else return 0; | |
} | |
function delete_record($id){ | |
// [dennis] reuse your dbconnect() function | |
$conn = $this->dbconnect(); | |
if($id){ | |
mysqli_query($conn,"delete from info where id = $id limit 1"); | |
// [dennis] don't build a query like a string, but define prepared statements. See: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php | |
return mysqli_affected_rows($conn); | |
} | |
} | |
function update_record($data){ | |
// [dennis] reuse your dbconnect() function | |
$conn = $this->dbconnect(); | |
if(count($data)){ | |
$id = $data['rid']; | |
unset($data['rid']); | |
$values = implode("','", array_values($data)); | |
$str = ""; | |
foreach($data as $key=>$val){ | |
$str .= $key."='".$val."',"; | |
} | |
$str = substr($str,0,-1); | |
$sql = "update info set $str where id = $id limit 1"; | |
// [dennis] don't build a query like a string, but define prepared statements. See: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php | |
$res = mysqli_query($conn,$sql); | |
if(mysqli_affected_rows($conn)) | |
{ | |
return $id; | |
} | |
return 0; | |
} | |
else return 0; | |
} | |
function update_column($data){ | |
// [dennis] reuse your dbconnect() function | |
$conn = $this->dbconnect(); | |
// [dennis] the count function is hardly a validation for this case. you need array_key_exists() for each expected key | |
if(count($data)){ | |
$id = $data['rid']; | |
unset($data['rid']); | |
$sql = "update info set ".key($data)."='".$data[key($data)]."' where id = $id limit 1"; | |
// [dennis] don't build a query like a string, but define prepared statements. See: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php | |
$res = mysqli_query($conn,$sql); | |
if(mysqli_affected_rows($conn)) return $id; | |
return 0; | |
} | |
} | |
function error($act){ | |
return json_encode(array("success" => "0","action" => $act)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment