This is driving me insane.. I added some code so if first artno is empty then it should skip it and add the next row instead...so if i leave the first row blank it stores the second row to the DB.. This indicates that my $i loop is working..
So why does it only save 1 row???
New code:
Code: Select all
if($insert_stmt = $mysqli->prepare("INSERT INTO RegisterFPCargo (sID, artNo, lotNo, productionDate, bestbeforeDate, qty) VALUES (?, ?, ?, ?, ?, ?)")) {
for($i = 1; $i <= $NumberofRows ; $i++){
if($_POST['artNo'.$i] <> "") {
$insert_stmt->bind_param('issssi', $tID, $_POST['artNo'.$i], $_POST['lotNo'.$i], $_POST['pDate'.$i], $_POST['bbDate'.$i], $_POST['qty'.$i]);
echo $_POST['artNo'.$i]."<br>"; //Debug
$insert_stmt->execute();
}
}
}
echo $tID;
the full code of the page:
Code: Select all
<?php
include_once(__DIR__."/../LoginSystem/Includes/DBConnect.php");
include_once(__DIR__.'/../LoginSystem/Includes/FunctionsToInclude.php');
sec_session_start();
if(login_check($mysqli) == true) {
require(__DIR__.'/../Mainpage/newpage_lib.php');
require(__DIR__."/../LoginSystem/Includes/DBConnect.php");
date_default_timezone_set("Europe/Stockholm");
$CurrentDateTime=date("Y-m-d H:i:s"); //create date time
$Current=date("Y-m-d"); //Används till mindate
$User=$_SESSION['userName'];
$UserID=$_SESSION['userID'];
$Language="Swedish";
$Submitted=$_POST['Submitted'];
$NumberofRows=20;
if (!$mysqli->set_charset("utf8")) { } //Sätt DB charset till utf8.
if ($stmt = $mysqli->prepare("SELECT
cID, userJob, userGroup FROM Users WHERE userID = ?")) {
$stmt->bind_param('i', $UserID);
$stmt->execute();
$stmt->bind_result($UsercID, $UserJob, $UserGroup);
$stmt->store_result();
$stmt->fetch();
$stmt->close();
}
if ($UserGroup == "Admin"){
$homepage = new Page();
ob_start();
if (!isset($Submitted)) {
?>
<table>
<form id="RegisterRPShipping" name="RegisterRPShipping" form accept-charset="utf-8" method="post" action="RegisterRPShipping.php">
<tr>
<td style="background-color: #80ffff"; FONT COLOR="#000000"; colspan="2";><b>Report shipped ready product</b></td>
</tr>
<tr>
<td style="background-color: #b3ffff"; FONT COLOR="#000000"; colspan="2";><i>This action reports the departure of products from the plant.</i></td>
</tr>
<tr>
<td style="background-color: white;"><FONT COLOR="#000000"><b><?php echo "The Truck ID or Container Number:";?></b></FONT></td>
<td style="background-color: white;"><input name="shipmentID" type="text" id="shipmentIDID" style="width:300px; height:18px;" value="" required /></td>
</tr>
<tr>
<td style="background-color: white;"><FONT COLOR="#000000"><?php echo "This is a:";?></FONT></td>
<td style="background-color: white;"><select name="shipmentType" id="shipmentTypeID" style="width:300px; height:18px;">
<option value="0" selected>Truck</option>
<option value="1">Container</option>
<option value="2">Other Transport</option>
</td>
</tr>
<tr>
<td style="background-color: white;"><FONT COLOR="#000000"><b><?php echo "Expected Arrival Date:";?></b></FONT></td>
<td style="background-color: white;"><input name="arrivalDate" type="date" id="arrivalDateID" min="<?php echo $Current;?>" style="width:300px; height:18px;" value="" required /></td>
</tr>
<tr>
<td style="background-color: #b3f40f"; FONT COLOR="#000000"; colspan="2";><b>Content.</b></td>
</tr>
<tr>
<td style="background-color: #b3f40f"; FONT COLOR="#000000"; colspan="2";><i>Please add the content of the Truck / Container.</i></td>
</tr>
</table>
<table>
<tr>
<th style="background-color: white; width:50px;"><FONT COLOR="#000000"><b><?php echo "ArtNo.";?></b></FONT></td>
<th style="background-color: white; width:100px;"><FONT COLOR="#000000"><b><?php echo "LotNo";?></b></FONT></td>
<th style="background-color: white; width:120px;"><FONT COLOR="#000000"><b><?php echo "ProductionDate";?></b></FONT></td>
<th style="background-color: white; width:140px;"><FONT COLOR="#000000"><b><?php echo "BestBeforeDate";?></b></FONT></td>
<th style="background-color: white; width:120px;"><FONT COLOR="#000000"><b><?php echo "Qty (Kg)";?></b></FONT></td>
</tr>
<?php
for($i = 1; $i <= $NumberofRows ; $i++){
?>
<tr>
<td style="background-color: white;"><input name="<?php echo "artNo".$i ?>" type="text" id="<?php echo "artNoID".$i ?>" style="width:50px; height:18px;" value="" /></td>
<td style="background-color: white;"><input name="<?php echo "lotNo".$i ?>" type="text" id="<?php echo "lotNoID".$i ?>" style="width:100px; height:18px;" value="" /></td>
<td style="background-color: white;"><input name="<?php echo "pDate".$i ?>" type="date" id="<?php echo "pDateID".$i ?>" style="width:140px; height:18px;" value="" /></td>
<td style="background-color: white;"><input name="<?php echo "bbDate".$i ?>" type="date" id="<?php echo "bbDateID".$i ?>" style="width:140px; height:18px;" value="" /></td>
<td style="background-color: white;"><input name="<?php echo "qty".$i ?>" type="number" id="<?php echo "qtyID".$i ?>" min="2016-09-11" style="width:100px; height:18px;" value="" /></td>
</tr>
<?php } ?>
<tr>
<td style="background-color: white;"><b> </b></td>
</tr>
<tr>
<input type="hidden" name="Submitted" value="TRUE"/>
<td style="background-color: white;"><input type="submit" name="Submit" value="<?php echo "Send"; ?>"/> </td>
</tr>
</form>
</table>
<?php
} else { //Efter att man tryckt på post knappen
//print_r($_POST);
$shipmentID=$_POST['shipmentID'];
$shipmentType=$_POST['shipmentType'];
$arrivalDate=$_POST['arrivalDate'];
$didArrive = false;
//$Current
//Spara trippen i databasen
//Ta reda på tripID
//Lagra innehållet i innehållsdatabasen med tripid som key
// Spara trippen i databasen och ta reda på högsta(ID)
if (!$mysqli->set_charset("utf8")) {}
if($insert_stmt = $mysqli->prepare("INSERT INTO RegisterTransport (transportID, transportType, departureDate, arrivalDate, didArrive) VALUES (?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('sissi', $shipmentID, $shipmentType, $Current, $arrivalDate, $didArrive);
$insert_stmt->execute();
if($stmt = $mysqli->prepare("SELECT tID FROM RegisterTransport ORDER BY tID DESC LIMIT 1") ){
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($tID);
$stmt->fetch();
}
} else { die("Database is down. Shipment was not saved. Please try again later.");}
if($insert_stmt = $mysqli->prepare("INSERT INTO RegisterFPCargo (sID, artNo, lotNo, productionDate, bestbeforeDate, qty) VALUES (?, ?, ?, ?, ?, ?)")) {
for($i = 1; $i <= $NumberofRows ; $i++){
if($_POST['artNo'.$i] <> "") {
$insert_stmt->bind_param('issssi', $tID, $_POST['artNo'.$i], $_POST['lotNo'.$i], $_POST['pDate'.$i], $_POST['bbDate'.$i], $_POST['qty'.$i]);
echo $_POST['artNo'.$i]."<br>"; //Debug
$insert_stmt->execute();
}
}
}
echo $tID;
}
$content=ob_get_contents();
ob_end_clean();
$homepage->content = $content;
$homepage->Display();
} else { echo 'You are not authorized to Add or Change Company information. Please contact Your administrator.'; }
} else { echo 'You are not authorized to access this page, please login.'; }
?>
celauran wrote: mysqli_stmt::execute returns a boolean, which you can capture. If it's false, you can check mysql::error
How would i do that in the above code? I tried
if (!$mysqli->error) {
printf("Errormessage: %s\n", $mysqli->error);
}
but it didnt print anything...?
Please help me before my keyboard will pass trough my monitor
Thanks