gpt4 book ai didi

php - 如何更改php中的选项文本?

转载 作者:行者123 更新时间:2023-11-29 12:16:21 25 4
gpt4 key购买 nike

我尝试构建一个管理页面。管理员将填写一份表格以在数据库中添加新产品并将其显示在商店网站上。问题是当我尝试从保管箱中选择性别时,新产品没有添加到数据库的产品表中,如下所示:(我想选择性别,例如男孩)

管理页面和数据库结果:

enter image description here

我使用的代码:

$host = "";
$userMS = "";
$passwordMS = "";
$connection = mysql_connect($host,$userMS,$passwordMS) or die("Couldn't connect:".mysql_error());
$database = "projectDataBase";
$db = mysql_select_db($database,$connection) or die("Couldn't select database");

if (isset($_POST['sAddProduct']))
{

addNewProduct();
}else if(isset($_POST['delete']))
{
$Product_ID=$_POST['Product_ID'];
$mysqlquery="delete from Product where Product_ID= ".$Product_ID."";
mysql_query($mysqlquery);
echo "Deleted successfully";
echo("<FORM><INPUT Type='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>");
}else{
showForm();
}


// add new product
function addNewProduct()
{
$ProductName = $_POST['Product_Name'];
$ProductPrice = $_POST['Price'];
$Gender = $_POST['Gender_ID'];


//database query to add product
$insertStringProduct = "INSERT into Product(Product_Name, Price,Gender_ID)
VALUE('$ProductName', '$ProductPrice', '$Gender')";
$result = mysql_query($insertStringProduct);



echo ("<p1>Product added Successfully</p1>");
echo("<FORM><INPUT Type='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>");
}



//function for the form page
function showForm()
{
//First form for adding new product
$self = htmlentities($_SERVER['PHP_SELF']);
echo("<form action = '$self' method='POST'>
<fieldset>
<legend>Adding New Product</legend>
Product Name: <input name='Product_Name' type='text' size = '40'>
<br /><br />
Price: <input name='Price' type='text' size = '20'><br><br />


Gender:
<select name='Gender_Description'>
<option value = '%'> <-- select--></option>");

$dbQuary = " SELECT DISTINCT Gender_Description from Gender";
$result = mysql_query($dbQuary);

while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[0]</option>");
}
echo("
</select>


<br/><br/>
<input type='submit' name='sAddProduct' value = 'Add'/>
<input type='reset' value='Clear' />
</fieldset>

</form>");


}

结果(未添加任何内容)

但是,当我将代码更改为

 Gender: 
<select name='Gender_ID'>
<option value = '%'> <-- select--></option>");

$dbQuary = " SELECT DISTINCT Gender_ID from Gender";
$result = mysql_query($dbQuary);

正在运行

enter image description here

谁能帮我解决这个问题吗?

最佳答案

addNewProduct你在期待$_POST['Gender_ID']要设置。所以当然,<select name='Gender_Description'>不起作用,因为 Gender_Description != Gender_ID 。这也是为什么当你改变它时它确实有效。

我假设您想要实现的是显示性别描述,并且它仍然有效。为此,您需要 ID 和描述:

$dbQuary = " SELECT DISTINCT Gender_ID, Gender_Description from Gender";
$result = mysql_query($dbQuary);

while($row = mysql_fetch_row($result)){
echo("<option value ='$row[0]'> $row[1]</option>");
}

安全

您的代码极不安全。您正在使用mysql_*自 2013 年以来已弃用,并且您没有以任何方式清理输入,因此您的代码对 SQL 注入(inject)开放(可能存在于各种查询中;插入、更新、删除等,并允许数据泄漏、DOS ,以及可能的代码执行和数据删除/更改)。防止这种情况的首选方法是准备好的语句(使用 mysqli_*PDO )。它们使用起来并不困难,而且生成的代码也更好。

关于php - 如何更改php中的选项文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29731823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com