我想MySQL查詢,並保持在選擇下拉列表PHP的查詢,並保持在選定
<select name="selectjob"></select>
,這是MySQL查詢
SELECT repair_id FROM `repair_list` where status = 'work'
我想MySQL查詢,並保持在選擇下拉列表PHP的查詢,並保持在選定
<select name="selectjob"></select>
,這是MySQL查詢
SELECT repair_id FROM `repair_list` where status = 'work'
所有你需要做的是:
//db connection
mysql_connect("localhost","user","password");
mysql_select_db("database");
//query
$sql=mysql_query("SELECT repair_id FROM repair_list where status = 'work'");
if(mysql_num_rows($sql)){
$select= '<select name="selectjob">';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['repair_id'].'">'.$rs['repair_id'].'</option>';
}
}
$select.='</select>';
echo $select;
或者使用mysqli作爲我的天才朋友在建議的評論中:
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database')
or die ('Cannot connect to db');
$result = $conn->query("SELECT repair_id FROM repair_list where status = 'work'");
echo "<select name='selectjob'>";
while ($row = $result->fetch_assoc()) {
echo '<option value="'.$row['repair_id'].'">'.$row['repair_id'].'</option>';
}
echo "</select>";
?>
我的朋友開始生活在當今世界。 'mysql_ *'現在已被棄用。使用'mysqli_ *'或'PDO'。 –
是好的,但價值是不是在我的選擇下拉列表 –
@NonBrazilian,這是一個良好的開端。嘗試進一步調試好友! – Guns
這將引導您完成:
$status = 'work';
$query = $mysqli->prepare("SELECT repair_id FROM `repair_list` where status = ?");
$query->bind_param("s",$status);
if (!$query->execute())
{
$flag = false;
}
$query->bind_result($repair_id);
$select= '<select name="selectjob">';
while($query->fetch()){
$select.='<option value="'.$repair_id.'">'.$repair_id.'</option>';
}
}
$select.='</select>';
加1爲準備好的語句。你應該總是使用準備好的語句! – Guns
select.php
$stmt = $dbh->prepare("SELECT repair_id FROM repair_list where status ='work'");
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$repair_id[] = array('repair_id' => $selected_row['repair_id ']);
}
echo json_encode($repair_id, JSON_UNESCAPED_UNICODE);
}
JS
$.ajax({
type: 'POST',
url: 'select.php',
dataType: "json",
success: function(data) {
var select = ('#select');
for (var i = 0; i < data.length; i++) {
$(select).append('<option id=' + data[i].repair_id + ' value="' + data[i].repair_id + '">' + data[i].repair_id + '</option>');
}
},
error: function(data) {
}
});
html
<select name="selectjob" id="select"></select>
嘗試使用AJAX
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT repair_id FROM repair_list where status = 'work'";
$result = $conn->query($sql);
if ($result->num_rows > 0):
$html ='';
while($row = $result->fetch_assoc()) {
$html.='<option value="'.$row['repair_id'].'">'.$row['repair_id'].'</option>';
}
echo $html;
else:
echo "0 results";
endif;
$conn->close();
你是不是清楚,您保存所選repair_id。通過更改$ selectedValue變量的值,您可以顯示所需的選定值。
<select name="selectjob"></select>
<?php
$query = mysql_query("SELECT repair_id FROM `repair_list` where status = 'work'");
$selectedValue = 2; //some id which is selected
while ($row = mysql_fetch_array($query)) { ?>
<option value="<?php echo $row['repair_id']?>" <?php echo ($row['repair_id'] == $selectedValue)? ' selected="selected"': ''?>><?php echo $row['repair_id'];?></option>
<?php } ?>
你的問題不清楚。你能更清楚明確嗎? –
你的問題根本不清楚 –
好的,我想創建下拉列表,我的值在下拉列表中形成 mysql查詢例如:SELECT repair_id FROM'repair_list' where status ='work' –