这里我试图将图像上传到目录并在数据库中添加路径。 在这里,我首先添加一些产品细节,并尝试将图像上传到上传目录,并在数据库中添加上传的图像路径。
这就是我所做的:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "wan_products_box");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$product_name = mysqli_real_escape_string($link, $_POST['product_name']);
$product_category = mysqli_real_escape_string($link, $_POST['product_category']);
$product_price = mysqli_real_escape_string($link, $_POST['product_price']);
$pro_url = mysqli_real_escape_string($link, $_POST['pro_url']);
$co_owners = mysqli_real_escape_string($link, $_POST['co_owners']);
// attempt insert query execution
$sql = "INSERT INTO product_list (product_name, product_category, product_price,product_referrence_URL,product_co_owners) VALUES ('$product_name', '$product_category', '$product_price', '$pro_url', '$co_owners')";
if(mysqli_query($link, $sql)){
echo "New product created.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//image upload code
if($_POST)
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if(file_exists("uploaded_images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded_images/" . $_FILES["file"]["name"]))
{
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded_images/" . $_FILES["file"]["name"];
}
else
{
echo 'Unable to store';
}
}
}
}
}
mysqli_close($link);
?>
这是我的html表单:
<div id="menu2" class="tab-pane fade">
<h4>Add new product</h4>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal" method="post" action="files/insert.php" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="product_name">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_name" id="product_name" placeholder="Iphone 5c" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Product Category</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="product_category">
<option>Mobile</option>
<option>Television</option>
<option>Printer</option>
<option>Watch</option>
<option>Monitor</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product-pic">Upload your profile picture</label>
<div class="col-sm-10">
<input type="file" class="form-control" name="file" id="file" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product_price">Product Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_price" id="product_price" placeholder="Rs.36,000" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pro_url">Reference URL</label>
<div class="col-sm-10">
<input type="URL" class="form-control" name="pro_url" id="pro_url" placeholder="http://www.amazon.com" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="co_owners">Co-owners</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="co_owners">
<option>Select no. owners</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Add product</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
数据被插入数据库但图像上传部分没有发生...... 我怎样才能做到这一点?如何修改代码以将图像上传到数据库并添加数据库路径?
当我运行此输出时,我得到的是
New product created.
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 31
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 37
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 43
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line
答案 0 :(得分:2)
您的代码中存在一些问题。
首先,您的表单缺少处理文件时需要的有效enctype enctype="multipart/form-data"
。
您还在第二次查询中混合MySQL API。
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
if(mysql_query($query_image))
使用您在第一次查询中使用的相同方法。
所以将其更改为if(mysqli_query($link, $query_image))
同时将or die(mysqli_error($link))
添加到mysqli_query()
以检查错误。
还要确保该文件夹具有相应的写入权限。
另一方面,您的<select>
选项没有值
<option>Select no. owners</option>
<option>1</option>
...
您需要添加
<option value="empty_value">Select no. owners</option>
<option value="1">1</option>
...
并为其他人做同样的事。
<option>Mobile</option>
也是如此。这些人也应该有价值观。
<option value="mobile">Mobile</option>
...
您不会在数据库中输入任何内容。
答案 1 :(得分:0)
除了缺少enctype="multipart/form-data"
之外,HTML部分存在问题。您的第二个查询中存在问题,这是导致这些错误的原因:
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
您需要将此更改为UPDATE
查询并更新数据库中已创建的行。
此外,还可以使用行的索引号作为图像名称。