0

I don't know why insert values in my table doesn't work using foreach to traverse Json Object.. when I see the table, fields are empty.

Json Object (from jquery.ajax)

[{"marca":"Cisco","producto":"UCS","subproducto":"Nexus"},"marca":"Citrix","producto":"Networking","subproducto":"Netscaler"}]

print_r($data)

Array
(
[0] => Array
    (
        [marca] => Cisco
        [producto] => UCS
        [subproducto] => Nexus
    )

[1] => Array
    (
        [marca] => Citrix
        [producto] => Networking
        [subproducto] => Netscaler
    )

)

PHP CODE

$data = json_decode($this->dataNewPoliza['alcances'], true);

$marca;
$producto;
$subproducto;

$sqlAlc = "INSERT INTO t_poliza_alcanceproductos VALUES (:idp,:m,:p,:s)";
$resultAlc = $this->dbConnect->prepare($sqlAlc) or die ($sqlAlc);

foreach ($data as $key => $value) {
    foreach ($value as $mps => $valuemps) {
            if($mps == 'marca') {
            $marca = $valuemps;
        }

        if($mps == 'producto') {
            $producto = $valuemps;
        }

        if($mps == 'subproducto') {
            $subproducto = $valuemps;
        }
    }

    $resultAlc->bindValue(':idp',$id_poliza,PDO::PARAM_INT);
    $resultAlc->bindValue(':m',$marca,PDO::PARAM_INT);
    $resultAlc->bindValue(':p',$producto,PDO::PARAM_INT);
    $resultAlc->bindValue(':s',$subproducto,PDO::PARAM_INT);

    if(!$resultAlc->execute()) {
        return false;
    } else {
        return true;
    }
}

Hope to get some help.

Solved

The problem was that I could not do return false or true within the foreach and as said @zerkms , I don't need nested loop, and this is final result:

$data = json_decode($this->dataNewPoliza['alcances'], true);

$sqlAlc = "INSERT INTO t_poliza_alcanceproductos VALUES (:idp,:m,:p,:s)";

$resultAlc = $this->dbConnect->prepare($sqlAlc) or die ($sqlAlc);

foreach ($data as $key => $value) {
    $resultAlc->bindValue(':idp',$id_poliza,PDO::PARAM_INT);
    $resultAlc->bindValue(':m',$value['marca'],PDO::PARAM_INT);
    $resultAlc->bindValue(':p',$value['producto'],PDO::PARAM_INT);
    $resultAlc->bindValue(':s',$value['subproducto'],PDO::PARAM_INT);

    $resultAlc->execute();
}

return true;

Thanks for ur help!

6
  • Discover $value['marca'], what you do with nested loop is just nonsense Commented Jan 24, 2013 at 23:52
  • I agree with zerkms also is it normal that $id_poliza is not set in ur code (probably set before no?) Commented Jan 24, 2013 at 23:54
  • Could give me an example of how you could do better please? And yes, $id_poliza stated before that code. Commented Jan 24, 2013 at 23:56
  • 1
    @SoldierCorp: $value['marca'] - this is how you access the array values Commented Jan 25, 2013 at 0:02
  • @zerkms Nested loop is necesary because Json has two arrays inside, then if I remove the second each, only insert one record in database (but with blank values yet). I print $value['marca'] , value['producto'] , value['subproducto'] before execute $result and if they have data but not insert. Commented Jan 25, 2013 at 0:18

1 Answer 1

1

PDOStatement::bindValue data type wrong, $marca, $product, $subproducto is string not integer.

$resultAlc->bindValue(':m',$marca,PDO::PARAM_STR);

Maybe you should look PDOStatement::bindValue

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

3 Comments

Yes, corrected that but now the loop runs only 1 time and only 1 array inserted in the table
after $resultAlc->execute print PDO::errorInfo Fetch extended error information associated with the last operation on the database handle
@SoldierCorp So what is your new question? If insert for failed you whether tried print PDO::errorInfo.

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.