当我用mysqli命中提交时执行多查询

时间:2016-03-17 10:07:42

标签: php mysql mysqli

我正在学习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>

1 个答案:

答案 0 :(得分:1)

  1. 请勿使用multi_query。此功能有一个非常特殊的用途,不适合您的情况。通过单独的电话运行您的查询。
  2. 使用准备好的陈述。
  3. 使用PDO,特别是如果您只学习PHP。
  4. 代码应为

    $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]);
    

    虽然我不太明白这些东西是打算做什么的。所有这些设置和重置相同的值看起来像是一团糟。