мне кажется, что все в порядке, но форма не будет отправлена в мою базу данных.Я не знаю, что еще делать, я не получаю никаких ошибок или чего-то еще, поэтому я действительно запутался.Я что-то упустил?Это делает меня действительно одержимым
Вот моя функция вставки:
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>
Моя таблица продуктов содержит:
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`)
)