我如何修复这些错误 - 重定向到我的服务器(maby)

时间:2011-01-31 23:45:27

标签: php mysql

代码

<?php 

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename ($_FILES['photo']['did']); 

$did = $_POST['did']; 
$name = $_POST['dname']; 
$disc = $_POST['ddisc']; 
$price = $_POST['dprice']; 
$pic=($_FILES['photo']['did']); 

print_r($_POST); 
session_start(); 
$con = mysql_connect("","restoraunt","123456"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("restoraunt", $con); 

//Writes the information to the database 
mysql_query("INSERT INTO `menu` VALUES ('$did', '$name', '$disc', '$price','a','$pic')") ; 



/*************************/ 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['did']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
/**************************/ 



if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error()); 
} 
echo "1 record added"; 

mysql_close($con); 

?> 

错误:

  

注意:未定义索引:照片中   D:\ WEB \ Sites \ Restoraunt \ addDish.php on   第5行

     

注意:未定义索引:照片中   D:\ WEB \ Sites \ Restoraunt \ addDish.php on   第17行

Array ( 
    [did] => 90
    [dname] => uhh 
    [ddisc] => jhjjhh
    [dprice] => 12
    [dcateg] => Appetiser
    [photo] => Penguins.jpg 
    [Add] => Add 
)  
     

注意:   未定义的索引:照片输入   D:\ WEB \ Sites \ Restoraunt \ addDish.php on   第40行抱歉,有问题   上传您的文件。注意:未定义   变量:sql in   D:\ WEB \ Sites \ Restoraunt \ addDish.php on   第55行错误:查询为空

3 个答案:

答案 0 :(得分:1)

$ _FILES数组具有特定的格式和布局,并且您的密钥(例如did)将不存在。标准布局是:

$_FILES = array(
    'name of your input type="file" field' => array(
          'name' => 'name of file on client machine',
          'type' => 'mime type of file, as provided by the client',
          'size' => 'size of uploaded file',
          'tmp_name' => 'name of server-side temporary file the upload was stored in',
          'error' => 'error code of upload operation'
    )
);

因此,$_FILES['photo']['did']将返回无效索引,因为_FILES数组中没有did元素。

数组结构和错误代码的完整详细信息分别为herehere

SQL错误是由于您尝试运行查询两次。将查询字符串直接在mysql_query()函数调用中作为参数,稍后再使用未定义的$sql变量。

同样,您正在引用两个单独的文件上传:

$_FILES['photo']
$_FILES['uploadedfile']

在不同的地方。应该是哪一个?

答案 1 :(得分:0)

第一条建议:绝不信任用户,总是验证和过滤输入

$did = filter_input(INPUT_POST, 'did');
$name = filter_input(INPUT_POST, 'dname');
$disc = filter_input(INPUT_POST, 'ddisc');
$price = filter_input(INPUT_POST, 'dprice');
$photo = (isset($_FILES['photo'])) ? $_FILES['photo'] : null;

然后,$_FILES['photo']$sql不存在。 Allways使用isset()empty() ...

测试存在

答案 2 :(得分:0)

解决了大多数错误 - 仍然有一个

错误:

注意:未定义的索引:第33行的D:\ WEB \ Sites \ Restoraunt \ addDish.php中的上传文件 该文件已上载,您的信息已添加到directoryError:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“资源ID#2”附近使用正确的语法

代码:

[PHP]<?php
session_start();

//This is the directory where images will be saved
$target_path = "images/";
$target = $target_path . basename ($_FILES['photo']['name']);

$did = $_POST['did'];
$name = $_POST['dname'];
$disc = $_POST['ddisc'];
$price = $_POST['dprice'];
$pic=($_FILES['photo']['name']);

$con = mysql_connect("","restoraunt","123456"); // àéôä ùí äùøú ùìê, localhost àå îùäå?
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("restoraunt", $con);

//Writes the information to the database
mysql_query("INSERT INTO `menu` VALUES ('$did', '$name', '$disc', '$price','a','$pic')") ;
// àéôä ùîåú äòîåãåú áèáìä ùìê áãàèàáééñ?
// insert into menu (c1, c2, c3, c4) values('$c1', '$c2', '$c3', '$c4');

/*************************/
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
/**************************/

if (!mysql_query($con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con);
?>[/PHP]