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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.