Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delete database record with modal 21 34
MySQL left join performance 4 14
simple mysql statement 3 11
php ssh2_scp_send 1 6
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now