Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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']
        ));
}
share|improve this question
Try var_dump($array_produse) before foreach and see what the output is – bungeshea Jan 17 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) } – Iulian Bozeanu Jan 17 at 23:10
Where are you getting $post_id from? – bungeshea Jan 17 at 23:13
$post_id = $post->ID; – Iulian Bozeanu Jan 17 at 23:13
Is the id column unique? What is your db table structure? – bungeshea Jan 17 at 23:14
show 5 more comments

4 Answers

(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'
    )    
);
share|improve this answer
Ok, added that. Same problem... – Iulian Bozeanu Jan 17 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?... – Iulian Bozeanu Jan 17 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 – bungeshea Jan 18 at 0:00
Make $post_id an integer: replace 'id' => $post_id, with 'id' => (int) $post_id, – Nabil Kadimi Jan 18 at 0:48
Not working.... – Iulian Bozeanu Jan 18 at 1:09
// 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(); 

}
share|improve this answer
No error shown, no insert made :) – Iulian Bozeanu Jan 18 at 9:40
Do you mind sharing the code (all of it)? – Nabil Kadimi Jan 18 at 10:36

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

share|improve this answer

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'
        )
    );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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