3

I have the following array which I would like to insert into mysql database.

  • Item[0] = Soccer
  • Item[1] = Rugby
  • Item[2] = Football
  • Item[3] = Netball
  • Item[4] = Hockey

I am using the following function to insert into the database, located in functions.php:

//Capture items 
    function item($register_data)
    {
        array_walk($register_data,'array_clean');

        $fields = ' '.implode(',',array_keys($register_data)).' ';
        $data = '\''.implode('\',\'',$register_data).'\'';

        //Insert user Data into the database
        $query = "INSERT INTO items ($fields) VALUES ($data)";
        mysql_query($query);
    }

Now this is how I insert:

for($i =0;$i<4;$i++)
                {
                    $item = array($i = item[i]);        
                }


//Call the function to insert into the database
item($item);

This method doesn't seem to work. Please assist

11
  • 2
    And what is the error? Commented Mar 14, 2013 at 12:47
  • 1
    1) Use mysql_error() to get hints to what went wrong. 2) Switch to mysqli or PDO as mysql_x functions are deprecated. Commented Mar 14, 2013 at 12:47
  • 1
    Doesn't work how? Also, mysql_* is deprecated... Check into PDO or MySQLi. Commented Mar 14, 2013 at 12:48
  • 2
    What does your database look like? That array looks like a list of set of records rather than columns, so why are you inserting them as fields. Also arraykeys (0, 1, 2) are terrible names for your fields. Commented Mar 14, 2013 at 12:49
  • 1
    echo query. And see if it a valid SQL. Commented Mar 14, 2013 at 12:51

2 Answers 2

1

PHP has a built in serialize & deserialize functions (http://php.net/manual/en/function.serialize.php) for that. You can pass any object or array to it, and it will return a serialized string, which you can store in a single field in the database.

Sign up to request clarification or add additional context in comments.

1 Comment

I dont really wanna serialize the input data. I would like to insert it individually. One after Another
1

Since your items table has two columns, one of which is id (auto increment) you're going to want to skip over it and only specify the second column ItemName. This is described in the mysql documentation on this page.

Lets say you want to insert 5 sports in this table:

$sports[0] = "Soccer";
$sports[1] = "Rugby";
$sports[2] = "Football";
$sports[3] = "Netball";
$sports[4] = "Hockey";

You'll want the query to look something along these lines:

INSERT INTO items (ItemName) VALUES ('Soccer'), ('Rugby'), ('Football'), ('Netball'), ('Hockey');

The code would look something like this:

function insert($insert_data)
{
    $fields = implode(', ', $insert_data);
    $data = '(\''.implode('\'), (\'', $insert_data).'\')';

    //mysql_query("INSERT INTO items (ItemName) VALUES $data;");
    echo "INSERT INTO items (ItemName) VALUES $data;";
}

$sports = array();
$sports[0] = "Soccer";
$sports[1] = "Rugby";
$sports[2] = "Football";
$sports[3] = "Netball";
$sports[4] = "Hockey";

insert($sports);

You may also want to sanitize the strings first and take a look at mysqli since mysql is pretty outdated.

5 Comments

Nice Example. Let me try this
What if I dont have a static number of fields for the $sports array ?
@user1783675 You mean if the sports array has more than 5 sports? The insert function is going to generate a query depending on how many sports it has, as long as the array is larger than 1 it should be fine.
yes @Edward-a. The number of sporting activities varies all the time. for user it can be 2, another 5 and another user 15.
@user1783675 As long as there is one or more sports in the array it should do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.