我有一个动态形式,其中输入名称=''值来自数据库,但是如何在函数中定义它呢?或者您对如何编写此代码有更好的建议。
<?php
$query = "SELECT * FROM product_types";
$input_product_attribute = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($input_product_attribute)) {
$product_type_ID = $row['Product_type_ID'];
$label_name = $row['Product_type_label_name'];
$product_type_attribute = $row['Product_type_attribute'];
$label_comment = $row['Product_type_label_comment'];
?>
<div id='div_<?php echo $product_type_ID ?>' class="divParameter" style="display: none;">
<div class="form-group">
<label for='<?php echo $label_name ?>'><?php echo $label_name ?></label>
<input id='<?php echo $label_name ?>' type="text" name='<?php echo $product_type_attribute ?>' class="form-control">
<label><?php echo $label_comment ?></label>
</div>
</div>
<?php } ?>
<?php
function createRows(){
if (isset($_POST['submit'])) {
global $connection;
file_put_contents('debug.txt', json_encode($_POST)."\n", FILE_APPEND );
$productType = $_POST['select_box'];
$productAttribute = $_POST['?']; //PROBLEM!!
$productType = mysqli_real_escape_string($connection, $productType );
$productAttribute = mysqli_real_escape_string($connection, $productAttribute );
$query = "INSERT INTO products(Product_type,Product_size) ";
$query .= "VALUES ('$productType', '$productAttribute') ";
}
?>
答案 0 :(得分:1)
您可以按照以下方式创建表格和php代码
HTML
<label for='<?php echo $label_name ?>'><?php echo $label_name ?></label>
//solution to your problem, see name attribute
<input name='dynamic_values[<?php echo $product_type_attribute ?>]' id='<?php echo $label_name ?>' type="text" class="form-control">
PHP
<?php
function createRows(){
if (isset($_POST['submit'])) {
global $connection;
file_put_contents('debug.txt', json_encode($_POST)."\n", FILE_APPEND );
$productType = $_POST['select_box'];
//$productAttribute = $_POST['?']; //PROBLEM!! //problem solved below
$productType = mysqli_real_escape_string($connection, $productType );
//$productAttribute = mysqli_real_escape_string($connection, $productAttribute );
//solution to your problem
if( !empty($_POST['dynamic_values']) ) {
foreach( $_POST['dynamic_values'] as $val ) {
$query = "INSERT INTO products(Product_type,Product_size) ";
$val = mysqli_real_escape_string($connection, $val );
$query .= "VALUES ('$productType', '$val') ";
}
}
}
}
?>
如果您遇到任何问题,请在评论中告诉我。
答案 1 :(得分:0)
我这样做:
function update_products($products), function insert_products($products), unction find_products_by_id($id, $options=[])....
示例
function insert_products($products) {
global $db;
$errors = validate_products($products);
if(!empty($errors)) {
return $errors;
}
$sql = "INSERT INTO products ";
$sql .= "(cat_id, name, code, content) ";
$sql .= "VALUES (";
$sql .= "'" . db_escape($db, $products['cat_id']) . "',";
$sql .= "'" . db_escape($db, $products['name']) . "',";
$sql .= "'" . db_escape($db, $products['code']) . "',";
$sql .= "'" . db_escape($db, $products['content']) . "'";
//echo $sql;
$sql .= ")";
$result = mysqli_query($db, $sql);
// For INSERT statements, $result is true/false
if($result) {
return true;
} else {
// INSERT failed
echo mysqli_error($db);
db_disconnect($db);
exit;
}
}