Jump to content

Insert in a DB, thanks


masgas

Recommended Posts

Hi! I'm trying to insert emails into a DB, I'm trying with this code, but it only adds numbers to the id field...

any ideas for it?

Could I insert with the same script more than one field, let's say Name, and Surname separately?

<?php
function insertar ()

{
$con = mysql_connect ('','','') or die ("imposible conectar con base de datos");
@mysql_select_db ('correos') or die ("estamos mejorando el servicio");
$sql = "INSERT INTO mails (`email`) VALUES ('$email')";
mysql_query ($sql) or die (mysql_error ($con));
}
$emails = array ("mrfeitoc@mail.es",
"egamir@mail.es",
"igonzalez@mail.es",
"jmbejar@mail.es",
"dortigosa@mail.es",
"jpulido@mail.es");
foreach ($emails as $email){
insertar ();}
?>
Link to comment
Share on other sites

You need to pass the $email variable to the function

[code]<?php
function insertar ($email)

      {
      $con = mysql_connect ('','','') or die ("imposible conectar con base de datos");
      @mysql_select_db ('correos') or die ("estamos mejorando el servicio");
      $sql = "INSERT INTO mails (`email`) VALUES ('$email')";
      mysql_query ($sql) or die (mysql_error ($con));
      }
$emails = array ("mrfeitoc@mail.es",
"egamir@mail.es",
"igonzalez@mail.es",
"jmbejar@mail.es",
"dortigosa@mail.es",
"jpulido@mail.es");
foreach ($emails as $email){
  insertar ($email);}
?>[/code]

PS You only need to connect and select the database once per page, not every time you call the function
Link to comment
Share on other sites

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.