gpt4 book ai didi

php - 将 blob 文件/图像上传到 Mysql

转载 作者:行者123 更新时间:2023-11-28 23:47:53 25 4
gpt4 key购买 nike

我有这段 php 代码,它从 html 表单中获取值(名称、文件、照片、地址......),并尝试在 sql 数据库中插入或更新它们。

<?php 
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
session_start();
$con=mysqli_connect("localhost","","","");

$id=$_REQUEST['id'];

//Variable intilisation
$name = '';
$remarcs = '';
$address = '';
$test_res = '';
$date = '';
$phone = '';
$new_path = '';



if (isset ($_POST['name'])) {
$name = $_POST['name'];
}
if (isset ($_POST['remarcs'])) {
$remarcs = $_POST['remarcs'];
}
if (isset ($_POST['test_res'])) {
$test_res = $_POST['test_res'];
}
if (isset ($_POST['address'])) {
$address = $_POST['address'];
}

if (isset ($_POST['date'])) {
$date = $_POST['date'];
}

if (isset ($_POST['phone_num'])) {
$phone = $_POST['phone_num'];
}

if(!empty($_FILES)){ //Check file is uploaded or not
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
$path = "../uploads/".$_FILES['file']['tmp_name'];
move_uploaded_file($file, $path);
$new_path = $path;
echo "Uploaded";
}
if($check == false){
echo "Not uploaded";
}
}
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)){
$sql=" update patients set values
name = '$name',
echo_photo = 'NULL',
echo_file = '$new_path',
remarcs = '$remarcs',
test_res = '$test_res',
date = '$date',
address = '$address',
phone_num = '$phone'
WHERE id = ".$id;

$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
header("location:update_done.php");

}else{
header("location:update_false.php");

}
if($result){
echo $name."<p>\n</p>";
echo $remarcs."<p>\n</p>";
echo $test_res."<p>\n</p>";
echo $address."<p>\n</p>";
echo $phone."<p>\n</p>";
}

mysqli_close($con);

?>

问题是,当我只上传下图所示的文件时,出现以下错误: enter image description here

当我不上传文件时,出现错误:请选择一个文件。有什么帮助吗?谢谢。

这是 html 表单:

<form action="update.php" id="Form2" method="POST" enctype="multipart/form-data" class="c">
<div align="center">
<?php echo "Updating information about Patient ".$row["name"]; ?>
<table class="imagetable" border="1" cellspacing="3" align="center">
<th>Personal Informations</th>
<th>Test Results</th>

<tr><td>Name<br>
<input type="text" class="large-fld" name="name" placeholder="Patient Name" value="<?php echo $row['name'];?>"/></td>
<td>Remarcs:<br>
<textarea type="text" cols="40" rows="5" class="large-fld" name="remarcs" placeholder="Remarcs"><?php echo $row['remarcs'];?></textarea></td>
<tr><td>Address<br>
<input type="text" class="large-fld" name="address" placeholder="Address" value="<?php echo $row['address'];?>"/>
</td>
<td>Test<br> <textarea type="text" cols="40" rows="5" class="large-fld" name="test_res" placeholder="Test Result"><?php echo $row['test_res'];?></textarea></td></tr>
</td>
</tr>
<tr><td>Phone Number<br>
<input type="text" class="large-fld" name="phone_num" placeholder="Phone Number" value="<?php echo $row['phone_num'];?>"/>
</td>
<th>Files</th>
</tr>
<td>Scanned Echo Photo<br>
<input type="file" class="" name="echo_photo" id="echo_photo" placeholder="Add echo photo" value="<?php echo $row['echo_photo'];?>"/></td>
<td>Echo Files:<br>
<input type="file" name="file" id="file" value="<?php echo $row['echo_files'];?>"/><br></td>
</tr></th></table>
<div class="row" align="center">
<input type="submit" name="submit" id="btnUploadId" class="large-btn" value="Update" onClick="btnOnClickUpload()">
<input type="hidden" id="courseIdHidden" value="<?php echo $idd; ?>" /></td></tr>
</table></div>
</form>

最佳答案

尝试初始化变量。我已经添加了整个代码。您不需要获取 $_POST['file']。

<?php 
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="clinic"; // Database name
$tbl_name="patients"; // Table name
session_start();
$con=mysqli_connect("localhost","root","root","clinic");

$id=$_REQUEST['id'];

$name = '';
$remarcs = '';
$address = '';
$test_res = '';
$date = '';
$phone = '';
$new_path = '';

if (isset ($_POST['name'])) {
$name = $_POST['name'];
}
if (isset ($_POST['remarcs'])) {
$remarcs = $_POST['remarcs'];
}
if (isset ($_POST['test_res'])) {
$test_res = $_POST['test_res'];
}
if (isset ($_POST['address'])) {
$address = $_POST['address'];
}

if (isset ($_POST['date'])) {
$date = $_POST['date'];
}

if (isset ($_POST['phone_num'])) {
$phone = $_POST['phone_num'];
}

if(isset($_FILES['file'])){ //Check file is uploaded or not

$path = "../uploads/".$_FILES['file']['name'];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){
$new_path = $path;
$sql=" update patients set
name = '$name',
echo_photo = 'NULL',
echo_file = '$new_path',
remarcs = '$remarcs',
test_res = '$test_res',
date = '$date',
address = '$address',
phone_num = '$phone'
WHERE id = ".$id;

$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));

if($result){
echo $name."<p>\n</p>";
echo $remarcs."<p>\n</p>";
echo $test_res."<p>\n</p>";
echo $address."<p>\n</p>";
echo $phone."<p>\n</p>";
}
echo "Uploaded";
} else {
echo "Not uploaded";
}
}
mysqli_close($con);

?>

关于php - 将 blob 文件/图像上传到 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251291/

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