如何使用下拉列表,使用select查询,根据所选选项(php/mysql)更新文本字段
例如:如何使用下拉列表,使用select查询,根据所选选项(php/mysql)更新文本字段,php,mysql,Php,Mysql,例如: echo '<select>'; $mysqli = new mysqli( "localhost", "root", "user", "db" ); $con = $mysqli; if( $mysqli->connect_errno ) { die("Failed to connect to MySQL: (" . $mysqli->connect_errno
echo '<select>';
$mysqli = new mysqli( "localhost", "root", "user", "db" );
$con = $mysqli;
if( $mysqli->connect_errno ) {
die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
$username = $_SESSION['user'];
$selectStatement = "SELECT id FROM purchase_order WHERE username = ?";
$statement1 = $mysqli->prepare( $selectStatement );
$statement1->bind_param( "s", $username );
$statement1->execute();
$statement1->bind_result( $id );
while( $statement1->fetch() ) {
echo '<option value="' . $id . '">' . $id . '</option>';
}
echo '</select>';
echo';
$mysqli=newmysqli(“localhost”、“root”、“user”、“db”);
$con=$mysqli;
如果($mysqli->connect\u errno){
die(“未能连接到MySQL:(“$mysqli->connect_errno.”)、“$mysqli->connect_error”);
}
$username=$\会话['user'];
$selectStatement=“从采购订单中选择id,其中用户名=?”;
$statement1=$mysqli->prepare($selectStatement);
$statement1->bind_参数(“s”,$username);
$statement1->execute();
$statement1->bind_结果($id);
而($statement1->fetch()){
回显“.$id.”;
}
回声';
现在让我们假设我有数量、描述、金额、小计、税、总计的文本字段,一旦我在下拉列表中选择ID2,我希望文本字段中的值显示数据库中第二个ID的数据
$username = $_SESSION['user'];
$selectStatement = "SELECT * FROM purchase_order WHERE username = ?";
$statement1 = $mysqli->prepare( $selectStatement );
$statement1->bind_param( "s", $username );
$statement1->execute();
$statement1->bind_result( $id );
$statement1->fetch();
echo '<input type="text" value="' . $quantity . '" id="quantity" name="quantity" readonly>';
echo '<input type="text" value="' . $description . '" id="description" name="description" readonly>';
echo '<input type="text" value="' . $amount . '" id="amount" name="amount" readonly>';
etc.
$username=$\u会话['user'];
$selectStatement=“从采购订单中选择*,其中用户名=?”;
$statement1=$mysqli->prepare($selectStatement);
$statement1->bind_参数(“s”,$username);
$statement1->execute();
$statement1->bind_结果($id);
$statement1->fetch();
回声';
回声';
回声';
等
我假设这是一个$\u GET或$\u POST解决方案。我不知道该怎么做。请提供帮助。为
指定一个名称,以便您在提交表单时可以参考该名称
然后使用此POST参数查找表中该行的详细信息
echo '<form method="POST">';
echo '<select name="po_id">';
$mysqli = new mysqli( "localhost", "root", "user", "db" );
$con = $mysqli;
if( $mysqli->connect_errno ) {
die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
$username = $_SESSION['user'];
$selectStatement = "SELECT id FROM purchase_order WHERE username = ?";
$statement1 = $mysqli->prepare( $selectStatement );
$statement1->bind_param( "s", $username );
$statement1->execute();
$statement1->bind_result( $id );
while( $statement1->fetch() ) {
echo '<option value="' . $id . '">' . $id . '</option>';
}
echo '</select>';
if (isset($_POST['po_id'])) {
$username = $_SESSION['user'];
$selectStatement = "SELECT quantity, description, amount FROM purchase_order WHERE username = ? AND id = ?";
$statement1 = $mysqli->prepare( $selectStatement );
$statement1->bind_param( "si", $username, $_POST['po_id'] );
$statement1->execute();
$statement1->bind_result( $quantity, $description, $amount );
$statement1->fetch();
echo '<input type="text" value="' . $quantity . '" id="quantity" name="quantity" readonly>';
echo '<input type="text" value="' . $description . '" id="description" name="description" readonly>';
echo '<input type="text" value="' . $amount . '" id="amount" name="amount" readonly>';
}
echo '</form>';
echo';
回声';
$mysqli=newmysqli(“localhost”、“root”、“user”、“db”);
$con=$mysqli;
如果($mysqli->connect\u errno){
die(“未能连接到MySQL:(“$mysqli->connect_errno.”)、“$mysqli->connect_error”);
}
$username=$\会话['user'];
$selectStatement=“从采购订单中选择id,其中用户名=?”;
$statement1=$mysqli->prepare($selectStatement);
$statement1->bind_参数(“s”,$username);
$statement1->execute();
$statement1->bind_结果($id);
而($statement1->fetch()){
回显“.$id.”;
}
回声';
如果(isset($_POST['po_id'])){
$username=$\会话['user'];
$selectStatement=“从采购订单中选择数量、说明、金额,其中用户名=?和id=?”;
$statement1=$mysqli->prepare($selectStatement);
$statement1->bind_参数(“si”、$username、$_POST['po_id']);
$statement1->execute();
$statement1->bind_结果($quantity,$description,$amount);
$statement1->fetch();
回声';
回声';
回声';
}
回声';
如果您想在不提交表单的情况下完成此操作,则需要学习JavaScript和AJAX。这比我在这个答案中愿意教的要多。如果您希望在不提交表单的情况下实现这一点,您需要使用JavaScript。