php和MYSQL中的逻辑问题

时间:2017-09-05 20:04:19

标签: php html mysqli

所以我从头开始做一个演示网络商店,我似乎无法弄清楚为什么我的表单在提交时表单空白时不保留数据。当我编辑表单中的值时,它会在那里成功更新到数据库,与删除相同,但是当我尝试添加新的时,它不会通过并将其添加到数据库中我已经成立了。我错过了什么吗?提前谢谢!

require_once '../core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';

// Get brands from database
$sql ="SELECT * FROM brand ORDER BY brand";
$results = $db->query($sql);
$errors = array();

//Edit brand
if (isset($_GET['edit']) && !empty($_GET['edit'])) {
  $edit_id = (int)$_GET['edit'];
  $edit_id = sanitize($edit_id);
  $sql2 = "SELECT * FROM brand WHERE id = '$edit_id'";
  $edit_result = $db->query($sql2);
  $eBrand = mysqli_fetch_assoc($edit_result);
}

// Delete brand
if (isset($_GET['delete']) && !empty($_GET['delete'])) {
  $delete_id = (int)$_GET['delete'];
  $delete_id = sanitize($delete_id);
  $sql = "DELETE FROM brand WHERE id = '$delete_id'";
  $db->query($sql);
  header('Location: brands.php');
}

// if add form is submmited
if (isset($_POST['add_submit'])) {
  $brand = sanitize(mysqli_real_escape_string($db, $_POST['brand']));

  // check if brand is blank
if ($_POST['brand'] == '') {
  $errors[] .= 'You must enter a brand!';
}
// check if brand exists in database
  $sql = "SELECT * FROM brand WHERE brand = '$brand'";
if (isset($_GET['edit'])) {
  $sql = "SELECT * FROM brand WHERE brand = '$brand' AND id != '$edit_id'";
}

$result = $db->query($sql);
$count = mysqli_num_rows($result);

if ($count > 0) {
  $errors[] .= $brand.' already exists. Please choose another brand 
  name...';
}
// display errors
if (!empty($errors)) {
  echo display_errors($errors);
}else {

  // add brand to database
  $sql = "INSERT INTO brand (brand) VALUES '$brand'";

  if (isset($_GET['edit'])) {
    $sql = "UPDATE brand SET brand = '$brand' WHERE id = '$edit_id'";
  }

  $db->query($sql);
  header('Location: brands.php');
}
}
?>
<h2 class="text-center">Brands</h2><hr>

<!-- Brand form -->
<div class="text-center">
<form class="form-inline" action="brands.php<?=((isset($_GET['edit']))?'?
edit='.$edit_id:'');?>" method="post">
<div class="form-group">
  <?php
    $brand_value = '';
   if (isset($_GET['edit'])) {
    $brand_value = $eBrand['brand'];
  }else {
    if (isset($_POST['brand'])) {
      $brand_value = sanitize($_POST['brand']);
    }
  } ?>
  <label for="brand"><?=((isset($_GET['edit']))?'Edit':'Add a');?> Brand:
</label>
  <input type="text" name="brand" id="brand" class="form-control" value="<?
=$brand_value; ?>">
  <?php if(isset($_GET['edit'])): ?>
    <a href="brands.php" class="btn btn-default">Cancel</a>
  <?php endif; ?>
  <input type="submit" name="add_submit" value="<?=
((isset($_GET['edit']))?'Edit':'Add');?> brand" class="btn btn-success">
</div>
</form>
</div><hr>
<table class="table table-bordered table-striped table-condensed" 
style="width:auto; margin:0 auto;">
<thead>
<th></th><th>Brand</th><th></th>
</thead>
<tbody>
<?php while($brand = mysqli_fetch_assoc($results)) : ?>
  <tr>
    <td><a href="brands.php?edit=<?=$brand['id'];?>" class="btn btn-xs btn-
default"><span class="glyphicon glyphicon-pencil"></span></a></td>
    <td><?=$brand['brand']; ?></td>
    <td><a href="brands.php?delete=<?=$brand['id'];?>" class="btn btn-xs 
btn-default"><span class="glyphicon glyphicon-remove-sign"></span></a></td>
  </tr>    
<?php endwhile; ?> 

2 个答案:

答案 0 :(得分:1)

问题出在INSERT查询中,您忘记了VALUES的括号(),您的查询必须是:

$sql = "INSERT INTO brand (brand) VALUES ('$brand')";

..代替..

$sql = "INSERT INTO brand (brand) VALUES '$brand'";

答案 1 :(得分:0)

Soooo我觉得我在SQL语句中犯了一个完整的新手错误。我重新将我的SQL输入phpmyadmin,结果发现我的SQL插入代码出错了。我应该没有括号,在字段名称周围有括号。

实施例。 (brand)代替('brand')

现在一切正常。谢谢大家为我的菜鸟错误做出贡献。我已经解决了。