简体   繁体   中英

PHP & MYSQL: Insert records in two different tables

I'm trying to save records in two different tables using PHP but it seems that my first query is working and the second query not. it inserts records in table borrow (ontlening) but it does not insert records into table borrow-details (ontleninglijn). Here is my PHP code: 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");
}  

also other PHP file:

    <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>

echo the query that is not working, try it in phpmyadmin or your mysql client (like heidisql or mysql workbench) and see what mysql says about your query

Furthermore I would like to suggest to at the very least filter your customer input, because these queries are hurting my eyes. (I would also advise using prepared statements in this day and age too, maybe something you can look at when you get this to work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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