?
Solved

How do I insert multiple records in a php mysql db?

Posted on 2011-09-21
4
Medium Priority
?
353 Views
Last Modified: 2012-05-12
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(localStorage.trxCtr);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);
?>
0
Comment
Question by:kbios
  • 3
4 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36577355
This way is superior in my opinion
foreach(localStorage as $key => $value)
//key is itemi and value is value
  if($val != "X") {
    $sql = "INSERT INTO table (....)";
    $result = mysql_query($sql,$conn) or die(mysql_error());
  }

Open in new window

0
 
LVL 13

Accepted Solution

by:
themrrobert earned 2000 total points
ID: 36577357
sorry i didn't fully spell out $value, this is the proper code:
<?php
  $conn = mysql_connect("localhost", "X", "Y.") or die(mysql_error());
  mysql_select_db("test");

foreach(localStorage as $key => $value)
  //key is itemi and value is value
  if($value != "X") {
    $sql = "INSERT INTO table (....)";
    $result = mysql_query($sql,$conn) or die(mysql_error());
  }

  mysql_close($conn);
?>

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36577519
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
0
 

Author Comment

by:kbios
ID: 36577815
I think I can gleen what I need from your code. It at least gives me some direction. Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question