Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following code from my data between tables, but for some reason only one value is inserted to database

<?php

    DEFINE("DB_SERVER", "localhost"); //LOCALHOST
    DEFINE("DB_USER", "user");
    DEFINE("DB_PASS", "pass");
    DEFINE("DB_NAME", "table");

    $connect = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die("connect issue". ' ' .  mysql_error());
    mysql_query('SET NAMES utf8'); 

    $db = mysql_select_db(DB_NAME,$connect) or die("connect issue". ' ' . mysql_error());
    mysql_query('SET NAMES utf8');

    if (!$db){
        echo "connect issue";
    }

    $sql = "SELECT id, column2 FROM tablea";
    $result = mysql_query($sql) or die(mysql_error());

    $set = date("Y-m-d H:i:s", time());

    while ($row = mysql_fetch_array($result)) {

        $id = $row['id'];
        $list = $row['column2'];

        echo "user_id: $id";
        echo "<br/><br/>";

        $makes = explode (";", $row['column2']);

        $i = 0;
        foreach ($makes as $make) {

            $sql2 = "SELECT url FROM tableb WHERE id = '$make'";
            $result2 = mysql_query($sql2) or die(mysql_error());

            while ($row2  = mysql_fetch_array($result2)) {

                echo $row2[0];
                echo $row2[1];
                echo $row2[2];
                echo $row2[3];
                echo $row2[4];
                echo $row2[5];
                echo $row2[6];
                echo $row2[7];

                $m1 = $row2[0];
                $m2 = $row2[1];
                $m3 = $row2[2];
                $m4 = $row2[3];
                $m5 = $row2[4];
                $m6 = $row2[5];
                $m7 = $row2[6];
                $m8 = $row2[7];

                echo "<br/>";

            }

            if (++$i == 8) break;
        }

        $sql3 = "INSERT INTO tablec (partner_id, make1, make2, make3, make4, make5, make6, make7, make8, saved) VALUES ('$id', '$m1', '$m2', '$m3', '$m4', '$m5', '$m6', '$m7', '$m8', '$set')";
        $result3 = mysql_query($sql3) or die(mysql_error());

        var_dump($sql3);
        var_dump($result3);

        if($result3) {

            echo "done";

        }

        else {

            echo "fail";

        }       

        echo "<pre>";
            var_dump($row);
        echo "</pre>";


        echo "<hr/>";

    }

?>

var_dump sql:

string(186) "INSERT INTO tablec (partner_id, make1, make2, make3, make4, make5, make6, make7, make8, saved) VALUES ('75', 'opel', '', '', '', '', '', '', '', '2014-02-06 18:20:14')"

it works fine, but the only 1 variable inserted the table "$row2[0];"

whats the problem? thank you for your help!

share|improve this question
    
I don't understand your problem. Do you have an SQL error ? –  iPouf Feb 6 at 17:30
    
Can you explain more? Which variable is inserted, which isn't, how did you check this, what have you tried to fix this so far? –  Mark Feb 6 at 17:53
add comment

1 Answer

You only ever fetch one field from your tableb:

        $sql2 = "SELECT url FROM tableb WHERE id = '$make'";
                        ^^^^---- here

If you want more fields, you need to specify them:

SELECT url, field1, field2, etc...
share|improve this answer
    
Also i think the third query needs to be inside the foreach loop.. –  Nouphal.M Feb 6 at 17:32
    
That too, but that's up to the OP to figure out... running nested queries like they're attempting is highly inefficient anyways. –  Marc B Feb 6 at 17:33
add comment

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.