用mysqli_fetch_row传递一个select到一个表



我有一个问题,传递的查询返回一些记录,我已经有一个选择,返回一个输入,但这只适用于一个记录,但不止一个需要显示,所以一切,查询带给我,我需要把它放在一个表。

<?php 
$conexion=mysqli_connect('localhost','root','mysql','venta3');
$continente=$_POST['continente'];
$sql1="SELECT cobro, debe,idEntrega FROM inventario Where (idCliente='$continente'AND (cantidadP>0 or cantidadM>0 OR cantidadG>0))";
$result2=mysqli_query($conexion,$sql1);
$cadena2="<select id='cobroDebe' name='cobroDebe' style='width:400px;'  class='chosen-choices2'>";
while ($ver2=mysqli_fetch_row($result2)) {
$cadena2=$cadena2."<option value='".utf8_encode($ver2[1])."'>".utf8_encode($ver2[1])."</option>";       
}
echo  $cadena2."</select>";

$result1=mysqli_query($conexion,$sql1);
$cadena1="<select id='cobroEntre' name='cobroEntre' style='width:400px;'  class='chosen-choices2'>";
while ($ver1=mysqli_fetch_row($result1)) {
$cadena1=$cadena1."<option value='".utf8_encode($ver1[0])."'>".utf8_encode($ver1[0])."</option>";       
}
echo  $cadena1."</select>";


$result3=mysqli_query($conexion,$sql1);
$cadena3=" ";
while ($ver3=mysqli_fetch_row($result3)) {
$cadena3=$cadena3."<input type='hidden' id='idEntrega' name='idEntrega'
value='".utf8_encode($ver3[2])."'>";
}
echo  $cadena3;
?>

我不打算为你写完整的解决方案,但我将向你展示如何使第一个选择安全地从数据库中填充,并从中学习,你可以做剩下的。

$conexion=mysqli_connect('localhost','root','mysql','venta3');
$continente=$_POST['continente'];
// Build the query with ? to prepare later
$sql1="SELECT cobro, debe,idEntrega FROM inventario Where (idCliente=? AND (cantidadP>0 or cantidadM>0 OR cantidadG>0))";
// Prepare the statement
$stmt = $conexion->prepare($sql1);
// Bind the params
$stmt->bind_param("s", $continente);
// Attempt to execute
if ($stmt->execute()) {
// if successful, get result
$result = $stmt->get_result();
// If query brought any rows
if ($result->num_rows > 0) {
// build the select
$cadena2="<select id='cobroDebe' name='cobroDebe' style='width:400px;'  class='chosen-choices2'>";
// Fetch assoc array from the result
while ($ver2 = $result->fetch_assoc()) {
// use it
$cadena2=$cadena2."<option value='". $ver2[1] ."'>". $ver2[1] ."</option>";
}
echo  $cadena2."</select>";
}
// if execution failed - echo the error
} else {
echo $stmt->error;
}
// free result and close statement
$result->free_result();
$stmt->close();
// You can reuse $stmt and $result because we've now freed them

注意while循环$ver2[1]可能不会给你带来任何信息。您必须查看db返回的是什么。我会使用$ver2['cobro'], $ver2['debe'], $ver2['idEntrega']

最新更新