无法读取数据库中的属性


Cannot read my attributes in database

<?php
    header("Content-type: text/html; charset=utf-8");
    require('db_connect.php');
    mysql_query("SET NAMES 'utf8'");
    $food_name = $_POST['food_name'];
    $restaurant_name = $_POST['restaurant_name'];
    $food_type = $_POST['food_type'];
    $food_price = $_POST['food_price'];
    $food_description = $_POST['food_description'];
    $uploadfile;
    $dest_folder = "picture/";
    $arr = array();
    $count = 0;
    if(!file_exists($dest_folder)){
        mkdir($dest_folder);
    }
    foreach($_FILES["pictures"]["error"] as $key=> $error){
        if($error == UPLOAD_ERR_OK){
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
            $name = $_FILES["pictures"]["name"][$key];
            $uploadfile = $dest_folder.$name;
            move_uploaded_file($tmp_name,$uploadfile);
            $arr[$count] = $uploadfile;
            $count++;
        }
    }
    $s2 = implode(',',$arr);
    $sql = "insert into foodmenu 
            (food_name,restaurant_name,food_type,food_price,food_description,food_img) 
            values 
            ('$food_name','$restaurant_name','$food_type','$food_price','$food_description','$s2',now())";
    $result = mysql_query($sql);
    if($result){
        echo"<script>alert('Success')</script>";
        echo"<script>location.href='admin.php'</script>";
    } else {
        echo"<script>alert('Failure')</script>";
        echo"<script>history.back();</script>";
    }    
?>

我可以知道错误是什么吗?因为它无法读取我的$food_name,直到$food_description.....和foreach($_FILES["图片"]["错误"]作为&key=>$error(.....可以提供任何解决方案吗?

<form action="add_action.php" method="post" name="send" onSubmit="return Check()"  enctype="multipart/form-data">
食品名称 : 食品描述 : 食品价格 : $   食物类型 : --- 开胃菜 主要课程 甜点 餐厅名称 : 图像:
<input name="btnSubmit" type="submit" class="inputButton" id="btnSubmit" value=" ADD " align="middle">
</form>
<div class="listbox">
<div class="menu">
<br><br>      
<form action="add_action.php" method="post" name="send" onSubmit="return Check()"  enctype="multipart/form-data">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr>
<td width="180" align="right">Food Name :</td>
<td width="150">
<input name="food_name" type="text" class="food_namelist" style="width:300px;">
</td>
</tr>
<tr>
<td width="100" align="right">Food Description :</td>
<td width="222">
<textarea name="food_description" class="food_namelist" rows="3" style="height:100px; width:500px;"></textarea>
</td>
</tr>
<tr>
<td width="100" align="right">Food Price :</td>
<td width="222">

$  

<tr>
<td width="100" align="right">Food Type :</td>
<td width="222">
<select name="food_type">
<option value="" selected>---</option>
<option value="appertizers">appertizers</option>
<option value="main courses">main courses</option>
<option value="desserts">desserts</option>
</select>
</td>
</tr>
<tr>
<td width="180" align="right">Restaurant Name :</td>
<td width="222">
<input name="restaurant_name" type="text" class="food_namelist" style="width:300px;">
</td>
</tr>
<tr>
<td align="right">Images :</td>
<td style=" ">
<input type="uploadfile" name="pictures[]" />
</td>
</tr>
</table>
<input name="btnSubmit" type="submit" class="inputButton" id="btnSubmit" value=" ADD " align="middle">
</form>
</div>
</div>

在你的表格中可能会发生很多错误的事情。

第一个是,您的表单输入名称和&_POST['xyz']名称可能不匹配。

第二个是,请永远不要忘记将您的帖子值包装在 htmlspecialchars 中,例如$food_name = htmlspecialchars($_POST['food_name']);

你的价值观也不会被注入威胁抛出。因此,经过上述两次修改后,如果您仍然遇到问题,请附上您的表单html。我排除了你的db_connect.php。请根据需要编辑详细信息。SQL 代码::'

 <?php
 define("DB_DSN","mysql:hostname=localhost;dbname=tumy");
    define("DB_USR","root");
    define("DB_PASS","");
    $conn = new PDO(DB_DSN,DB_USR,DB_PASS);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    $food_name = htmlspecialchars($_POST['food_name']);
    $restaurant_name = htmlspecialchars($_POST['restaurant_name']);
    $food_type = htmlspecialchars($_POST['food_type']);
    $food_price = htmlspecialchars($_POST['food_price']);
    $food_description = htmlspecialchars($_POST['food_description']);
    $date = now();//create a column in your database named "date(or as wish)
    $dest_folder = "picture/";
    $arr = array();
    $count = 0;
    if(!file_exists($dest_folder)){
        mkdir($dest_folder);
    }
    foreach($_FILES["pictures"]["error"] as $key=> $error){
        if($error == UPLOAD_ERR_OK){
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
            $name = $_FILES["pictures"]["name"][$key];
            $uploadfile = $dest_folder.$name;
            move_uploaded_file($tmp_name,$uploadfile);
            $arr[$count] = $uploadfile;
            $count++;
        }
    }
    $s2 = implode(',',$arr);
    $sql = "INSERT INTO foodmenu 
            (food_name,restaurant_name,food_type,food_price,food_description,food_img,date) 
            VALUES
            ('$food_name','$restaurant_name','$food_type','$food_price','$food_description','$s2','$date')";
    $st = $conn->prepare($sql);
    if($st->execute()){
        echo"<script>alert('Success')</script>";
        echo"<script>location.href='admin.php'</script>";
    } else {
        echo"<script>alert('Failure')</script>";
        echo"<script>history.back();</script>";
    }    
    $conn = null;
?>`