PHP & MySQL : Insert records in two different tables

标签 php mysql

我正在尝试使用 PHP 将记录保存在两个不同的表中,但似乎我的第一个查询有效而第二个查询无效。它在借用表 (ontlening) 中插入记录,但不会在借用明细表 (ontleninglijn) 中插入记录。 这是我的 PHP 代码: 包括('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 对您的查询的看法

此外,我想建议至少过滤您的客户输入,因为这些查询会伤害我的眼睛。 (我也建议在这个时代也使用准备好的语句,也许当你让它工作时你可以看看。)

关于PHP & MySQL : Insert records in two different tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43310831/

相关文章:

php - 保护PDF文档不被URL直接访问

php - 计算总工作时间

php - 合并两个数组后出现 Unicode 错误

php - 使用百分比选择获取行号的最快方法(LIMIT X,1)?

jquery - 在 jqGrid 子网格中获取自定义 rowId

c# - FileStream 错误 - 进程无法访问文件...被另一个进程使用

sql - 我可以在 SQL 存储过程中分配变量吗?

Php/MySQL 到 ASP.NET/SQL Server,建议是否值得麻烦

php - 终身管理网站的用户 session

mysql - 使用连接和子查询优化简单查询