Page 1 of 1

FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 1:34 pm
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

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 2:24 pm
by klevis miho
ok change this:
foreach($email)
to this:
foreach($email as $values)

and change all $email occurences with $values

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 2:57 pm
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

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 3:10 pm
by klevis miho
Paste the html form code

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 3:26 pm
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

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 3:41 pm
by klevis miho
try to make the
name="email"
to
name="email[]"

and leave the php code as is

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 3:58 pm
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

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 4:07 pm
by Eran
Why are you assigning $email 20 times... I didn't think such code existed outside of the daily WTF

Re: FOREACH Insert loop problem!

Posted: Wed Feb 10, 2010 4:20 pm
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