Hello there this is my 2nd post for today dealing with my PHP oop functions. I have created (with help of people from stackoverflow) a simple connection to the database using the function, now i need to create two simple functions of 'Insert' and 'Delete'. I know this will look like i am asking you to do the work for me and do not expect that the answer will fall down on me from one of you, but i as for some assistance on where to go and what to do as i have just touched the oop and the functions look like hell to me, actually i have just started the PHP overall. I will present the way i think the functions should be lay out, but i DO NOT know what to put there, if any of you can show me at least one of them i then might have an idea of where to go next. Thank you.
My file so far ( with comments):
<?php
require_once(dirname(__FILE__) . 'cfg.php');
class Database {
private $dbConn; //stores the database connection
public function __construct($dbConn)
{
global $cfg;
mysqli_connect($cfg['db']['host'], $cfg['db']['user'], $cfg['db']['pass'])
or die('Could not connect to MySQL server.');
mysqli_select_db($dbConn, $cfg['db']['db'])
or die('Unable to select database: ');
}
public function insert($parameters)
{
//construct INSERT INTO (...) VALUES
// construct the inserted record(s) (...)
//run the query
//$result = get the number of rows affected
return $result;
}
}
If any of you can guide me to show what should go inside the insert function so it would work i then can carry on doing my next statements like 'Delete' and 'Select'/ Thank you in advance and i hope you can help me in some way.
EDIT: work so far :
require_once(dirname(__FILE__) . '\cfg.php');
class Database {
private $dbConn;
public function __construct()
{
global $cfg;
$this->dbConn = new mysqli($cfg['db']['host'], $cfg['db']['user'], $cfg['db']['pass'], $cfg['db']['db']);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
public function select($parameters){
$fields = implode(',',$parameters['fields']);
//divides the fields array in to what is included in there(name and surname colums in this table)
$table = $parameters['table'];
//Takes the table
$sql_query = $this->dbConn->query("
SELECT $fields FROM $table WHERE id <> 0
");
//takes the query from mysqli (contains a lot of functions)
$sql_result = mysqli_fetch_assoc($sql_query);
return $sql_result;
}
public function insert($parameters)
{
$fields = implode(',',$parameters['fields']);
$values = implode(',',$parameters['$values']);
//divides the fields array in to what is included in there(name and surname colums in this table)
$table = $parameters['table'];
//Takes the table
$sql_query = $this->dbConn->query("
INSERT INTO $table ($fields) VALUES ('$values')
");
//construct INSERT INTO (...) VALUES // construct the inserted rerd(s) (...), //run the query
$result = $this->dbConn->affected_rows;
//$result = get the number of rows affecte
return $result;
//DOES NOT ADD VALUES TO THE TABLE ANYMORE MiSITAKE !!!!!!!!!!!!!!!!!!!!!!!
//PROBABLY IN THE $values = implode(',',$parameters['$values']); !~~~!!!!!!!!!!!!!!!!!!11
}
EDIT: I have done some mistakes in the Insert function. It does not save the parameters in to database from the form i created. i think that the problem is in $values = implode(',',$parameters['$values']); Will work on that more. if anyone got any ideas would be more than helpfull.