1

I have a PHP page that dynamically generates forms based on the data received from a MySQL database. For example, say there is an entry for the Logo URL, it would be as follows:

configName = MainLogoURL; configValue = "blah"; 

That's the general idea. The task that I'm struggling with is updating the database, all of the data is sent through a POST request. The end result is like this

Array ( 
       Array ( configName => "MainLogoURL", configValue => "BLAH 2" )
)

And obviously there would be more arrays in that structure for each variable. If there is a more efficient way then could you please let me know (and how to do it) or could you just let me know how to do it.

bgs
  • 3,061
  • 7
  • 40
  • 58
Brodie
  • 73
  • 12

2 Answers2

0

As Yatin said, foreach($_POST as $key => $value) is a good start.

You should take a look at the MySQL documentation for REPLACE statements. That should allow you to insert multiple values with a single statement.

The PHP manual has PDO examples to get you started, and there are a couple of working PDO multiple insert examples here.

Community
  • 1
  • 1
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
0

just like some people sayed... use pdo, in your case this should look like:

$dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$stmt = $dbh->prepare("INSERT INTO myTable (configName, configValue) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

$myArray = Array ( 
   Array ( "configName" => "MainLogoURL", "configValue" => "BLAH 2" )
)


foreach($myArray as $arr)
{
    $name  = $arr['configName'];
    $value = $arr['configValue'];
    $stmt->execute();
}

posted this because you sayed you want to have a sample :)

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • of course if you want to update you have to change the statement :) – Dwza Dec 20 '13 at 15:25
  • you may have a look at [pdo stored procedur](https://www.google.de/#q=pdo+stored+procedure&safe=off) this looks like you can do some stuff to get it better running... in case there are to many array entrys :) but im still happy that i could help :) – Dwza Dec 20 '13 at 15:43