我想在PHP中使用MYSQL在数据库中插入图像路径
我想在数据库中插入图像路径。 在浏览器中,当任何用户选择图像路径时,该图像将存储在图像文件夹中,该路径将存储在数据库中。 我尝试了,但是if($type==…)部分不起作用。它将转到else语句 编辑:我想在PHP中使用MYSQL在数据库中插入图像路径,php,mysql,image,mysqli,Php,Mysql,Image,Mysqli,我想在数据库中插入图像路径。 在浏览器中,当任何用户选择图像路径时,该图像将存储在图像文件夹中,该路径将存储在数据库中。 我尝试了,但是if($type==…)部分不起作用。它将转到else语句 编辑: if (isset($_POST['bSubmit'])) { var_dump($_FILES); $name = $_FILES['file']['name']; $type=var_dump($_FILES['file']['type']); if ($t
if (isset($_POST['bSubmit'])) {
var_dump($_FILES);
$name = $_FILES['file']['name'];
$type=var_dump($_FILES['file']['type']);
if ($type == 'image/jpeg' || $type == 'image/png' || $type == 'image/gif') {
if (file_exists(dirname($_SERVER['DOCUMENT_ROOT']) . '/htdocs/Amit_404_Store/img/' . $name)) {
echo 'file is already present';
} else {
$uploadimage = move_uploaded_file($_FILES['file']['tmp_name'], dirname($_SERVER['DOCUMENT_ROOT']) . '/htdocs/Amit_404_Store/img/' . $name);
echo "Stored in: " . "" . $name . "<br />";
$destination = $name;
echo "Upload in: " . "" . $destination . "<br />";
//Database connection
$servername = "Localhost";
$username = "root";
$password = "";
$database = "new_404_store2";
//session value store and retrive data for index.html file
$email = $_SESSION['user_name1'];
$conn = mysqli_connect($servername, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$Update_query = "update customer_information2 set Image_Path='$destination' where Email_Id='$email'";
$result = mysqli_query($conn, $Update_query);
if (!$result) {
die('Could not enter data: ' . mysqli_error());
}
if ($uploadimage) {
echo 'image uploaded and stored';
} elseif (!$uploadimage) {
echo 'image not uploaded';
}
}
} else {
echo 'Invalid file type';
}
}
//mysqli_close($conn);
?>
if(isset($\u POST['bSubmit'])){
变量转储($\u文件);
$name=$_文件['file']['name'];
$type=var_dump($_FILES['file']['type']);
如果($type=='image/jpeg'| |$type=='image/png'| |$type=='image/gif'){
如果(文件_存在(dirname($_SERVER['DOCUMENT_ROOT'])。/htdocs/Amit_404_Store/img/'.$name)){
echo“文件已存在”;
}否则{
$uploadimage=move_upload_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT'])。/htdocs/Amit_404; Store/img/。$name);
echo“存储在:”.$name.“
”;
$destination=$name;
echo“上传到:”.$destination.“
”;
//数据库连接
$servername=“Localhost”;
$username=“root”;
$password=“”;
$database=“new_404_store2”;
//会话值存储和检索index.html文件的数据
$email=$\会话['user\u name1'];
$conn=mysqli\u connect($servername、$username、$password、$database);
if(mysqli\u connect\u errno()){
echo“未能连接到MySQL:”.mysqli_connect_error();
}
$Update\u query=“更新客户信息2设置图像\路径=“$destination”,其中电子邮件\ Id=“$Email”;
$result=mysqli\u查询($conn,$Update\u查询);
如果(!$result){
die('无法输入数据:'.mysqli_error());
}
如果($uploadimage){
echo“上传并存储图像”;
}elseif(!$uploadimage){
回显“图像未上传”;
}
}
}否则{
回显“无效文件类型”;
}
}
//mysqli_close($conn);
?>
您尚未编写enctype,文件传输需要它
<form method="post" action="example4.php" id="frmOrderDetailsGraphic" class="" autocomplete="off" enctype="multipart/form-data">
应该是
if ($type == 'image/jpeg' || $type == 'image/jpg' || $type == 'image/png' || $type == 'image/gif') {
如果您正在添加图像文件,但仍然收到错误您收到的错误我收到的其他部分消息“无效文件类型”光标未检查此代码如果($type=='image/jpeg'| |$type=='image/png'| |$type=='image/gif')){您确定$type是否为上述类型之一吗?我应该在哪里键入此代码。您好,Abhi,我也尝试了此操作,但再次收到输出无效的文件类型您的提交按钮名称为bSubmit,因此您的第一行应该是if(isset($\u POST['bSubmit']){是的,我编写的代码是正确的if(isset($\u POST['bSubmit']))仅检查您发布的代码是否已写入if(isset($\u POST['submit']){if(isset($\u POST['bSubmit']){$name=$\u FILES['file']['name'];$type=var\u dump($\u FILES['file']['type']);
if ($type == 'image/jpeg' || $type == 'image/jpg' || $type == 'image/png' || $type == 'image/gif') {