I am quite new in php and mysql. I was trying to insert multiple (unknown ) number of rows into mysql database. The data is posted into the table through a link - http://localhost:81/logdata.php?CtrlID=3842&DateTime=2017-05-18+11%3A45%3A23&Bat=50.2&LVSD=1&Indt=29.4&Outdt=32.8&submit
The following code works perfect as long as a single row is posted. But I have no idea how to insert several rows together and how the link should look like. Actually rows containing data are formed and stored in a Microcontroller. I am sending the data with the help of GPRS. The controller successfully sending one row at a time, the data is correctly recorded in mysql database. But I am struggling to send several rows together. I would highly appreciate any suggestion.
<?php
$servername = "localhost";
$username = "root";
$password = ""; //your pwd
$dbname = "mirzu";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
if($conn){
echo 'Successfully Connected database.';
}
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//if(isset($_GET['submit'])){ //
$ID = $_GET['CtrlID'];
$DateTime = $_GET['DateTime'];
$battery = $_GET['Bat'];
$LVSD = $_GET['LVSD'];
$IndoorT = $_GET['Indt'];
$OutdoorT = $_GET['Outdt'];
$totalCtrlID = sizeof($ID);
for($i=0;$i<$totalCtrlID;$i++) {
$InsCtrlID = [$ID][$i];
$InsDateTime = [$DateTime][$i];
$Insbattery = [$battery][$i];
$InsLVSD = [$LVSD][$i];
$InsIndoorT = [$IndoorT][$i];
$InsOutdoorT = [$OutdoorT][$i];
$query = "INSERT INTO btsdata (CtrlID,DateTime,Batt,LVSD,IndT,OutdT)".
"VALUES ('$InsCtrlID','$InsDateTime','$Insbattery','$InsLVSD','$InsIndoorT','$InsOutdoorT');";
}
if (mysqli_query($conn, $query)) {
echo "New record created successfully into database";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
//}
mysqli_close($conn);
?>
Two records the query will become:
INSERT INTO TABLE (column1, column2) VALUES ('data', 'data'), ('data', 'data')
Same as a more than two record.
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);