繁体   English   中英

PHP和MYSQL:在两个不同的表中插入记录

[英]PHP & MYSQL: Insert records in two different tables

我正在尝试使用PHP将记录保存在两个不同的表中,但似乎我的第一个查询正在运行而第二个查询没有。 它在表借(ontlening)中插入记录,但它不会将记录插入表借用细节(ontleninglijn)。 这是我的PHP代码:include('dbcon.php');

$id  = $_POST['selector'];
$ontlenerId  = $_POST['ontlenerId'];
$ontleningEindDatum  = date('Y-m-d', strtotime("+21 days"));

if ($id == '' ){ 
    header("location: transacties.php");
     }
else{

    $query1 = "INSERT INTO `ontlening`(`OntlenerId_RS`, `BibliothecarisId_RS`, `OntleningDatum`, `OntleningEindDatum`) VALUES ('$ontlenerId', 1, NOW(), '$ontleningEindDatum')";
    mysqli_query($cn, $query1)or die(mysql_error());
    $query2 = mysqli_query($cn, "select * from ontlening order by OntleningId DESC")or die(mysql_error());
    $row = mysqli_fetch_array($query2);
    $ontleningId  = $row['OntleningId']; 
    $N = count($id);
    for($i=0; $i < $N; $i++)
    {
        $query3 = "INSERT INTO `ontleninglijn`(`BoekId_RS`, `OntleningId_RS`, `OntleningStatus`) VALUES ('$id[$i]','$ontleningId','Ontleend')";
        mysqli_query($cn, $query3)or die(mysql_error());

    }
    header("location: ontleningTicket.php");
}  

还有其他PHP文件:

    <lable>Ontleningdatum:</lable><br>
    <input type="date" value="<?php echo date('Y-m-d') ?>" id="ontleningDatum" disabled>

    <lable>Ontlening Einddatum:</lable><br>
    <input type="date" name="ontleningDatumEind" id="ontleningDatumEind" value="<?php echo date('Y-m-d', strtotime("+21 days")); ?>" disabled><br>
    <button name="ontlenen_opslaan" style="width:200px; height: 25px">Ontlenen</button>
</div>
<div id="ontlenen">
    <div id="ontlener_gegevens">
        <?php
            include('dbcon.php');
            $sql = "SELECT * FROM ontlener";
            $result = mysqli_query($cn, $sql) or die("Error");
        ?>
        <label>Ontlener Naam:</label><br>
        <select id="select2" name="ontlenerId" style="width:300px;" required>
            <option value=""></option>          
            <?php 
                $ontlenerId = $row['OntlenerId'];
                while($row = mysqli_fetch_array($result)) { ?>
                <option value="<?php echo $row['OntlenerId']; ?>"><?php echo $row['OntlenerVoornaam']."  ".$row['OntlenerAchternaam']." ".",".$row['OntlenerKlas']; ?></option>
                <?php } ?>
        </select>
    </div>
</div>
<div id="boekgegevens" style="width:1000px; text-align: center;">
    <p>Boek Gegevens:</p>

    <table id="datatabel">
        <thead>
            <tr>
                <th>Boek Id</th>
                <th>Titel</th>
                <th>Auteur</th>
                <th>Genre</th>
                <th>Aantal Kopies</th>
                <th>Toevoegen</th>
            </tr>
        </thead>
        <tbody>
        <?php
            include('dbcon.php');
            $sql = "SELECT boek.BoekId, boek.BoekTitel, auteur.AuteurAchternaam, auteur.AuteurVoornaam, genre.GenreNaam, boek.BoekKopies FROM boek INNER JOIN boekauteur ON boek.BoekId = boekauteur.BoekId_RS INNER JOIN auteur ON boekauteur.AuteurId_RS = auteur.AuteurId INNER JOIN boekgenre ON boek.BoekId = boekgenre.BoekId_RS INNER JOIN genre ON boekgenre.GenreId_RS = genre.GenreId WHERE BoekKopies != 0";
            $result_boek = mysqli_query($cn, $sql) or die("Error");
            while($row=mysqli_fetch_array($result_boek)){
            $id=$row['BoekId'];
        ?>
            <tr class="<?php echo $row['BoekId']; ?>">

                <td><?php echo $row['BoekId']; ?></td>
                <td><?php echo $row['BoekTitel']; ?></td>
                <td><?php echo $row ['AuteurVoornaam']." ".$row ['AuteurAchternaam'];?> </td> 
                <td><?php echo $row['GenreNaam']; ?> </td> 
                <td><?php echo $row['BoekKopies']; ?></td>
                <td width="20"><input id="" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
                </td>

            </tr>
            <?php  }  ?>
        </tbody>
    </table>
</div>

回显不起作用的查询,在phpmyadmin或你的mysql客户端(如heidisql或mysql workbench)中尝试,看看mysql对你的查询有什么看法

此外,我建议至少过滤您的客户意见,因为这些查询伤害了我的眼睛。 (我也建议在这个时代使用准备好的陈述,也许你可以看到当你开始工作时。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM