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 ("[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 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 ("[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 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! Link to comment https://forums.phpfreaks.com/topic/20991-insert-in-a-db-thanks/#findComment-93173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.