Jump to content

Archived

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

masgas

Insert in a DB, thanks

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 ();}
?>

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
ok!!! grerat! it worked perfect!

I guess I will go now and spend the time you saved me learning about passing variables to the functions!!!

Thank you again!

Share this post


Link to post
Share on other sites

×

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.