Jump to content

OOP Programming...Insert ?


hola

Recommended Posts

I have a function that inserts into table tru Post method of form.

 

But it is not inserting into database.

 

Under Function I have foreach loop and insert statement.

 

Function fun($fields, $values, $table){

foreach ($fields...){...}

foreach($values..){...}

mysql_query("INSERT INTO $table($fields)VALUES($values)") or die('Fail Inserting...');

}

 

For coding side...

$connect->fun($fields,$values,"table");

 

I tried Debugging the value passed is correct...but its not inserting into DB;

 

Array ( [ 0 ] => test [1] => 1 ) Error: Fail Inserting...

 

Why is it not inserting?

 

Link to comment
https://forums.phpfreaks.com/topic/80722-oop-programminginsert/
Share on other sites

Here is the code:

Class test{

function insert($fields, $values, $table) {

foreach ($fields as $field) {

$queryfields += "`". $field .",";

}

 

foreach ($values as $value) {

$queryvalues += "'". $value ."'";

}

 

mysql_query("INSERT INTO $table($queryfields)VALUES($queryvalues)") or die('Inserting failed...');

}

 

}

----------------------------------------------------------------------------------------------------

if(isset($_POST['submit'])){

$name = $_POST['name'];

$age = $_POST['age'];

 

$fields = array("name", "age");

$values = array($name, $age);

$test->insert($fields,$values,"table_test");

}

Link to comment
https://forums.phpfreaks.com/topic/80722-oop-programminginsert/#findComment-409481
Share on other sites

Instead of using foreach to join the values within an array together you could just use implode instead:

<?php

Class test
{
    function insert($fields, $values, $table)
    {
        $qryFields = implode("','", $fields);
        $qryValues = implode("','", $values);

        $qry = "INSERT INTO $table('$qryFields') VALUES ('$qryValues')";

        mysql_query($qry) or die('QUERY ERROR: Inserting failed...<br /><code>'.$qry.'</code><br />' . mysql_error());
   }

}


if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $age  = $_POST['age'];

    $fields = array('name', 'age');
    $values = array($name, $age);

    $test = new test;
    $test->insert($fields, $values, 'table_test');
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/80722-oop-programminginsert/#findComment-409626
Share on other sites

its still givin me error;

 

I tried debugging ...everything appears correctly but still givin me the error;  ???

 

Array ( [ 0 ] => bob [1] => 19 ) INSERT INTO test('name','age') VALUES ('bob','19') QUERY ERROR: Inserting failed...

 

Is it because of the "age" filed being numeric and its passing string? or is it something else?

 

 

Link to comment
https://forums.phpfreaks.com/topic/80722-oop-programminginsert/#findComment-409685
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.