Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

tkw-themsach

<?php

/* Khoi tao CSDL */

$db = mysql_connect("localhost","root","123456") or die("KHONG THE KET NOI");

mysql_select_db("thuvien",$db);

/* Ket thuc khoi tao CSDL */

?>

<!-- Tao form nhap du lieu -->

<head><title>Them sach</title></head>

<center><form>

<table>

<tr><td>Ma Sach: </td><td><input /></td></tr>

<tr><td>Ten Sach: </td><td><input /></td></tr>

<tr><td>Loai Sach: </td><td><input /></td></tr>

<tr><td>Tac gia: </td><td><input /></td></tr>

<tr><td>Nha Xuat Ban: </td>

    <td><select>

        <?php

            /* Truy van tat ca ten cac Nha Xuat Ban ra menu cho minh lua chon */

            $sql = "SELECT DISTINCT tennxb,manxb FROM nxb"; /* Lay cac gia tri tu CSDL */

            $query = mysql_query($sql);

                    if (mysql_num_rows($query) == 0) /* Kiem tra xem co CSDL chua */

                    {

                        echo "Chua co du lieu";

                    }

                else

                    {

                        while($row = mysql_fetch_array($query))

                            {

                                echo "<option>".$row[tennxb]."</option>";

                            }

                    }

            /* Ket thuc truy van ten cac Nha Xuat Ban */

        ?>

    </select></td></tr>

</table>

<input />

</form>

<!-- Ket thuc form nhap du lieu -->

<?php

/* Bat dau qua trinh nhap du lieu tu form vao CSDL */

if ($_POST[masach] != null && $_POST[masach] != "")

    {

        mysql_query("INSERT INTO sach(masach,tensach,loaisach,tacgia,manxb) VALUES('$_POST[masach]','$_POST[tensach]','$_POST[loaisach]','$_POST[tacgia]','$_POST[nxb]')");

    }

/* Ket thuc qua trinh nhap du lieu tu form vao CSDL */

/* Hien tat ca cac CSDL hien co */

$sql = "SELECT * FROM sach,nxb WHERE nxb.manxb = sach.manxb"; // Ket noi manxb tu 2 table

$query = mysql_query($sql);

/* Tao bang xuat du lieu */

    echo "<table border=1px>";

    echo "<th>Ma Sach</th><th>Ten Sach</th><th>Loai Sach</th><th>Tac Gia</th><th>Nha Xuat Ban</th><th>Dia Chi</th>";

    if (mysql_num_rows($query) == 0) // Kiem tra xem co du lieu hay chua

        {

            echo "Chua co du lieu";

        }

    else

        {

            while($row = mysql_fetch_array($query)) // Tao vong lap de xuat tat ca cac du lieu ra bang

                {

               echo "<tr><td>".$row[masach]."</td>";

                    echo "<td>".$row[tensach]."</td>";

                    echo "<td>".$row[loaisach]."</td>";

                    echo "<td>".$row[tacgia]."</td>";

                    echo "<td>".$row[tennxb]."</td>";

                    echo "<td>".$row[diachi]."</td></tr></center>";

                }

        }

    echo "</table>";

/* Ket thuc bang xuat du lieu */

?>

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: