我正在学习Mysql / PHP,并且尝试使用msqli执行多查询。我已经通读了一些教程,并尝试应用它们,但是我无法正常工作……希望有人能帮助我。

别管我凌乱的代码,以后会清理!!!

代码:



else if (isset($_POST['btnSlaOpInGs'])) {
		$id=$_POST['id'];
		$uitgeleend=$_POST['uitgeleend'];
		$nr=$_POST['nr'];
		$model=$_POST['model'];
		$serienummer=$_POST['serienummer'];
		$capaciteit=$_POST['capaciteit'];
		$uptodate=$_POST['uptodate'];
		$persoon=$_POST['persoon'];
		$datumuitgeleend=$_POST['datumuitgeleend'];
		$datumretour=$_POST['datumretour'];
		$opmerking=$_POST['opmerking'];
		$sql="UPDATE ipads SET nr='$nr', model='$model', serienummer='$serienummer', capaciteit='$capaciteit', uptodate='$uptodate', persoon='$persoon', datumuitgeleend='$datumuitgeleend', datumretour='$datumretour', opmerking='$opmerking' WHERE id='$id';";
		$sql.="INSERT INTO geschiedenis (SELECT * FROM ipads WHERE id='$id');";
		$sql.="UPDATE ipads SET uitgeleend='Nee', persoon='', datumuitgeleend='', datumretour='', opmerking='' WHERE id='$id';";
		mysqli_multi_query($db, $sql);
		header("location:index.php");
}





形式:



<form id="gegevensForm" class="col-xs-4" form method="POST" action="ipad-edit.php">
	<input type="hidden" name="id" value="<?php echo $id?>">
	<div class="form-group">
		<label>Uitgeleend</label>
		<select class="form-control" name="uitgeleend"/>
			<option value="Ja" <?php if($uitgeleend == "Ja") echo "selected";?>>Ja</option>
			<option value="Nee" <?php if($uitgeleend== "Nee") echo "selected";?>>Nee</option>
		</select>
	</div>

    <div class="form-group">
        <label>Nr</label>
        <input type="text" class="form-control" name="nr" value="<?php echo $nr ?>" />
    </div>

    <div class="form-group">
        <label>Model</label>
        <input type="text" class="form-control" name="model" value="<?php echo $model ?>" />
    </div>

    <div class="form-group">
        <label>Serienummer</label>
        <input type="text" class="form-control" name="serienummer" value="<?php echo $serienummer ?>" />
    </div>

	<div class="form-group">
        <label>Capaciteit</label>
		 <input type="text" class="form-control" name="capaciteit" value="<?php echo $capaciteit ?>" />
    </div>

	<div class="form-group">
		<label>Up-to-Date</label>
		<select class="form-control" name="uptodate"/>
			<option value="Ja" <?php if($uptodate == "Ja") echo "selected";?>>Ja</option>
			<option value="Nee" <?php if($uptodate == "Nee") echo "selected";?>>Nee</option>
		</select>
	</div>

	<div class="form-group">
        <label>Persoon</label>
        <input type="text" class="form-control" name="persoon" value="<?php echo $persoon ?>" />
    </div>

	<div class="form-group">
        <label>Datum uitgeleend</label>
        <input type="text" id="datepicker" class="form-control" name="datumuitgeleend" value="<?php echo $datumuitgeleend ?>" />
    </div>

	<div class="form-group">
        <label>Datum retour</label>
        <input type="text" id="datepicker1" class="form-control" name="datumretour" value="<?php echo $datumretour ?>" />
    </div>

	<div class="form-group">
		<label for="comment">Opmerking</label>
		<textarea class="form-control" rows="5" id="comment" name="opmerking" /><?php echo $opmerking ?></textarea>
	</div>

    <button class="btn btn-primary pull-right" name="btnSlaOp" input type="submit"><i class="fa fa-save"></i> Opslaan</button>

    <a href="overzicht-ipads.php"><input type="button" name="btnCancel" value="Annuleer" class="btn btn-primary pull-left"></a>

	<button class="btn btn-primary pull-middle" name="btnSlaOpInGs" type="submit"><i class="fa fa-save"></i> Opslaan & Archiveren</button>

    <?php
		include('../includes/get_footer.php');
	?>
</form>

最佳答案

请勿使用multi_query。此功能有一个非常特殊的用途,不适合您的情况。通过单独的调用运行查询。
使用准备好的语句。
使用PDO,尤其是在仅学习PHP的情况下。


该代码应为

$sql="UPDATE ipads SET nr=?, model=?, serienummer=?, capaciteit=?, uptodate=?, persoon=?, datumuitgeleend=?, datumretour=?, opmerking=? WHERE id=?";
$pdo->prepare($sql)->execute([$nr,$model,$serienummer,$capaciteit,$uptodate,$persoon,$datumuitgeleend,$datumretour,$opmerking,$id]);

$sql="INSERT INTO geschiedenis (SELECT * FROM ipads WHERE id=?)";
$pdo->prepare($sql)->execute([$id]);

$sql ="UPDATE ipads SET uitgeleend='Nee', persoon='', datumuitgeleend='', datumretour='', opmerking='' WHERE id=?";
$pdo->prepare($sql)->execute([$id]);


尽管我不太了解这些东西打算做什么。所有这些设置和相同值的重置看起来像一团糟。

关于php - 当我用mysqli点击Submit时执行多重查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36057328/

10-13 09:14