获取2个表数据并创建一个新表

时间:2019-05-14 16:48:35

标签: php mysqli

我尝试从输入表和out_materials表的2个表中获取数据,最后我想创建一个名为total的新表,我要在其中插入从那2个表中找到的获取数据

$ sql用于获取输入表 $ sql1用于out_materials表获取 和$ sql2用于总表,我想在其中插入所有数据 $

                            session_start();
                            // Change this to your connection info.
                            $DATABASE_HOST = 'localhost';
                            $DATABASE_USER = 'root';
                            $DATABASE_PASS = '';
                            $DATABASE_NAME = 'storemgmnt';
                            // Try and connect using the info above.
                            $con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
                            if ( mysqli_connect_errno() ) {
                                // If there is an error with the connection, stop the script and display the error.
                                die ('Failed to connect to MySQL: ' . mysqli_connect_error());
                            }

                            $sql="SELECT Materials_code,Materials_name,Parts_code,Unit,Input_qty FROM input ORDER BY Materials_name";
                            if ($result=mysqli_query($con,$sql))
                                  {
                                  // Fetch one and one row
                                  while ($row=mysqli_fetch_row($result))
                                    {
                                        $inm_code = $row['Materials_code'];
                                        $inm_name = $row['Materials_name'];
                                        $inp_code = $row['Parts_code'];
                                        $inunit = $row['Unit'];
                                        $inqty = $row['Input_qty'];
                                    }
                                  // Free result set
                                 // mysqli_free_result($result);
                                }
                            $sql1="SELECT Materials_code,Materials_name,Parts_code,Unit,Out_qty FROM out_materials ORDER BY Materials_name";
                            if ($result1=mysqli_query($con,$sql1))
                                  {
                                  // Fetch one and one row
                                  while ($row1=mysqli_fetch_row($result1))
                                    {
                                        $outm_code = $row1['Materials_code'];
                                        $outm_name = $row1['Materials_name'];
                                        $outp_code = $row1['Parts_code'];
                                        $outunit = $row1['Unit'];
                                        $outqty = $row1['Out_qty'];
                                    }
                                  // Free result set
                                //  mysqli_free_result($result);
                                }

                            $sql2=mysqli_query($con, "INSERT INTO total (Materials_code,Materials_name,Parts_code,Unit,Input_qty,Out_qty)
                            VALUES('$inm_code','$inm_name','$inp_code','$inunit','$inqty','$outqty')");


                            echo "<table border='1'>
                                 <tr>
                                   <th>Materials Code</th>
                                   <th>Materials Name</th>
                                   <th>Parts Code</th>
                                   <th>Unit</th>
                                   <th>Input Qty</th>
                                   <th>Out Qty</th>
                                   <th>Qty</th>

                                 </tr>";
                        if(is_resource($sql2))
                        {
                            while($row2 = mysql_fetch_array($sql2))
                                  {
                                  echo "<tr>";
                                  echo "<td>" . $row2['Materials_code'] . "</td>";
                                  echo "<td>" . $row2['Materials_name'] . "</td>";
                                  echo "<td>" . $row2['Parts_code'] . "</td>";
                                  echo "<td>" . $row2['Unit'] . "</td>";
                                  echo "<td>" . $row2['Input_qty'] . "</td>";
                                  echo "<td>" . $row2['Out_qty'] . "</td>";
                                  echo "<td>" . $row2['Qty'] . "</td>";
                                  echo "</tr>";
                                  }
                        }

                            echo "</table>";

                            mysqli_close($con);
                        ?>

0 个答案:

没有答案