-2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2919681
  • 21
  • 1
  • 6
  • Have a look here: http://php.net/manual/de/mysqli.prepare.php under `Procedural style`. There you find waht you need to put in you `insert` function. – TiMESPLiNTER Nov 03 '13 at 14:54
  • 2
    First of all, you should read some books about OOP. The theory is very important, even it can take a lot of time, that you want to spend "coding". And as for your question - you need to create public function, that accept 2 parameters - 1) table - the table you are want to insert to, 2) key=>value array of the fields you are inserting/updating. Then loop through the array and create a string with valid sql query. Good luck. – Yura Sokolov Nov 03 '13 at 14:54
  • +1 to doing some reading and not just getting SO to build your app for you, piece by piece – sevenseacat Nov 03 '13 at 14:58
  • This is your second post. And you totally didn't take any advice from your [first post](http://stackoverflow.com/questions/19752805/is-my-cfg-php-connection-right/19753148#19753148) so why would we want to help you again? – PeeHaa Nov 03 '13 at 15:04
  • @PeeHaa Not true, i did take the advice and it helped a lot. I just want to go further as fast as possible as the time is due. and i am doing quite good with the help of people in here. So far the class contains insert and select which work perfectly and delete function which is on its way to working status. – user2919681 Nov 03 '13 at 17:34
  • The above has absolutely nothing to do with OOP. And you have done absolutely done nothing with the advice people gave you as can be seen by looking at your code. – PeeHaa Nov 03 '13 at 17:36
  • @PeeHaa i agree with you that is why i have changed that completely and done it differently. i can post it here if you would like to see it. – user2919681 Nov 03 '13 at 17:41

2 Answers2

0

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;
}
Chap2
  • 43
  • 1
  • 6
0
  • 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.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Aurelien Maigret
  • 402
  • 3
  • 12