1

How can insert values on input in database mysql by json_encode and foreach, this html code and php code don't work true and done insert value incomplete in database table as: "[" Or "1"

DEMO: http://codepad.viper-7.com/LAOJXC

I want they as in database table row:

           Column data_1           | Column static
Row1: ["1111111111", "2222222222"] | 12
Row2: ["3333333333", "4444444444"] | 34
Row1: ["5555555555", "6666666666"] | 56

<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="static[]" value="12">

<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">
<input name="static[]" value="34">

<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="static[]" value="56">

$data = array();
$data_1 = json_encode($_POST[data_1]);
$static = $_POST[static];
foreach($static as $idx=>$val){
    $data[] = array(
              'data_1' => $data_1[$idx],
              'static' => $static[$idx]
                   )
}

$this->db->insert_batch('MyTable', $data);
3
  • what data you are going to store and why JSON Commented Oct 30, 2011 at 10:05
  • It seems the problem is in your $this->db object. What's this, some kind of PDO? Does it work if you replace insert_batch to simple insert method (in the loop)? What error throws mysql? Commented Oct 30, 2011 at 10:10
  • @Kasheftin I am curious, if you familiar with PHP syntax? Commented Oct 30, 2011 at 10:16

1 Answer 1

4

The reason why your code only inserts single characters to the database is because you convert the array $_POST['data_1'] to a string using json_encode() and then try to access this string as an array later.

When you access a string with the square brackets notation (in your code: $data_1[$idx]), PHP interprets this as a character-access in the string, resulting in only a single character.

From the PHP manual:

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

Look at this working example:

<form method="post">
<input name="data_1[]" value="1111111111">
<input name="data_1[]" value="2222222222">
<input name="static[]" value="12">

<input name="data_1[]" value="3333333333">
<input name="data_1[]" value="4444444444">
<input name="static[]" value="34">

<input name="data_1[]" value="5555555555">
<input name="data_1[]" value="6666666666">
<input name="static[]" value="56">
<input type="submit">
</form>

<?php
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
    $data[] = array(
              'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx*2+1])),
              'static' => $static[$idx]
                   );
}
echo "<pre>";
print_r($data);
?>

DEMO http://codepad.viper-7.com/ca51ZB

Sign up to request clarification or add additional context in comments.

1 Comment

added some explanation because SO is a place for anyone to learn and understand instead of just getting their problems solved.

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.