2017-09-05 97 views
0

因此,我從頭開始演示網上商店,我似乎無法弄清楚爲什麼我的表單在提交時表單爲空時不保留數據。當我編輯表單中的值時,它就在那裏,可以成功更新到數據庫,與刪除一樣,但是當我嘗試添加一個新的時,它不會通過並將其添加到我設置的數據庫中。我錯過了什麼嗎?提前致謝!在php和MYSQL中的邏輯問題

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; ?> 
+1

調試,我的男人,調試。在這兩種情況下,所有相關變量都充滿了您期望的值嗎?如果您在phpmyadmin或任何您使用的版本中進行了複製(來自調試,而不是手工填寫),您的查詢是否正確並正確工作? – deg

+3

你錯過了INSERT SQL值部分的括號,你應該添加錯誤檢查。 –

+3

一些明智的代碼縮進將是一個好主意。它可以幫助我們閱讀代碼,更重要的是,它可以幫助您**調試您的代碼** [快速瀏覽編碼標準](http://www.php-fig.org/psr/psr-2/ )爲了您自己的利益。您可能會被要求在幾周/幾個月內修改此代碼 ,最後您會感謝我。 – RiggsFolly

回答

1

的問題是在INSERT查詢,你忘了括號()的價值觀,你的查詢必須是:

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

的..instead ..

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

是的,這是我的錯誤沒有包裹我的變量括號。喬恩在評論中引起了我的注意,但是這並不能解決我提交新變量時我的表單中沒有任何內容被傳遞的問題。 – WebDeVGuy24

0

SOOOO我覺得我在SQL語句中犯了一個完整的菜鳥錯誤。我重新將我的SQL輸入到phpmyadmin中,結果發現我的SQL插入代碼存在錯誤。當應該沒有括號時,我在字段名稱周圍有括號。

例。 (brand)而不是('brand')

現在一切正常,因爲它應該。謝謝大家爲我的菜鳥錯誤做出貢獻。我解決了它。