Jump to content


Photo

Insert in a DB, thanks


  • Please log in to reply
2 replies to this topic

#1 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 16 September 2006 - 06:15 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 September 2006 - 07:45 PM

You need to pass the $email variable to the function

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

PS You only need to connect and select the database once per page, not every time you call the function
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 16 September 2006 - 07:52 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users