如何从MySQL表中动态获取选择标记的值 [英] How to dynamically get the value of a select tag from MySQL table
问题描述
感谢您的支持,我很抱歉如果这个问题看起来很简单,因为我对PHP很陌生。我试图做一个学校项目,我现在的问题是,我似乎无法得到下拉列表的值。我一直试图在每个论坛和一些类似的问题在这里搜索,但他们似乎只显示如何填充数据库值的下拉列表。
填充代码下拉菜单正在工作,但录制选定的值不适用于我。
这是我的下拉菜单的php代码:
< div class =panel>
< label class =panelname>类别:< / label>
< option>选择一个< / option>
<? //检索所有类别并添加到下拉菜单中
$ query ='SELECT category_id,name FROM category ORDER BY name ASC';
$ request = mysqli_query($ dbc,$ query);
while($ row = mysqli_fetch_array($ request,MYSQLI_NUM)){
$ cat_id = $ row [0];
echo< option value = \row [0] \;
//检查粘性:
if(isset($ _ POST ['category'])&&($ _POST ['category'] == $ row [0])){
echo'selected =选择';
// $ _ POST []
} echo> $ row [1]< / option> \\\
;
}
?>
< / select>
<? if(array_key_exists('category',$ add_product_errors))echo'< label class =error>'。 $ add_product_errors ['category']。 < /标签>; ?>
< / div>
以下是我的验证代码的一部分: - $ /
$ ($ _SERVER ['REQUEST_METHOD'] =='POST'){
//检查类别:
if(!isset($ _ POST ['category'])||!filter_var($ _ POST ['category'],FILTER_VALIDATE_INT,array('min_range'=> 1) )){
$ add_product_errors ['category'] ='请选择一个类别!';
}
..... //验证的其他代码(例如:if(empty($ add_product_errors)){继续查询})
我不知道什么似乎是问题,但是$ _POST ['category']中没有值保存到MySQL和I继续收到请选择类别错误。
谢谢大家的帮助。 :)
谢谢,我似乎已经解决了这个问题..
我好像删除了行[0]中的($)行,以便正确读取它。
仅在行[0]上放置$:
echo< option value = \row [ 0] \ ;
Thanks for dropping by and I'm sorry if this question seems to be easy as I'm kinda new to php. Im trying to do a school project and my problem now is that I cant seem to get the values of the dropdown. I have been trying to search in every forum and some similar questions in here but they seem to only show how i can populate the dropdown with database values.
The code for populating the dropdown menu is working, but recording the selected value is not working for me.
Here is my php code for the dropdown:
<div class="panel">
<label class="panelname">Category:</label>
<select name="category" class="signup_field">
<option>Select One</option>
<? // retrieve all the categories and add to the pull down menu
$query = 'SELECT category_id, name FROM category ORDER BY name ASC';
$request = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$cat_id = $row[0];
echo "<option value=\"row[0]\"";
//check for stickyness:
if (isset($_POST['category']) && ($_POST['category'] == $row[0])) {
echo ' selected="selected"';
//$_POST[]
} echo ">$row[1]</option>\n";
}
?>
</select>
<? if (array_key_exists('category', $add_product_errors)) echo '<label class="error">' . $add_product_errors['category'] . '</label>'; ?>
</div>
and here is my part of my code for validation: -
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for a category:
if (!isset($_POST['category']) || !filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$add_product_errors['category'] = 'Please select a category!';
}
..... // other codes for validation (Eg: if (empty($add_product_errors)) { proceed to query} )
I don't know what seems to be the problem but there are no values recorded in the $_POST['category'] for me to save into MySQL and I keep getting the Please select a category error.
Thank you all for your help. :)
Thanks, I seem to have solve the problem ..
I have seemed to have deleted the ($)row in the row[0] for it to be read properly. just put $ on the row[0]:
echo "<option value=\"row[0]\"";
这篇关于如何从MySQL表中动态获取选择标记的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!