FOREACH Insert loop problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

FOREACH Insert loop problem!

Post by cap2cap10 »

Hello again, php Technorati! Yes, I have another issue that has arisen! I am getting an error for my mysql insert loop.
here is the error:
Parse error: syntax error, unexpected ')'
here is my code:

Code: Select all

if (isset($_POST['uploaded']))
{
$email = $_POST['email'];
foreach($email){
// Connect to server and select databse.
mysql_connect(*******************************) or die(mysql_error());
mysql_select_db(***********) or die(mysql_error());
 
$insert_email = "INSERT INTO mail_list (email) VALUES ('$email')";
$result = mysql_query($insert_email) or die (mysql_error());
 
$count1 = '0';
 
while ($row = mysql_fetch_assoc($result))
{
    $count1 ++;
}
}
 
}
mysql_close();
echo " All ".$count1." Addresses have been uploaded to Database!";
 
?>
I am trying to insert 30 email addresses into my mail list.
Can someone point me in the right direction? :banghead:
Thanks in advance

Batoe

Many obstacles are on the path, but so is success! The unknown Optimist
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: FOREACH Insert loop problem!

Post by klevis miho »

ok change this:
foreach($email)
to this:
foreach($email as $values)

and change all $email occurences with $values
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: FOREACH Insert loop problem!

Post by cap2cap10 »

Ok, I am getting another error. Here is the error:
Warning: Invalid argument supplied for foreach()
Here is the code:

Code: Select all

<?php
if (isset($_POST['uploaded']))
{
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
foreach($email as $values){
// Connect to server and select databse.
mysql_connect(**********************************) or die(mysql_error());
mysql_select_db(**************) or die(mysql_error());
 
$insert_email = "INSERT INTO mail_list (email) VALUES ('$values')";
$result = mysql_query($insert_email) or die (mysql_error());
 
$count1 = '0';
 
while ($row = mysql_fetch_assoc($result))
{
    $count1 ++;
}
}
echo " The ".$count1." Addresses have been uploaded to Database!";
}
 
 
?>
Please enlighten me.

Thanks in advance

Batoe
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: FOREACH Insert loop problem!

Post by klevis miho »

Paste the html form code
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: FOREACH Insert loop problem!

Post by cap2cap10 »

here is the code:

Code: Select all

<tr><form method="POST" action="my_email_contacts.php">
  <td>1.&nbsp;<input name="email" type="text" size="35"></td>
  <td>2.&nbsp;<input name="email" type="text" size="35"></td>
  <td>3.&nbsp;<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>4.&nbsp;<input name="email" type="text" size="35"></td>
  <td>5.&nbsp;<input name="email" type="text" size="35"></td>
  <td>6.&nbsp;<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>7.&nbsp;<input name="email" type="text" size="35"></td>
  <td>8.&nbsp;<input name="email" type="text" size="35"></td>
  <td>9.&nbsp;<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>10.<input name="email" type="text" size="35"></td>
  <td>11.<input name="email" type="text" size="35"></td>
  <td>12.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>13.<input name="email" type="text" size="35"></td>
  <td>14.<input name="email" type="text" size="35"></td>
  <td>15.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>16.<input name="email" type="text" size="35"></td>
  <td>17.<input name="email" type="text" size="35"></td>
  <td>18.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>19.<input name="email" type="text" size="35"></td>
  <td>20.<input name="email" type="text" size="35"></td>
  <td>21.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>22.<input name="email" type="text" size="35"></td>
  <td>23.<input name="email" type="text" size="35"></td>
  <td>24.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>25.<input name="email" type="text" size="35"></td>
  <td>26.<input name="email" type="text" size="35"></td>
  <td>27.<input name="email" type="text" size="35"></td>
</tr>
<tr>
  <td>28.<input name="email" type="text" size="35"></td>
  <td>29.<input name="email" type="text" size="35"></td>
  <td>30.<input name="email" type="text" size="35"></td>
</tr>
<tr><td colspan="3" align="center">
<input type=hidden name=uploaded value=true />
<input type="Submit" value="Submit!" ></form>
</td></tr>
Batoe
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: FOREACH Insert loop problem!

Post by klevis miho »

try to make the
name="email"
to
name="email[]"

and leave the php code as is
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: FOREACH Insert loop problem!

Post by cap2cap10 »

Ok great, but one more error. Data has been added to db but we get this error.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
Here is php code again:

Code: Select all

<?php
if (isset($_POST['uploaded']))
{
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
$email = $_POST['email'];
foreach($email as $values){
// Connect to server and select databse.
mysql_connect(**************************************) or die(mysql_error());
mysql_select_db(**************) or die(mysql_error());
 
$insert_email = "INSERT INTO mail_list (email) VALUES ('$values')";
$result = mysql_query($insert_email) or die (mysql_error());
 
$count1 = '0';
 
while ($row = mysql_fetch_assoc($result))
{
    $count1 ++;
}
}
echo " The ".$count1." Addresses have been uploaded to Database!";
}
 
 
?>
Batoe
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: FOREACH Insert loop problem!

Post by Eran »

Why are you assigning $email 20 times... I didn't think such code existed outside of the daily WTF
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: FOREACH Insert loop problem!

Post by cap2cap10 »

Gotcha! I thought that since there were 30 input fields that it was necessary. Ok one is enough then?

Sorry, still a novice.


Batoe
Post Reply