【发布时间】:2017-12-14 12:45:50
【问题描述】:
我有一个 CRUD 系统,并为其中三个字段创建了验证,以确保它们不为空,这样可以完美运行。我想通过确保 ID 不存在来添加进一步验证。
这里是代码..
$required = array('id', 'name', 'family');
$noDouble = true;
$mysqli = new mysqli("localhost", "username", "password", "database");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}else{echo "connected";}
$query1 = mysqli_query($mysqli, "SELECT * FROM products WHERE id=='" . $product->id ."'");
if (!$query1){
$noDouble = false;
}
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])){
$error = true;
}
}
if($_POST && $error) {
echo "<div class='alert alert-danger'>All fields required</div>";
}elseif($_POST && $noDouble) {
echo "<div class='alert alert-danger'>That part number already exists</div>";
}
// if the form was submitted
elseif ($_POST){
// set product property values
$product->id = $_POST['id'];
$product->name = $_POST['name'];
$product->family = $_POST['family'];
//$product->number = $_POST['number'];
$product->description = $_POST['description'];
$product->ext_description = $_POST['ext_description'];
$product->PRF = $_POST['PRF'];
if(isset($_POST['publish'])){
//$stok is checked and value = 1
$published = $_POST['publish'];
$product->publish = $_POST['publish'];
}
else{
//$publish is not checked and value=0
$published = 0;
$product->publish = 0;
}
//$product->publish = $_POST['publish'];
/* $product->category_id = $_POST['category_id']; */
// create the product
if($product->create()){
echo "<div class='alert alert-success'>Product was created.</div>";
}
}
但是,查询的 if 语句似乎不起作用,因为它继续告诉我部件号存在。我的查询有问题吗?我也尝试用$_POST['id'] 替换$product->id,但这会导致更多错误。任何帮助将不胜感激。
【问题讨论】:
标签: php mysql forms validation