Solved

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

Posted on 2011-09-21
4
339 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now