Hi, I have problems with INSERTing VALUES in new table, with ALTER i made columns but then when I am inserting values for example one column from one table with 4 rows and second column from other table with also 4 rows, in my new table i get 16 rows instead 4 rows, and I don't know why!

You should post the queries that you were trying to use so that we can help you.

I didn't understand what you want!
Your example is not clear, what do you mean by:

with ALTER i made columns but then when I am inserting values for example one column from one table with 4 rows and second column from other table with also 4 rows, in my new table i get 16 rows instead 4 rows

What are your tables ? what are the attributes of your tables ?
How many rows you want to insert in each table ?
What do you want to do after inserting the data in your tables ?

Please write more details and give us your query to understand your Database or PHP problem.

Ok, so here is my code:

<?php
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("db_baza") or die(mysql_error()); 

    if(mysqli_connect_errno())
    {
        echo "Error: Greška u konekciji";
        exit;
    }

    $sql1 = "SHOW TABLES FROM db_baza";
    $result1 = mysql_query($sql1);


    if (!$result1) 
    {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }


    //with this loop we are going throu table names and names of the columns and showing them in checkboxes     
    while ($row1 = mysql_fetch_row($result1))
    {
        echo  "<div id='blok' style='width:200px;height:200px;background-color:red;margin:20px;float:left;'>{$row1[0]}"."</br>"; 
        $tablename=$row1[0];


        $sql2="SELECT column_name FROM information_schema.columns WHERE table_name = '{$tablename}'";
        $result2= mysql_query($sql2);

        echo "<form id='forma1' action='' method='POST'>";
        //making names of the column to be name_of_the_table_name_of_column, so i will know from which table is which column
        while($row2=mysql_fetch_row($result2)) {
            $z=$tablename."_".$row2[0];
        echo "<input type='checkbox' name='kolona[]' value='{$z}' />{$z}</br>";
        }

        echo "</div>";

    }   echo "<input type='submit' name='submit' value='Klikni'/>";
        echo "</form>";  


    if(isset($_POST['submit']))
        {                  //making variable to fit the sintax of the INSERT query
                            $x="SELECT TABLE_NAME 
                                FROM INFORMATION_SCHEMA.TABLES
                                WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='db_baza'";
                                $y=mysql_query($x);
                                while($p=mysql_fetch_row($y))
                                { $a[]=$p[0].",";}
                                $w=sizeof($a)-1;
                                $a[$w]=rtrim($a[$w],",");
                                $h=implode(" ",$a);


            if(isset($_POST['kolona']))
                {
                    //going through checked values
                    foreach($_POST['kolona'] as $vrednost)
                    { 
                    //making names of the columns in new table
                     mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");

                     //making variable to fit the sintax for INSERT query
                     $g=explode("_", $vrednost);


                     $arr[]=$g[0].".".$vrednost;

                    }
                    //inserting values in new table
                    mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',$arr))." FROM $h");


                }
        }
?>

So, I have tables in database, I need to show their names and column names through checkboxes. I need to checked columns go to new table, in this case 'tablica', I mean, that checked columns need to just to go next to each other in new table. But when I check for example one column with 4 rows from one table and other column from other table with 5 rows i get in my new table 'tablica' 20 rows, combination of this 4 rows and 5 rows and i need to get like 5 rows instead, where will be one fiels null or empty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.