插入和删除方法

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
", 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.

This is to give you an idea of a universal function

<?php

 // @param array $dataArray[field] = value
 // @param string $tabla
 // @return string

function insert(array $dataArray, $tabla) {
  $insertStr = 'INSER INTRO ' . $tabla;
  $row  = 'VALUES' . $field = '(';
  foreach ($dataArray as $index => $value) {
    $field .= '`' . $index . '`,';
    $row   .= (is_string($value )) ? "'" . $value . "'," : $value . ',';
  }
  $field =  trim($field, ',') . ')';
  $row   =  trim($row, ',') . ')';
  $sql = $insertStr . $field . $row;
 return $sql;
}
  • Add an another parameter to your insert function : the name of the table.
  • And the second parameter ($parameters) should be, I think, a double array :

$parameters = array(
    array(
        "name" => "test",
        "column2" => "toto"
    ),
    array(
        "name" => "test 2",
        "column2" => "titi"
    )
    // ...
);
  • If $parameters is a single array: $parameters = array($parameters);.
  • Get the names of these columns ("name" and "column2" in this example).
  • With this, you can now construct your query:
    INSERT INTO $table ($columns) VALUES ($array1), ($array2), ...;.

Remember to escape your data.

  • Then (execute, and get the number of rows), you can use the functions MySQL.