发送POST数据时未定义的索引

时间:2016-08-09 20:35:01

标签: php html mysql

我遇到了一些问题,看起来我无法找到解决问题的方法。我尝试对每个POST数据使用isset,但是虽然它可以解决所有问题,但数据不会被添加。 我会留下HTML代码和PHP,所以也许你可以帮我调试这段代码。

  

adm_prod.php(处理表单的html页面)

            <form method="POST" action="includes/prod-add.php">
  Product Name<br>
  <input type="text" name="Name"><br>
  Price:<br>
  <input type="number" name="Price"><br>
            Product Description<br>
            <input type="text" name="Description"><br>
            Photo<br>
            <input type="file" name="Photo"><br>
            </br>
            <button name="submit">Add Product</button>

</form>
  

prod-add.php(处理插入/验证信息的php文件)

<?php
include 'databaseConnection.php';

$name = $_POST['Name'];
$price = $_POST['Price'];
$description = $_POST['Description'];
$target_dir = "images-uploads/";
$target_file = $target_dir . basename($_FILES['Photo']['name']);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

if(isset($_POST['submit'])) {
    $check = getimagesize($_FILES['Photo']['tmp_name']);
    if($check !== false) {
        $uploadOk = 1;
    }      else {
        echo "The File Is not an image";
        $uploadOk = 0;
    }
}

if(file_exists($target_file)) {
    $filename = $_FILES['Photo']['name'];
    $extension = end(explode(".",$filename));
    $name = rand(pow(10, 7), pow(10, 8)-1);
    $newfilename = $name . "." .$extension;
    $uploadOk = 1;
    echo "Image already exists. Image Name changed to " . $newfilename;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
$uploadOk = 0;    
echo "Sorry, only JPG, PNG and JPEG are accepted";
}



if ($uploadOk == 0) {
    echo " Sorry, your product was not added, please check the error";
    $fileupload = 0;

} else {
    if (move_upload_file($_FILES['Photo']['tmp_name'], $target_file)) {
        $fileupload = 1;
        $imagePath = basename( $_FILES['Photo']['name']) . "." . $imageFIleType;
        }
}

if($fileupload == 1) {

    $addProd = "INSERT INTO meniu (name, price, description, path) VALUES ('$name','$price','$description','$imagePath')";


    if ($conn->connect_error) {
        die("Connection Failed: " . $conn->connect_error);
    }

    if ($conn->query($addProd) == TRUE) { ?>
<script>
    window.alert("Product: <?php echo $name; ?> has been added successfully ");
    </script>


    <?php    } else { ?>
        <script> 
            window.alert("Error: <?php echo $conn->error; ?>");
        </script>
  <?php  }

}

?>

对不起,如果代码不是很清楚,我还在学习PHP。通常我没有这样的问题,但这是我第一次使用图片上传。 基本上,在数据库中我想介绍名称,描述,价格,照片路径。路径应该类似于../uploads/photoname.extension。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

提交表单时会导致错误,而不会先填写所有字段。因此,您需要首先检查表单是否已提交,以检查$_POST变量是否已设置。

另外,请将其添加到 html form中的enctype="multipart/form-data"标记中。

$name=$price=$description="";
if($_SERVER['REQUEST_METHOD']=="POST") {
//if the form has been submitted, initialize the values.

$name = $_POST['Name'];
$price = $_POST['Price'];
$description = $_POST['Description'];
$target_dir = "images-uploads/";
$target_file = $target_dir . basename($_FILES['Photo']['name']);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
:
//rest of the code
:
}

在里面检查文件是否随上传:

if(isset($_POST['submit']) && isset($_FILES['Photo'])) {
$check = getimagesize($_FILES['Photo']['tmp_name']);  
}

将整个过程封装在if语句中。

另外,正如tadman所说,在mysql查询中使用预备语句 PDO 进行参数化查询。