-1

I have no idea why this foreach it`s not running. When i post the article, only inserts once. Should make it at much as needed. Do you see any problem?

$array_produse = array();
for($i=1; $i<=8; $i++)
{
    $gramaj = isset($_POST['image'.$i.'_gramaj']) ? $_POST['image'.$i.'_gramaj'] : '';
    $pret = isset($_POST['image'.$i.'_pret']) ? $_POST['image'.$i.'_pret'] : '';

    $array_produse[$i]['gramaj'] = $gramaj;
    $array_produse[$i]['pret'] = $pret;
    $array_produse[$i]['numar_produs'] = $i;
}

foreach($array_produse as $produs)
{
   $wpdb->insert(
        'produse',
        array(
            'id' => $post_id,
            'pret' => $produs['pret'],
            'gramaj' => $produs['gramaj'],
            'numar_produs' => $produs['numar_produs']
        ));
}
10
  • Try var_dump($array_produse) before foreach and see what the output is
    – shea
    Jan 17, 2013 at 23:08
  • I have 8 meta in total. I added just 3 in this example when posted the article. Result: array(8) { [1]=> array(3) { ["gramaj"]=> string(1) "1" ["pret"]=> string(1) "1" ["numar_produs"]=> int(1) } [2]=> array(3) { ["gramaj"]=> string(1) "2" ["pret"]=> string(1) "2" ["numar_produs"]=> int(2) } [3]=> array(3) { ["gramaj"]=> string(1) "3" ["pret"]=> string(1) "3" ["numar_produs"]=> int(3) } Jan 17, 2013 at 23:10
  • Where are you getting $post_id from?
    – shea
    Jan 17, 2013 at 23:13
  • $post_id = $post->ID; Jan 17, 2013 at 23:13
  • Is the id column unique? What is your db table structure?
    – shea
    Jan 17, 2013 at 23:14

4 Answers 4

0

(See comments)

$wpdb->insert(
    'produse',
    array(
        'id' => $post_id,
        'pret' => $produs['pret'],
        'gramaj' => $produs['gramaj'],
        'numar_produs' => $produs['numar_produs']
    ),
    array(
        '%d',
        '%s',
        '%s',
        '%d'
    )    
);
5
  • Ok, added that. Same problem... Jan 17, 2013 at 23:30
  • Hmm, i tried var_dump($produs), this only returns the first item. If i try var_dump($array_produse), I get all the items that i add in wordpress post. What should I do now?... Jan 17, 2013 at 23:49
  • This is the expected behavior - the foreach loop strips a single item from $array_produse and stores it in $array_produse. I'm not too sure what's going wrong
    – shea
    Jan 18, 2013 at 0:00
  • Make $post_id an integer: replace 'id' => $post_id, with 'id' => (int) $post_id, Jan 18, 2013 at 0:48
  • Not working.... Jan 18, 2013 at 1:09
0
// Turn on errors display
$wpdb->show_errors();

foreach($array_produse as $produs){

    // Prepare query
    $sql = $wpdb->prepare("
        INSERT INTO $wpdb->produse ('id', 'pret', 'gramaj', 'numar_produs') 
        VALUES (%d, %s, %s, %s)", 
        $post_id, $produs['pret'], $produs['gramaj'], $produs['numar_produs']
        )
    );

    // Execute query
    $wpdb->query($sql);

    // Show error if any
    $wpdb->print_error(); 

}
2
  • No error shown, no insert made :) Jan 18, 2013 at 9:40
  • Do you mind sharing the code (all of it)? Jan 18, 2013 at 10:36
0

Problem solved. 'id' from db was primary, needs to be index.

0

Final code, if anyone needs it:

global $wpdb;

$array_produse = array();
for($i=1; $i<=8; $i++)
{
    $gramaj = isset($_POST['image'.$i.'_gramaj']) ? $_POST['image'.$i.'_gramaj'] : '';
    $pret = isset($_POST['image'.$i.'_pret']) ? $_POST['image'.$i.'_pret'] : '';

    if($gramaj != '' and $pret != '')
    {
        $array_produse[$i]['id'] = $post->ID;
        $array_produse[$i]['gramaj'] = $gramaj;
        $array_produse[$i]['pret'] = $pret;
        $array_produse[$i]['numar_produs'] = $i;
    }
}

foreach($array_produse as $produs)
{

    $wpdb->insert(
        'produse',
        array(
            'id' => $produs['id'],
            'pret' => $produs['pret'],
            'gramaj' => $produs['gramaj'],
            'numar_produs' => $produs['numar_produs']
        ),
        array(
            '%d',
            '%s',
            '%s',
            '%d'
        )
    );
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.