• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with inserting into mysql table

LuckyTaxi

Diamond Member
I found a neat class that pulls your contacts from gmail, hotmail, and yahoo. It dumps the results onto your screen, displayed in a simple HTML table. I modified it so that it has checkboxes next to each entry so you can select which contacts you want to insert into a database.

The issue is when trying to insert them into a mysql table, I'm having issues grabbing the name and email. I've tried variation of nested foreach loops only to have thousands of records inserted. As it stands now, it doesn't pass the name and email.

 
so you have a php class that displays the contacts and builds the form, right? (with the ability to check multiple boxes and insert multiple contacts into your db)

the list array thats being passed, are those checkboxes as an array?

are the f_names and emails in an array as well?





also, you should insert all the rows at once. so rather than looping over a query, loop around the array and build a $values string that you can use in the query.

$values = '';
foreach ($list as $listItem)
{
$values .= "('" . $listItem['f_name'] . "','" . $listItem['email_home'] . "'),";
}
$values = trim($values, ",");

then use that var in your query.
This method involves less queries, and allows you do dump the values var in testing to make sure it's correct before inserting a bunch of bad data 🙂
 
Yes the checkboxes are an array. Thats what I was missing, putting the names/emails into an array. My problem is trying to get it to loop and retrieve all values before inserting into the db table. I'll play with it some more.

What's $values for? to hold all the values in the array?

so it'll be

sam,sam@domain.com,james,james@domain
 
pass the name and email in the form (comma seperate)

so in the form, the checkbox value will be name, email

<?
$itemValue = $fName . "," . $email;
?>
<input type=checkbox name=checkArr[] value="<?=$itemValue;?>">



then on the action page, you can loop through the checkArr
$values = ''; // this is a var you will use your query

foreach($checkArr as $items)
{
$tempArr = explode(",",$items); // temp array to pull apart the comma seperated data PER PERSON
$values .= "('" . $tempArr[0] . "','" . $tempArr[1] . "'),";
}
$values = trim($values, ","); // trim the trailing comma


then your query will be like:
$sql = "INSERT into test (f_name, email_home) values $values";
mysql_query($sql);


if you want to maintain the same IDs, you can add them to the value of the checkbox, then adjust the tempArr keys
make sure you trap data (make sure that there's no commas in the names, and make sure that each entry has a name AND an email
 
wow ... works like a charm however here's where it gets tricky. so far with your help i'm able to import the contacts i checked off. howerver, we know our address book isnt perfect. havent said that i noticed many of my contacts have just their first name or an email address as their display name. so, importing these contacts will be weird if it pulls it through with the email address listed as their first name.

so, i was thinking of taking it a step further. The html page that is displayed contains a checkbox and two input boxes.
The input boxes are nothing more than just the name and email displayed. Your code has the name/email passed in the checkbox's array but let's say I wanted to pass the field value from both input boxes? What I'm trying to get at is for the user to be able to modify the name before they import it into the db table. so say i check off two contacts and I wanted to modify the name for these two contacts, i would like to pass the value from the name field and not through the checkbox array. makes sense?

 
Originally posted by: lilcam
wow ... works like a charm however here's where it gets tricky. so far with your help i'm able to import the contacts i checked off. howerver, we know our address book isnt perfect. havent said that i noticed many of my contacts have just their first name or an email address as their display name. so, importing these contacts will be weird if it pulls it through with the email address listed as their first name.

so, i was thinking of taking it a step further. The html page that is displayed contains a checkbox and two input boxes.
The input boxes are nothing more than just the name and email displayed. Your code has the name/email passed in the checkbox's array but let's say I wanted to pass the field value from both input boxes? What I'm trying to get at is for the user to be able to modify the name before they import it into the db table. so say i check off two contacts and I wanted to modify the name for these two contacts, i would like to pass the value from the name field and not through the checkbox array. makes sense?


makes perfect sense!
there's several ways go to about it though.

on the form, put the text fields there and populate them with any name data you have (if none, leave blank)
name each field using an identifier PLUS the id
so if your ID is 3, the fName field could be <input type="text" name="fName3" value="">
also add the email as hidden (or text if you want it editable)
then use some variable variables on the action page to snag the data.

i haven't tested this, but it shold work

edit: jees, code attach sucks

<?
// form page
?>
<input type="checkbox" name="id[]" value="3">
<input type="hidden" name="email3" value="john@doe.com">
<input type="text" name="fName3" value="Display name, if you got it">
<?




// action page
$values = array(); // blank array to hold insert values (better than using a string)

foreach($checkArr as $items)
{
if(isset($_GET['fName'.$items]) && strlen(trim($_GET['fName'.$items])))
$thisFname = $_GET['fName'.$items];
if(isset($_POST['email'.$items]) && strlen(trim($_POST['email'.$items])))
$thisEmail = $_POST['email'.$items];

$values[] = "('" . $thisFname . "','" . $thisEmail . "')";
}


// then your query will look like
if(count($values))
{
$sql = "INSERT into test (f_name, email_home) values " . implode(",",$values);
mysql_query($sql);
}
?>

 
OMG .... attaching code sucks really bad.

OMG ... it works!!!! WOW ... thanks a lot for all your help!

Quick plug now that I got this feature working!

I've been working on a free contact managment system.
We're working out the kinks and implementing new things (this being one of them) and we've gotten great feedback thus far.

check it out. thanks for your help!

Keepm.com

 
Originally posted by: lilcam
OMG .... attaching code sucks really bad.

OMG ... it works!!!! WOW ... thanks a lot for all your help!

Quick plug now that I got this feature working!

I've been working on a free contact managment system.
We're working out the kinks and implementing new things (this being one of them) and we've gotten great feedback thus far.

check it out. thanks for your help!

Keepm.com

Thats awesome!

I'm glad i was able to help
The site looks great, did you do the design? Logo?

 
Yea it was like a 10 minute job on my part. I wasnt trying to make it look sexy or anything but a lot of folks like the simplicity.

 
Back
Top