donte1234567 2013-11-04 15:55
浏览 32

选择外部表名称以下拉列表并插入到仅具有id的其他表中

I have done some database normalization and made for the product table a foreign producttype table because of the fixed value's. Now i run into some php form problems.

I am trying to make work: adding a product and make a producttype option (producttypes from pull-down list) and store only the producttypeid to the product record.

My tables:

product (productname, producttype)

producttype (producttypeid, producttype

  • Thanks for supporting me! Still i cant get the pulldown items working in the form and when only the productname is given and submitted i get the message "1 record addedError: No database selected" this results in a new record with producttype blank.

There are two records into the producttype table database that should been showed in the pull-down. My updated code is as follow

<?php
// connection with database
include('connect.php');

// check page request
if (isset($_POST["submit"])){

// query insert to product
$sql="INSERT INTO product (productname, producttype)
VALUES('$_POST[productname]','$_POST[producttype]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

// query select from producttype
$query="SELECT producttypeid, producttype FROM producttype";
$result = mysql_query($query) or die ("Error: " . mysql_error());

// store options in array
$product_types = array();
while (list($id, $name,) = mysql_fetch_row($result))
{
$product_types[$id] = $name;
}
// safety against cross-site scripting 
function h($str)
{
return htmlentities($str, ENT_QUOTES, "UTF-8");
}
}?>

<!-- html input form !-->
<html>
<body>
<form action="addproduct.php" method="post">
<input type="hidden" name="submit" value="1">
Productname: <input type="text" name="productname"></br>
Producttype: <select>
<?php foreach($product_types as $id => $label): ?>
<option value="<?php echo h($id); ?>"><?php echo h($label); ?></option>
<?php endforeach; ?>
</select></br>
<input type="submit">
</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanmei2459 2013-11-04 16:04
    关注

    You have only one <option> tag within your <select>. To display all product types in the select menu, you'll need to store the options in an array:

    $product_types = array();
    while (list($id, $name,) = mysql_fetch_row($result))
    {
        $product_types[$id] = $name;
    }
    

    Then to display the options:

    <select>
        <?php foreach($product_types as $id => $label): ?>
            <option value="<?php echo h($id); ?>"><?php echo h($label); ?></option>
        <?php endforeach; ?>
    </select>
    

    The h function is used to escape the output so that your code won't be vulnerable to cross-site scripting:

    function h($str)
    {
        return htmlentities($str, ENT_QUOTES, "UTF-8");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line