kbios
asked on
How do I insert multiple records in a php mysql db?
I have data stored in localStorage variables. I want to retreive the data and pass to an INSERT INTO statement and then have mysql process the statement. The VALUES will be parsed from the localStorage variable. Any tips or ideas for a better approach would be appreciated.
Please look at the code below:
<?php
$conn = mysql_connect("localhost", "X", "Y.") or die(mysql_error());
mysql_select_db("test");
for ($i=1;$i<=parseInt(localSt orage.trxC tr);i++)
{
if ( localStorage["item" + i] != "X" )
{
$sql = "INSERT INTO table (field1, field2, field3) VALUES ('data1', 'data2', 'data3')";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
}
mysql_close($conn);
?>
Please look at the code below:
<?php
$conn = mysql_connect("localhost",
mysql_select_db("test");
for ($i=1;$i<=parseInt(localSt
{
if ( localStorage["item" + i] != "X" )
{
$sql = "INSERT INTO table (field1, field2, field3) VALUES ('data1', 'data2', 'data3')";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
}
mysql_close($conn);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What exactly are you doing on the fields? I often use the $key in the query and other places, so with the limited information you've provided, this is realistically the best answer possible, iterating through each identifier, without worrying about ones that might not exist (because it only iterates EXISTING items in the array.)
If you need anything else regarding this question, please post. Otherwise please accept my answer :-)
-Robert
If you need anything else regarding this question, please post. Otherwise please accept my answer :-)
-Robert
ASKER
I think I can gleen what I need from your code. It at least gives me some direction. Thanks.
Open in new window