Php 所有表单字段都工作正常,但图像未插入到表中,其为空且未显示任何错误
这是我的插入产品代码。db一切正常,但每当我插入图像/选择文件时,图像列都保持为空。当没有选择图像时,它也没有任何效果或错误,但是如果我没有选择任何其他字段,db中的表将保持为空。它与图像相反。我找到了一些关于图像上传的教程,但它们对我的代码也不起作用。有什么建议吗Php 所有表单字段都工作正常,但图像未插入到表中,其为空且未显示任何错误,php,mysql,forms,web,Php,Mysql,Forms,Web,这是我的插入产品代码。db一切正常,但每当我插入图像/选择文件时,图像列都保持为空。当没有选择图像时,它也没有任何效果或错误,但是如果我没有选择任何其他字段,db中的表将保持为空。它与图像相反。我找到了一些关于图像上传的教程,但它们对我的代码也不起作用。有什么建议吗 <?php include 'includes/overall/head.php'; include 'core/init.php'; ?> <script src="//tinymce.cachefl
<?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';
?>
查询中存在多个错误。首先,您忘记在de变量之间添加点
$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\u价格和$product\u关键字之前或之间,您还缺少一个空格
OR $product_brand=='' OR $product_price==''OR $product_keywords==''
并确保与数据库的连接设置正确
编辑:
第一部分是错误的。你不需要添加点。否则,点将显示在数据库中。很抱歉造成误解不要使用大写字体。这就像打耳光一样face@Abdulla固定的你能帮我修改一下代码吗?连接很好,我按照你的建议修改了代码,因为静止图像栏是空的!您在哪里定义了$image变量?Samadani..我认为$image=isset$_FILES['image']['name'];我需要在其他地方定义它吗?plz guidYou需要设置两件事。首先使用include ini_set'display_errors'上方的代码打开错误报告,1;ini设置“显示启动错误”,1;错误报告全部;在$Image=isset$_文件['Image']['name']之后;打印\u r$图像;出口要查看打印出来的内容已弃用:mysql\u connect:mysql扩展已弃用并将在将来删除:请在第4行的C:\xampp\htdocs\php\admin\u area\core\database\connect.php中使用mysqli或PDO