2016-06-19 80 views
-2

我想跟着一個教程,似乎不能夠將數據添加到數據庫中,並沒有檢查代碼,似乎沒有找到爲什麼它不工作。代碼應該將數據插入到數據庫中,而是在單擊按鈕時刷新頁面。請指出我出錯的地方。因爲我也是PHP新手。下面是我的代碼:無法插入到數據庫

<!DOCTYPE> 

<?php 

include ("includes/db.php"); 

?> 
<html> 
    <head> 
    <title>inserting Product</title> 

    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script> 
    <script> 
    tinymce.init({ selector:'textarea' }); 
    </script> 
    </head> 

    <body bgcolor="skyblue"> 


<form action="insert_product.php" method="post" enctype="multipart/form-data"> 

<table align="center" width="700" border="2" bgcolor="orange"> 

<tr align="center"> 
    <td colspan="7"><h2>Insert New Post Here</h2></td> 
    </tr> 


<tr> 
    <td align="right"><b>Product Title:</b></td> 
    <td><input type="text" name="product_title" size="60" required /></td> 
</tr> 

<tr> 
    <td align="right"><b>Product Category:</b></td> 
    <td> 
<select name="product_cat" > 
    <option>Select a Category</option> 
<?php 
$get_cats ="select * from categories"; 
$run_cats = mysqli_query($con, $get_cats); 

While ($row_cats=mysqli_fetch_array($run_cats)){ 


//$cat_id = $row_cats['cats_id']; 
$cat_title = $row_cats['cat_title']; 

echo "<option value='$cat_id'>$cat_title</option>"; 


} 


?> 

</select> 

    </td> 
</tr> 

<tr> 
    <td align="right"><b>Product Brand:</b></td> 
    <td> 
    <select name="product_brand" > 
     <option>Select a Brand</option> 
<?php 
$get_brands = "select * from brands"; 

$run_brands = mysqli_query($con, $get_brands); 

While ($row_brands=mysqli_fetch_array($run_brands)){ 


//$cat_id = $row_cats['cats_id']; 
$brand_title = $row_brands['brand_title']; 

echo "<option value='brand_id'>$brand_title</option>"; 

} 



?> 

    </td> 
</tr> 

<tr> 
    <td align="right"><b>Product Image:</b></td> 
    <td><input type="file" name="product_image" required /></td> 
</tr> 

<tr> 
    <td align="right"><b>Product Price:</b></td> 
    <td><input type="text" name="product_price" required /></td> 
</tr> 

<tr> 
    <td align="right"><b>Product Description:</b></td> 
    <td><textarea name="product_desc" cols="20" rows="10"></textarea></td> 
</tr> 

<tr> 
    <td align="right"><b>Product Keywords:</b></td> 
    <td><input type="text" name="product_keywords" size="50" required /></td> 
</tr> 



<tr align="center"> 
    <td colspan="7"><input type="submit" name="inset_post" value="Insert Product Now" /></td> 
</tr> 


</table> 

</form> 

    </body> 
    </html> 
<?php 

if(isset($_POST['insert_post'])){ 

//input data from the fields 
$product_title = $_POST['product_title']; 
$product_cat = $_POST['product_cat']; 
$product_brand = $_POST['product_brand']; 
$product_price = $_POST['product_price']; 
$product_desc = $_POST['product_desc']; 
$product_keywords = $_POST['product_keywords']; 

//input image from feild 
$product_image = $_FILES['product_image']['name']; 
$product_image_tmp = $_FILES['product_image']['tmp_name']; 

//move_uploaded_file($product_image_tmp,"product_images/$product_image"); 


echo $insert_product = "insert into products(product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords)values('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keywords')"; 

//$insert_pro = mysqli_query($con, $insert_product); 

//if($insert_pro){ 

//echo"<script>alert('Product has been inserted!')</script>"; 
//echo "<script>window.open('insert_product.php','_self'</script>)"; 

//} 


} 

?> 

    enter code here 
+1

(1)你有沒有意識到的代碼運行你的'插入into'查詢被註釋掉? (2)通常最好在將代碼示例發佈到此處之前嘗試將代碼示例減少到最小可重現的示例。 –

+0

看看提交輸入元素,'... name =「inset_post」...',它應該是'name =「insert_post」' –

回答

1

你的提交按鈕有名爲「inset_post」,而不是「插入」

+0

謝謝你所有我完全錯過了它現在工作。 –