表单不会在数据库中提交
Form wont submit in database
对我来说一切似乎都很好,但表单不会提交到我的数据库中。我不知道还能做什么,我没有得到任何错误或任何东西,所以我真的很困惑。我错过了什么?这让我真的很着迷
这是我的插入函数:
function insertDb($table, $data, $validation = null) {
global $link;
foreach($data as $key => $value) {
$data[$key] = trim($value);
}
if(!empty($validation)) {
foreach($validation as $field => $rules) {
foreach($rules as $rule_type => $rule_value) {
if($rule_type == 'unique') {
$check_exists = find('first', $table, array(array('field' => $field, 'operator' => '=', 'value' => $data[$field])));
if(!empty($check_exists)) {
$errors['error'][$field][$rule_type] = $field.' must be unique';
}
} elseif($rule_type == 'min_lenght') {
if(strlen($data[$field]) < $rule_value) {
$errors['error'][$field][$rule_type] = $field.' requires at least ' . $rule_value . ' characters';
}
}
}
}
if(isset($errors)) {
return $errors;
}
}
foreach($data as $key => $value) {
$insert_fields[] = $key;
$insert_values[] = '"' . $value . '"';
}
$query = 'INSERT INTO ' . $table. ' ('.implode(',', $insert_fields).') VALUES ('.implode(',', $insert_values).')';
mysqli_query($link, $query);
return true;
}
这是主要代码。表格对我来说很好,我不知道这里出了什么问题
<?php
include "configuration.php";
if(isset($_POST['submit'])) {
$result = insertDb('products',
array('category' => $_POST['name'],
'name' => $_POST['name'],
'price' => $_POST['price'],'active' => $_POST['active'],
'condition' => $_POST['condition']));
if($result === true) {
header("Location: products.php");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<style type="text/css"><?php include 'style.css'; ?></style>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
<?php include 'style.css'; ?>
</style>
</head>
<body>
<?php include "header.php";?>
<div class="container">
<form action="" method="post">
<h3>Products:</h3>
<div class="form-group">
<label for="category">Category: *</label>
<select name="category">
<?php
$res = mysqli_query($link,"select name from categories");
while ( $row=mysqli_fetch_assoc($res)) {
echo "<option>";
echo $row["name"];
echo "</option>";
} ?>
</select>
</div>
<div class="form-group">
<label for="name">Name: *</label>
<input type="text" name="name" id="name" value="<?php echo @$_POST['name']; ?>" />
<br/>
</div>
<div class="form-group">
<label for="price">Price: *</label>
<input type="text" name="price" id="price" value="<?php echo @$_POST['price']; ?>" />
</div>
<div class = "form-group">
<label for="active">Active: *</label>
<input type="checkbox" name="active" id="active" value="active" />
</div>
<div class = "form-group">
<label for = "condition">Condition: *</label>
<input type = "radio" name="condition" id="used" value="used" /><label for="used">Used</label>
<input type = "radio" name="condition" id="new" value="new" /><label for="new">New</label>
</div>
<input type="submit" name="submit" value="Add">
</form>
</div>
</body>
</html>
我的产品 table 包含:
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`active` varchar(2) DEFAULT NULL,
`condition` varchar(2) DEFAULT NULL,
PRIMARY KEY (`id`)
)
请检查您的数据库 table 列大小。您已经定义了活动的列大小,并且条件的长度仅为 2。但是当 运行 查询时,相应字段的传递值大小不止于此。另一个问题字段名称condition
更改为任何其他名称。您也不应该对整数类型字段使用“”。
对我来说一切似乎都很好,但表单不会提交到我的数据库中。我不知道还能做什么,我没有得到任何错误或任何东西,所以我真的很困惑。我错过了什么?这让我真的很着迷
这是我的插入函数:
function insertDb($table, $data, $validation = null) {
global $link;
foreach($data as $key => $value) {
$data[$key] = trim($value);
}
if(!empty($validation)) {
foreach($validation as $field => $rules) {
foreach($rules as $rule_type => $rule_value) {
if($rule_type == 'unique') {
$check_exists = find('first', $table, array(array('field' => $field, 'operator' => '=', 'value' => $data[$field])));
if(!empty($check_exists)) {
$errors['error'][$field][$rule_type] = $field.' must be unique';
}
} elseif($rule_type == 'min_lenght') {
if(strlen($data[$field]) < $rule_value) {
$errors['error'][$field][$rule_type] = $field.' requires at least ' . $rule_value . ' characters';
}
}
}
}
if(isset($errors)) {
return $errors;
}
}
foreach($data as $key => $value) {
$insert_fields[] = $key;
$insert_values[] = '"' . $value . '"';
}
$query = 'INSERT INTO ' . $table. ' ('.implode(',', $insert_fields).') VALUES ('.implode(',', $insert_values).')';
mysqli_query($link, $query);
return true;
}
这是主要代码。表格对我来说很好,我不知道这里出了什么问题
<?php
include "configuration.php";
if(isset($_POST['submit'])) {
$result = insertDb('products',
array('category' => $_POST['name'],
'name' => $_POST['name'],
'price' => $_POST['price'],'active' => $_POST['active'],
'condition' => $_POST['condition']));
if($result === true) {
header("Location: products.php");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<style type="text/css"><?php include 'style.css'; ?></style>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
<?php include 'style.css'; ?>
</style>
</head>
<body>
<?php include "header.php";?>
<div class="container">
<form action="" method="post">
<h3>Products:</h3>
<div class="form-group">
<label for="category">Category: *</label>
<select name="category">
<?php
$res = mysqli_query($link,"select name from categories");
while ( $row=mysqli_fetch_assoc($res)) {
echo "<option>";
echo $row["name"];
echo "</option>";
} ?>
</select>
</div>
<div class="form-group">
<label for="name">Name: *</label>
<input type="text" name="name" id="name" value="<?php echo @$_POST['name']; ?>" />
<br/>
</div>
<div class="form-group">
<label for="price">Price: *</label>
<input type="text" name="price" id="price" value="<?php echo @$_POST['price']; ?>" />
</div>
<div class = "form-group">
<label for="active">Active: *</label>
<input type="checkbox" name="active" id="active" value="active" />
</div>
<div class = "form-group">
<label for = "condition">Condition: *</label>
<input type = "radio" name="condition" id="used" value="used" /><label for="used">Used</label>
<input type = "radio" name="condition" id="new" value="new" /><label for="new">New</label>
</div>
<input type="submit" name="submit" value="Add">
</form>
</div>
</body>
</html>
我的产品 table 包含:
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`active` varchar(2) DEFAULT NULL,
`condition` varchar(2) DEFAULT NULL,
PRIMARY KEY (`id`)
)
请检查您的数据库 table 列大小。您已经定义了活动的列大小,并且条件的长度仅为 2。但是当 运行 查询时,相应字段的传递值大小不止于此。另一个问题字段名称condition
更改为任何其他名称。您也不应该对整数类型字段使用“”。