Jump to content

Inserting Array Values into table columns.


phileplanet

Recommended Posts

I basically want each value in the array to go into its own column. here's what I have so far:

[code]$columns = array();
$values = array();
foreach($formfields as $field) {
    $column = $field[2];
    $value = $_POST["$column"];
    array_push($columns, $column);
    array_push($values, $value);
}
$q = mysql_query("INSERT INTO $table($columns) VALUES($values)") or die (mysql_error());?>[/code]
When I try to INSERT, it gives me "Unknown column 'Array' in 'field list'". Any solutions?
Link to comment
Share on other sites

I was going to show it that way. It's up to you what you want to do. It's best to put a space after the table name.

Instead of asking if it's right, I invite everyone just to always try it before posting. That's one way we all learn.


Good luck.
Link to comment
Share on other sites

Yes, good. Well done. I recommend that you use one of these on each value before being inserted/updated in the table:

mysql_real_escape_string(), mysql_real_escape_string(), or addslashes()

[a href=\"http://us2.php.net/manual/en/function.mysql-escape-string.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.mysq...cape-string.php[/a]

[a href=\"http://us2.php.net/manual/en/function.addslashes.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.addslashes.php[/a]

But first make sure you know what you're magic_quotes setting is before applying the above type of functions on form data. See:

[a href=\"http://us2.php.net/manual/en/function.get-magic-quotes-gpc.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.get-...-quotes-gpc.php[/a]
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.