Php 如何将输入、文本区域和图像上载插入数据库
Hye,目前我在视频中学习了一些教程,但我不知道我的数据是如何不将pic上传到数据库的 这发生在我的数据库中,当我把一些数据放在表单中,我的pic仍然没有上传 这是PHP中的代码Php 如何将输入、文本区域和图像上载插入数据库,php,Php,Hye,目前我在视频中学习了一些教程,但我不知道我的数据是如何不将pic上传到数据库的 这发生在我的数据库中,当我把一些数据放在表单中,我的pic仍然没有上传 这是PHP中的代码 <?php $sqlservername = "127.0.0.1"; $sqlusername = "root"; $sqlpassword = ""; $sqldbname ="test"; $conn = new mysqli($sqlservername, $sqlusername, $sqlpassw
<?php
$sqlservername = "127.0.0.1";
$sqlusername = "root";
$sqlpassword = "";
$sqldbname ="test";
$conn = new mysqli($sqlservername, $sqlusername, $sqlpassword, $sqldbname);
if($conn->connect_error){
die("Connection fail");
}
session_start();
if(isset($_POST['li_submit'])){
$_SESSION['li_username'] = $_POST['username'];
$li_username = $_SESSION['li_username'];
}
if(isset($_POST['submit'])){
$id = $username = $password = $location = $description = "";
if(isset($_POST['username']))
{
$id = uniqid($prefix='u_');
$username = $_POST['username'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];}
if(isset($_POST['location'])){
$location = $_POST['location'];
}
if(isset($_POST['description']))
{
$description = $_POST['description'];
}
if(isset($_FILES['profile_pic'])){
$target_dir = "IMG_UPLOAD/";
$target_file = basename($_FILES['profile_pic']["name"]);
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$target_path = $target_dir . uniqid($prefix='img.'). "." .$file_type;
if(!move_uploaded_file($_FILE['profile_pic']['tmp_name'], $target_path)){
$target_path = "";
}
echo $target_path;
}
$sql_insert = $conn->prepare("insert into user_info(id, username, password,
location, description, image_path) values (?,?,?,?,?,?)");
$sql_insert->bind_param("ssssss", $id, $username, $password, $location,
$description, $target_path);
$sql_insert->execute();
$sql_insert->close();
}
$conn->close();
?>
这段代码是HTML格式的,如果你想看的话
<body>
<h1>Profile</h1>
<form method='post' enctype=''multipart/form-data'>
<input type='name' name='username' placeholder='Username'/>
<input type='password' name='password' placeholder='Password'/>
<input type='test' name='location'placeholder='Location'/>
<textarea name='description'>Enter about yourself</textarea>
<input type='file' name='profile_pic'/>
<input type='submit' name='submit' value='save'/>
</form>
</body>
我希望当我按下“保存”按钮时,我输入的所有数据都会保存到数据库中,包括基于屏幕截图的图片。请帮帮我,我还是PHP的新手,你在下面的一行还有一个打字错误。它将是$\u文件,而不是$\u文件
if(!move_uploaded_file($_FILE['profile_pic']['tmp_name'], $target_path)){
要将数据插入数据库,请使用以下代码:
$sql_insert = mysqli_query($conn, "insert into user_info (username, password, location, description, image_path) values ('$username', '$password', '$location', '$description', '$target_path')");
将id设置为主键并自动递增
还要确保您在包含php文件的目录中创建了一个名为IMG_UPLOAD的文件夹。是不是标记的enctype中有一个输入错误:好了,现在所有以表单形式插入的数据都成功上载到数据库中了,因为我所有的声明都是$username,哈哈哈。愚蠢的我,现在我需要找到一种方法,如何将图片上传到数据库,这样意味着,ID将自动生成,而我的代码中没有任何语句?是的,不需要将ID设置为主键。您将id插入数据库的方式是正确的:@MrHaikal