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 ("[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]");
foreach ($emails as $email){
insertar ();}
?>
Link to comment
https://forums.phpfreaks.com/topic/20991-insert-in-a-db-thanks/
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 ("[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]");
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
https://forums.phpfreaks.com/topic/20991-insert-in-a-db-thanks/#findComment-93171
Share on other sites

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.