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());
}
} 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 :
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.
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());
}
}
}
?> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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)...
|
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"...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |