gpt4 book ai didi

php - 所有表单字段都工作正常,但图像未插入表中,其为空并且不显示任何错误,如下所示

转载 作者:行者123 更新时间:2023-11-29 21:53:10 25 4
gpt4 key购买 nike

这是我的插入产品代码。数据库一切正常,但每当我插入图像/选择文件时,图像列保持为空。当未选择图像时,它也没有任何效果或错误,但如果我不选择任何其他字段,数据库中的表将保持为空。在图像的情况下则相反。我找到了有关图像上传的教程,但它们也不适用于我的代码。有什么建议吗?

<?php
include 'includes/overall/head.php';
include 'core/init.php';
?>

<script src="//tinymce.cachefly.net/4.2/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>

<?php
include 'includes/aside.php';
?>

<div class="container-form">
<div align="center">
<h1>Insert Products</h1>
</div>
<!--Register users here-->
<div class=>
<form action="insert_product.php" method="post" enctype="multipart/form_data">
<div class="table-responsive">
<table class="table" width="700" align="center" border="1">

<tr>
<td><strong>Car Title</strong></td>
<td><input type="text" name= "product_title"></td>
</tr>
<tr>
<td><strong>Car Category</strong></td>
<td>
<select name="product_cat">
<option>Select Category</option>

<?php
// SQL query
$strSQL = "SELECT * FROM categories ";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

// Name of the person
$cat_id = $row['cat_id'] ;
$cat_title = $row['cat_title'] ;
// Create a link to person.php with the id-value in the URL
$strLink = "<a href = 'person.php?id = " . $row['cat_id'] . "'</a>";
// List link
echo "<option value='$cat_id'>$cat_title</option>";
//echo "<li>" . $btitle . "</li>";

}
?>
</select>
</td>
</tr>

<tr>
<td><strong>Car Company</strong></td>
<td>
<select name="product_brand">
<option>Select Company</option>

<?php
// SQL query
$brand = "SELECT * FROM brands ";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($brand);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

// Name of the person
$brand_id = $row['brand_id'] ;
$brand_title = $row['brand_title'] ;
// Create a link to person.php with the id-value in the URL
$strLink = "<a href = 'person.php?id = " . $row['brand_id'] . "'</a>";
// List link
echo "<option value='$brand_id'>$brand_title</option>";
//echo "<li>" . $btitle . "</li>";
}
?>
</select>
</td>
</tr>
<tr>
<td><strong>Add Image</strong></td>
<td>
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
</td>
</tr>
<tr>
<td><strong>Car Rent</strong></td>
<td><input type="text" name= "product_price"></td>
</tr>
<tr>
<td><strong>Car Description</strong></td>
<td><textarea name="product_description"></textarea></td>
</tr>
<tr>
<td><strong>Car Keyword</strong></td>
<td><input type="text" name= "product_keywords"></td>
</tr>
<td colspan="2">
<button type="submit" name="insert_product" class="btn btn-primary col-md-offset-6">Add Vehicle</button>
</td>
</table>
</div>
</form>
<div>
</div>
</div>
</div>

<?php
if(isset($_POST['insert_product'])){
//text data variables
$product_title=$_POST['product_title'];
$product_cat=$_POST['product_cat'];
$product_brand=$_POST['product_brand'];
$product_price=$_POST['product_price'];
$product_description=$_POST['product_description'];

$product_status='on';
$product_keywords=$_POST['product_keywords'];

//image names: media file or multipart data



$image=isset($_FILES['image']['name']);
//Temp names
$temp_name1=isset($_FILES['image']['temp_name']);
//validation:

if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price==''OR $product_keywords==''
OR $product_description=='' OR $image='') {
echo "<script>alert('Please fill all the fields')</script>";
exit();
}

//here is my upload image to folder code but its not working

//upload images to folder
move_uploaded_file($temp_name1 , "product_images/$image");
$query= "INSERT INTO product ( cat_id, brand_id, date, product_title, image, product_price, product_description, product_status)
VALUES ( '$product_cat' , '$product_brand' , NOW() , '$product_title' , '$image' , '$product_price' , '$product_description' , '$product_status')";
$rs = mysql_query($query);
// Loop the recordset $rs
if($rs) {
echo 'Successful';
}
}
?>

<!-------------------------------------------------------->
<?php
include 'includes/footer.php';
?>

最佳答案

您的查询中有几个错误。首先,您忘记在变量之间添加点。

$query= "INSERT INTO product ( cat_id, brand_id, date, product_title,    image, product_price, product_description, product_status) 
VALUES ( '.$product_cat.' , '.$product_brand.' , NOW() , '.$product_title.' , '.$image.' , '.$product_price.' , '.$product_description.' , '.$product_status.')";

此外,您还漏掉了 $product_price 和 $product_keywords 之间的 OR 之前的空格

OR $product_brand=='' OR $product_price==''OR $product_keywords==''

并确保与数据库的连接设置正确

编辑:

第一部分是错误的。您不需要添加点。否则这些点将显示在数据库中。抱歉造成误会

关于php - 所有表单字段都工作正常,但图像未插入表中,其为空并且不显示任何错误,如下所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33430265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com