After learning how to setup a environment with html, js, ajax, php and mysql I'm able to insert single instances into a table.
I'm trying to get it working with multiple entries into the table for quite a while now. I was reading several documentation pages and recommendations on here but couldn't really figure out what I'm missing.
To clarify: What I'm trying to do is to insert 2 of the same entity. I input SafetyStock
and LotSize
of the Material Pack
thereupon, insert another Material named PET with different
There is no problem with getting the variables from ajax but i don't get the $stmt->bind_param
line. What do I have to write there to make the code running?
I tried to just have two lines of stmt
binding, but then it only wrote the last entry into the database. Another thing I did was execute the stmt
and then overwrite the variables and then do execute again, wasn't working as well.
my actual insert.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "scm";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind settingssupplychaincomponents
$stmt = $conn->prepare("INSERT INTO test1 (settingsSupplyChainComponentsId, settingsSupplyChainId, componentId, safetyStockW, lotSizeW)
VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)");
$stmt->bind_param("iiiii", $settingsSupplyChainComponentsId, $settingsSupplyChainId, $pack, $sspack, $lspack); //Pack
// set parameters for components
$settingsSupplyChainComponentsId = '';
$settingsSupplyChainId = $_POST['ssci'];
// set parameters for pack
$pack = 1;
$sspack = $_POST['sspack'];
$lspack = $_POST['lspack'];
// set parameters for PET
$pet = 2;
$sspet = $_POST['sspet'];
$lspet = $_POST['lspet'];
$values = array();
$stmt->execute($values);
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
you'll need to repeat the variables that should be inserted in both rows.
$stmt->bind_param("iiiiiiiiii",
$settingsSupplyChainComponentsId, $settingsSupplyChainId, $pack, $sspack, $lspack,
$settingsSupplyChainComponentsId, $settingsSupplyChainId, $pet, $sspet, $lspet);
I had this problem, there was no way of doing multiple entries, instead I used several $stmt, below is an example:
$sql = "SELECT TOP 1 SALDOS_DEUDA.FECHA_SALDOS FROM [dbo].[SALDOS_DEUDA]
WHERE FECHA_SALDOS = (SELECT MAX(FECHA_SALDOS) FROM dbo.SALDOS_DEUDA);";
$sql1 = "SELECT SUM(SALDO_DEUDA) / 1000000 AS SALDO_TOTAL FROM SALDOS_DEUDA
WHERE FECHA_SALDOS = (SELECT MAX(FECHA_SALDOS) FROM dbo.SALDOS_DEUDA);";
$sql2 = "SELECT (SUM(CASE WHEN TIPO_DEUDA = 'E' THEN SALDO_DEUDA ELSE 0 END) / SUM(SALDO_DEUDA)) AS PORCENTAJE_EXTERNO FROM SALDOS_DEUDA
WHERE FECHA_SALDOS = (SELECT MAX(FECHA_SALDOS) FROM dbo.SALDOS_DEUDA);";
$sql3 = "SELECT SUM(MONTO) AS TASA_FIJA FROM dbo.RIESGO_MERCADO_TASA
WHERE ANIO = (SELECT MAX(ANIO) FROM dbo.RIESGO_MERCADO_TASA)
AND MES = (SELECT MAX(MES) FROM dbo.RIESGO_MERCADO_TASA
WHERE ANIO = (SELECT MAX(ANIO) FROM dbo.RIESGO_MERCADO_TASA) )
AND TIPOTASA_ID = 1;";
$sql4 = "SELECT SUM(MONTO) AS MONEDA_LOCAL FROM dbo.RIESGO_MERCADO_TASA
WHERE ANIO = (SELECT MAX(ANIO) FROM dbo.RIESGO_MERCADO_TASA)
AND MES = (SELECT MAX(MES) FROM dbo.RIESGO_MERCADO_TASA
WHERE ANIO = (SELECT MAX(ANIO) FROM dbo.RIESGO_MERCADO_TASA) )
AND MONEDA_ID = 13;";
$sql5 = "SELECT (SUM(P.PROMEDIO * P.SALDO) / SUM(SALDO)) AS CPP
FROM dbo.PROMEDIO_PONDERADO P
INNER JOIN dbo.CG_CATEGORIA_PONDERADO C ON P.CATEGORIA_ID = C.CATEGORIA_ID
WHERE FECHA = (SELECT MAX(FECHA) FROM dbo.PROMEDIO_PONDERADO)
AND C.TIPO = 1;";
$stmt = sqlsrv_query( $conn, $sql);
$stmt1 = sqlsrv_query( $conn, $sql1);
$stmt2 = sqlsrv_query( $conn, $sql2);
$stmt3 = sqlsrv_query( $conn, $sql3);
$stmt4 = sqlsrv_query( $conn, $sql4);
$stmt5 = sqlsrv_query( $conn, $sql5);