使用php(phpMyAdmin)从网站将外键插入表中

时间:2016-03-11 17:20:20

标签: php mysql

所以,我的数据库(phpMyAdmin)中有3个表,自行车,书籍和用户。我已将book_id和user_id设为book表中的外键。现在我希望这些2从网页插入到book表中。 我有3个php文件,分别是book1.php,book2.php和book3.php。在book1中,您可以选择要预订的自行车,从而在新页面中显示该特定自行车。在book2中,您可以选择要预订的日期以及何时进入book3,user_id和bike_id应自动插入到book表中?但是当我在book2页面按书时,我只得到错误。 有人可以帮我PLZ ..下面是3个php文件的php代码。

book1.php:

 <?php
require 'connect.php';

$select_posts = "select * from bikes ";

$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
    <tr>

        <th>Name</th>
        <th>Image</th>
        <th>Available</th>
        <th>Select Bike</th>
    </tr>
    <?php while($row=mysql_fetch_array($run_posts)){ ?>
    <tr>

        <td><?php echo $row[bike_name]; ?></td>
        <?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
        <td><?php echo $row[avail]; ?></td>
        <td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
    </tr>
    <?php } ?>

book2.php:

<?php 
session_start();
?>

<?php
require 'connect.php';

if(isset($_GET['id'])){

$took_id = $_GET['id'];

$select_query = "select * from bikes where bike_id='$took_id'";

$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
    <tr>

        <th>Name</th>
        <th>Image</th>
        <th>Available</th>
        <th></th>
        <th>Select Date</th>
        <th>Book</th>

    </tr>
    <?php while($row=mysql_fetch_array($run_query)){ ?>
    <tr>
        <form action="book3.php" method="POST">
        <td><?php echo $row[bike_name]; ?></td>
        <?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
        <td><?php echo $row[avail]; ?></td>
        <td><?php echo $took_id; ?></td>
        <td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
        <td><input type="submit" name="Submit" value="Book"></td>
    </tr>
    <?php }} ?>
</table>

book3.php:

<?php
session_start();
?>
<?php
require 'connect.php';

// Get values from form 
$datepicker = $_POST['datepicker'];


$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
}

else {
echo "ERROR";
}

?>

0 个答案:

没有答案