无法在数据库中读取我的属性

时间:2015-10-17 08:07:45

标签: php html

<?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 [“pictures”] [“error”]为&amp; key =&gt; $ error)..... 可以为它提供任何解决方案吗?

<form action="add_action.php" method="post" name="send" onSubmit="return Check()"  enctype="multipart/form-data">
         食物名称:                              食物描述:                              食品价格:      $                    食物类型:               ---     appertizers     主修课     甜点                         餐厅名称:                              图片 :                         
<input name="btnSubmit" type="submit" class="inputButton" id="btnSubmit" value=" ADD " align="middle">
</form>

2 个答案:

答案 0 :(得分:0)

您的表单中可能会出现许多错误。

首先,您的表单输入名称和&_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;
?>`

答案 1 :(得分:0)

<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>