Jump to content

Help with database multiple record entry with php


ezeuba

Recommended Posts

Good day all. I am trying to achieve entering multiple email addresses to a mysql database field all at once. I wrote the following script to manage this but it's not working. I am seperating the emails with comma, but it only enters this (') into the database. I am trying to use implode function but I don't seem to be getting it right. Any help will be appreciated. Here is the code:

<?php
if(isset($_POST['submit'])){

$email = $_POST['email'];

if(empty($email)) {
echo '<p><font color="red"><strong>NO EMAIL ADDRESSES ENTERED</strong></font></p>';
$badinput = '<p><font color="red"><strong>PLEASE INPUT MEMBERS' EMAIL ADDRESSES</strong></font></p>';
echo $badinput;
}
else{

mysql_connect("somehost","someadmin","somepassword");//database connection
mysql_select_db("somedatabase");

//inserting data order
$allemails="('".implode("'), ('",$email)."')";
$membersemail = "INSERT INTO sometable (email) VALUES ('$allemails')";

//declare in the order variable
$result = mysql_query($membersemail);    //$membersemail executes
}
if($result){
    echo '<center><font color="red"><h1>SUCCESS!!!</h1></font></center>';
    echo '<center><h3>DATABASE POPULATED WITH MEMBERS' EMAIL ADDRESSES</h3></center>';
}
}
?>

<form name= "someform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<center><table border="0" cellspacing="10">
        <tbody>
        <tr>
          <td align="right"><span class="style5">Members' E-mail Addresses:</span> </td>
          <td><label for="email"></label>
            <textarea name="email" id="email" cols="45" rows="10"></textarea>
          </td>
        </tr>
      </tbody></table>
      <input type="submit" name="submit" value="Submit">
      <input type="reset" name="reset" value="Reset"></center>
</form>

What am I doing wrong? Thanks all.

See if this helps you with what you need to do:

<?php

$emails = '[email protected],[email protected],[email protected]';
$values = '';
foreach(explode(',', $emails) as $email)
    $values .= "('$email'),";

$query = "INSERT INTO emails VALUES " . rtrim($values, ',');

echo "$query\n";

 

See if this helps you with what you need to do:

<?php

$emails = '[email protected],[email protected],[email protected]';
$values = '';
foreach(explode(',', $emails) as $email)
    $values .= "('$email'),";

$query = "INSERT INTO emails VALUES " . rtrim($values, ',');

echo "$query\n";

Thanks a lot. I am in your debt oh master of php...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.