用户使用PHP上传广告到汽车网站

时间:2018-10-10 19:53:43

标签: php forms phpmyadmin image-uploading

我正疯狂地试图找出如何允许用户将广告上传到我为Uni项目创建的汽车网站上的方法。 用户应该能够输入汽车的详细信息和汽车的图像。经过大量搜索后,我发现建议将图像上传到目录,并将路径存储在数据库中。我想为图像分配新名称,因此它是唯一的。 将图像上传到目录的代码可以正常工作并重命名。我的问题是将数据插入数据库。我收到错误消息“通知:数组到字符串的转换”,我猜这是从数组中重命名完成的吗?请帮忙。这是我的PHP代码(我知道它到处都是切割等,需要整理)。

<?php



if(isset($_POST['upload_car'])){

$car_make = mysqli_real_escape_string($con, $_POST['make']);

$car_model = mysqli_real_escape_string($con, $_POST['model']);

$car_price = mysqli_real_escape_string($con, $_POST['price']);

$car_year = mysqli_real_escape_string($con, $_POST['year']);

$car_mileage = mysqli_real_escape_string($con, $_POST['mileage']);

$car_engine = mysqli_real_escape_string($con, $_POST['engine']);

$car_gearbox = mysqli_real_escape_string($con, $_POST['gearbox']);

$car_area = mysqli_real_escape_string($con, $_POST['area']);

$car_colour = mysqli_real_escape_string($con, $_POST['colour']);

$car_descr = mysqli_real_escape_string($con, $_POST['description']); 

$usr_id = mysqli_real_escape_string($con, $_SESSION['mem_ID']);


$sql = "SELECT * FROM users WHERE id='$usr_id'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_array($result))
    {
        $contact_num = $row['phone'];
        $contact_email = $row['email'];
    }
}

$image = $_FILES['image'];
$image_name = $_FILES['image']['name'];
$image_TmpName = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_error = $_FILES['image']['error'];
$image_type = $_FILES['image']['type'];
$imageExt = explode('.', $image_name);
$imageActualExt = strtolower(end($imageExt));
$allowed = array('jpg', 'jpeg', 'png');

if(in_array($imageActualExt, $allowed)){
    if($image_error === 0){
        if($image_size < 1000000){
            $image_nameNew = uniqid('', true).".".$imageActualExt;
            $imageDestination = 'uploads/'.$image_nameNew;
            move_uploaded_file($image_TmpName, $imageDestination);
        }else{
            echo"Your image is too big";
        }
    }else{
        echo"There was a problem uploading";
    }
}else{
    echo "You can not upload that file type.";
}




if(!preg_match("/^[a-zA-Z]+$/", $car_make, $car_model)){
    $error = true;
    echo'<script>alert("Name must contain only alphabets and space.")</script>';
}



if(!$error){
    if(mysqli_query($con, "INSERT INTO car(make, model, price, year, milage, engine_size, gearbox, area, colour,
                            vehicle_descr, photo, phot_dest, contact_num, contact_email, id)
    VALUES('" . $car_make ."', '" . $car_model . "', '" . $car_price . "', '" . $car_year . "', '" . $car_mileage . "',
    '" . $car_engine . "', '" . $car_gearbox . "', '" . $car_area . "', '" . $car_colour . "', '" . $car_descr . "',
    '" . $image_nameNew . "', '" . $imageDestination . "', '" . $contact_num . "', '" . $contact_email . "', '" . $usr_id . "' )"))
    {

        echo'<script>alert("Your car has been uploaded")</script>';
    }
    else{
        echo'<script>alert("Error uploading! Please try again later")</script>';
    }
}
 }

   ?>

1 个答案:

答案 0 :(得分:1)

您的问题出在preg_match()或以下这行:

if(!preg_match("/^[a-zA-Z]+$/", $car_make, $car_model)){

第三个参数的文档明确指出:

  

如果提供了匹配项,则将其填充为搜索结果。 $matches[0]将包含与完整模式匹配的文本,$matches[1]将具有与第一个捕获的带括号的子模式匹配的文本,依此类推。

因此,$car_model会在构建INSERT字符串之前变成一个数组。


我尚未检查您的代码,但将其更改为:

if(!preg_match("/^[a-zA-Z]+$/", $car_make)){

可能会解决您的问题。