473,509 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php array inserted (element by element) into mysql DB

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
3 2332
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2220
by: hshen | last post by:
Hi All, I have encountered a weird behavoir of embedded MySQL. Through a simple program I made (in Delphi 6), I can insert rows to a table through embedded MySQL and I can retrieve the rows from...
3
8561
by: jacob nikom | last post by:
Hi, I would like to be able to store numerical array in MySQL. I am really looking for the solution to store it in the one field only. For example, I have a matrix, which I want to store in...
5
6355
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
2
4288
by: Josué Maldonado | last post by:
Hi list, Is there a way to access an especific element of the array NEW in an TCL trigger, I have a loop that goes for each field (thanks Ian & Darren) like this: foreach id { then I can...
3
6860
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
0
2470
by: lvpaul | last post by:
Hello ! I am calling a .NET Webservice and getting back a ADO.NET Datatset. How can I convert this to an PHP-Array ? $ergebnis = $m_service->call( $function, $data ); print_r($ergebnis)...
2
1763
by: Nico | last post by:
Dear all, I created the following php form. <FORM METHOD="POST" ACTION="prova.php"> <b>Combination</b><br><br> Element 1: <INPUT NAME="el1" TYPE="TEXT"> <BR> Element 2: <INPUT NAME="el2"...
4
6485
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
2
4088
Claus Mygind
by: Claus Mygind | last post by:
I have an array of objects (a hash table). My section of code, the "for in" loop (lines 18 to 41) does not execute at full speed. But if I put in an alert box (line 13) to stop program execution...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.