我试图将图像引用附加到数据库中的image1-4列,而不是将图像上载x3次,并且在phpmyadmin中使用随机引用。

<?php

$user_name = "root";
$user_pass = "";
$host_name = "localhost";
$db_name = "melony";

$con = mysqli_connect($host_name, $user_name, $user_pass, $db_name);


if($con)
{

    $image = $_POST['image'];
    $name = $_POST['name'];
    $lat = (float) $_POST['lat'];
    $lng = (float) $_POST['lng'];


    for($i=1; $i<5; $i=$i+1){
        $now = DateTime::createFromFormat('U.u', microtime(true));
        $id = $now->format('YmdHisu');
        $upload_path = "uploads/$id.jpeg";


        $sql = "insert into uploads(name,lat,lng,image,image2,image3,image4) values
                ('$name','$lat','$lng','$upload_path','a','a','a')ON DUPLICATE KEY UPDATE
                image$i = '$upload_path'";

        if(mysqli_query($con,$sql))
        {
            file_put_contents($upload_path,base64_decode($image));
            echo json_encode(array('response'=>'Image Upload Successfully'));
        }
        else
        {
            echo json_encode(array('response'=>'Image upload failed1,

            mysqli_error($con)'));
        }
    }
}
else
{
    echo json_encode(array('response'=>'Image Upload Failed2'));
}

mysqli_close($con);

?>

最佳答案

您每次都在覆盖同一张图片...

$upload_path = "uploads/$id.jpeg";


大概应该是

$upload_path = "uploads/$i.jpeg";

08-26 08:30