?
Solved

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

Posted on 2011-09-21
4
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

752 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