0

I have a form with capturing data and posting in an array named "table" so the tables, columns and tuples are linked. I am now trying to iterate through them and seperate them into there individual strings, "$table" = name "$columns" = columns "$columnData" = data

When i do a var_dump on the data, it looks like:-

array (size=2)
0 => 
array (size=3)
  'name' => string 'quote' (length=5)
  'columns' => 
    array (size=3)
      0 => string 'qid' (length=3)
      1 => string 'item' (length=4)
      2 => string 'price' (length=5)
   'data' => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string 'ball' (length=4)
      2 => string '200' (length=3)
 1 => 
array (size=3)
  'name' => string 'rfq' (length=3)
  'columns' => 
    array (size=2)
      0 => string 'id' (length=2)
      1 => string 'item' (length=4)
  'data' => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string 'batt' (length=4)

Which looks in the correct format, although when i try to insert into the database it does not insert, there is something about the $columns variable in the insert statement that does not work. Although I am unsure if i am iterating through the data correctly.

 $AllData = $_POST["table"];
  //  var_dump($AllData)


foreach ($AllData as $sigleData) {

    $table = $sigleData['name'];
    $columns = implode(" ", $sigleData['columns']);
    $columnData = implode(" ", $sigleData['data']);

     $sqlQuery = "INSERT INTO " . $table . " ( " . $columns . ") VALUES( '" . $columnData . "')";

if ($dbConnectionT->query($sqlQuery) == TRUE) {
    echo "database updated";
    echo "</br>";
}
}

So table quote has columns qid, item and price. with values 1, ball and 200 which i am trying to insert correctly into.

Please note, I understand the database violations here, but trying to get the programming example working,

If the previous page is required, please comment. Thanks in advance.

3
  • 1
    echo $sqlQuery and see what you are getting. Commented Apr 15, 2015 at 12:34
  • Wouldn't you need to implode using a comma as the delimiter instead of a space? Commented Apr 15, 2015 at 12:35
  • Use Comma(,) as delimiter Commented Apr 15, 2015 at 12:49

2 Answers 2

1

Try with -

foreach ($AllData as $sigleData) {

      $table = $sigleData['name'];
      $columns = implode(", ", $sigleData['columns']);
      $columnData = implode(" ',' ", $sigleData['data']);

      $sqlQuery = "INSERT INTO " . $table . " ( " . $columns . ") VALUES( '" . $columnData . "')";

      if ($dbConnectionT->query($sqlQuery) == TRUE) {
        echo "database updated";
        echo "</br>";
      }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Billiant, that works, although the database inserts the values now 3 times, all the iterations:- so 1, ball, 200 is inserted into quote 3 times. How does this just get insert once please?
How would i put a check on please to make sure the tables exist in the database prior to updating them? Thanks
0

In your insert query you are building VALUE part with a single ' for both values... Just change the implode param with "', '"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.