masgas Posted September 16, 2006 Share Posted September 16, 2006 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?<?phpfunction 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 ();}?> Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2006 Share Posted September 16, 2006 You need to pass the $email variable to the function[code]<?phpfunction 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 Quote Link to comment Share on other sites More sharing options...
masgas Posted September 16, 2006 Author Share Posted September 16, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.