Jump to content

Help with database multiple record entry with php


Go to solution Solved by boompa,

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.

  • Solution

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

<?php

$emails = 'joe@example.com,fred@example.com,betty@example.net';
$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 = 'joe@example.com,fred@example.com,betty@example.net';
$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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.