By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

php array inserted (element by element) into mysql DB

P: n/a
The below code works well, except for the database insert. I want each
element of the array to be inserted into the database, but only the last
element of the array is inserted... all the elements will echo... any
ideas? I'm a PHP newbie... you can probably tell by the code:
if ($_POST['TOURN_ARRAY'])
{
foreach ($_POST['TOURN_ARRAY'] as $V)
{
echo "Value: $V<br>\n";
$result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES
('$id', '$V')";
}
if (mysql_query($result))
{
echo("It's all good.<br>");
}
else
{
echo("It's all bad: " . mysql_error());
}
}

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bart Nessux wrote:
The below code works well, except for the database insert. I want each
element of the array to be inserted into the database, but only the last
element of the array is inserted... all the elements will echo... any
ideas? I'm a PHP newbie... you can probably tell by the code:
if ($_POST['TOURN_ARRAY'])
{
for the sake of argument lets say your array has two elements, "one" and
"two"
foreach ($_POST['TOURN_ARRAY'] as $V)
{
echo "Value: $V<br>\n";
1st time echoes --- Value: one<br>\n
2nd time echoes --- Value: two<br>\n
$result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES ('$id', '$V')";
1st time --- $result = "INSERT ... VALUES('???', 'one')"
2nd time --- $result = "INSERT ... VALUES('???', 'two')"
}
foreach loop ended.

What is in $result?

(snip)


I'd do it more like this

$values = array();
foreach ($_POST['TOURN_ARRAY'] as $V) {
$values[] = "('$id', '$V')";
}
$result = 'INSERT ... VALUES ' . implode(', ', $values)

For my example of "one", "two" in this code above, at the end of
the loop, $values will be

{
[0] => ('???', 'one')
[1] => ('???', 'two')
}

and the final $result will be
INSERT ... VALUES ('???', 'one'), ('???', 'two')
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
Bart Nessux wrote:
The below code works well, except for the database insert. I want each
element of the array to be inserted into the database, but only the last
element of the array is inserted... all the elements will echo... any
ideas? I'm a PHP newbie... you can probably tell by the code:
if ($_POST['TOURN_ARRAY'])
{

for the sake of argument lets say your array has two elements, "one" and
"two"

foreach ($_POST['TOURN_ARRAY'] as $V)
{
echo "Value: $V<br>\n";

1st time echoes --- Value: one<br>\n
2nd time echoes --- Value: two<br>\n

$result = "INSERT INTO tb_name (recruit_id, tourn_id) VALUES ('$id', '$V')";

1st time --- $result = "INSERT ... VALUES('???', 'one')"
2nd time --- $result = "INSERT ... VALUES('???', 'two')"

}

foreach loop ended.

What is in $result?


(snip)


I'd do it more like this

$values = array();
foreach ($_POST['TOURN_ARRAY'] as $V) {
$values[] = "('$id', '$V')";
}
$result = 'INSERT ... VALUES ' . implode(', ', $values)

For my example of "one", "two" in this code above, at the end of
the loop, $values will be

{
[0] => ('???', 'one')
[1] => ('???', 'two')
}

and the final $result will be
INSERT ... VALUES ('???', 'one'), ('???', 'two')


That works great! Thanks for showing me how to go about it.

Jul 17 '05 #3

P: n/a
Hi Bart,

Using neater syntax will correct this problem in the future:

- Where did the $id variable come from?
- READ THE addslashes() documentation in the manual.
This leaves you wide open to SQL injection attack
- Notice that your foreach loop was not around your
mysql_query() call...
- Asking if $_POST['TOURN_ARRAY'] evaluates to
true is rather sloppy. You should check to see if it is
isset() and if it is an array. I used the short-circuting
feature of PHP operatiors to do this.
This is the equiv of what you had.....

<?
if ($_POST['TOURN_ARRAY'])
{
foreach ($_POST['TOURN_ARRAY'] as $V)
{
echo "Value: $V<br>\n";
$result =
"INSERT INTO tb_name (recruit_id, tourn_id) ".
"VALUES ('$id', '$V')";
}

if (mysql_query($result))
{
echo("It's all good.<br>");
}
else
{
echo("It's all bad: " . mysql_error());
}
}

?>

### This is what you should have: ###

<?
$id = 1; //Something

if (isset($_POST['TOURN_ARRAY']) && is_array($_POST['TOURN_ARRAY']))
{
foreach($_POST['TOURN_ARRAY'] as $V)
{
echo "Value: $V<br>\n";
$query =
"INSERT INTO tb_name (recruit_id, tourn_id) ".
"VALUES ('".addslashes($id)."', '".addslashes($V)."')";

if (mysql_query($result))
{
echo("It's all good.<br>");
}
else
{
echo("It's all bad: " . mysql_error());
}
}
}

?>
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.