未定义的索引错误php

时间:2014-02-20 11:12:41

标签: php html sql database post

它如此haaaaaaaaaaaaard来修复这个未定义:( ..我怎么能解决这个家伙..

这是我的php代码

<?php
    $con = mysqli_connect("localhost", "root", "lns123", "lnsdb");

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "INSERT INTO tblreservation(position,tel,menu1,name,Add2,birth,civil,sex,height,weight,emailadd,religion,spouse,soccupation,father,foccupation,mother,moccupation,address,elem,date,high,hdate,college,course,skills,frm,to2,eposition,company)VALUES('$_POST[position]','$_POST[tel]','$_POST[menu1]','$_POST[name]','$_POST[Add2]','$_POST[birth]','$_POST[civil]','$_POST[sex]','$_POST[height]','$_POST[weight]','$_POST[emailadd]','$_POST[religion]','$_POST[spouse]','$_POST[soccupation]','$_POST[father]','$_POST[foccupation]','$_POST[mother]','$_POST[moccupation]','$_POST[address]','$_POST[elem]','$_POST[date]','$_POST[high]','$_POST[hdate]','$_POST[college]','$_POST[course]','$_POST[skills]','$_POST[frm]','$_POST[to2]','$_POST[eposition]','$_POST[company]')";

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

    mysqli_close($con);
?> 

并且错误是未定义索引全部在第146行

这是第146行

$sql="INSERT INTO tblreservation(position,tel,menu1,name,Add2,birth,civil,sex,height,weight,emailadd,religion,spouse,soccupation,father,foccupation,mother,moccupation,address,elem,date,high,hdate,college,course,skills,frm,to2,eposition,company)VALUES('$_POST[position]','$_POST[tel]','$_POST[menu1]','$_POST[name]','$_POST[Add2]','$_POST[birth]','$_POST[civil]','$_POST[sex]','$_POST[height]','$_POST[weight]','$_POST[emailadd]','$_POST[religion]','$_POST[spouse]','$_POST[soccupation]','$_POST[father]','$_POST[foccupation]','$_POST[mother]','$_POST[moccupation]','$_POST[address]','$_POST[elem]','$_POST[date]','$_POST[high]','$_POST[hdate]','$_POST[college]','$_POST[course]','$_POST[skills]','$_POST[frm]','$_POST[to2]','$_POST[eposition]','$_POST[company]')";

帮助我们:(感谢

3 个答案:

答案 0 :(得分:0)

将所有$ _POST变量分配给另一个变量。那是

 $name = $_POST['name']; 
 and so on....

在查询中使用这些变量(第146行)。

答案 1 :(得分:0)

首先,正如@Lix所说,检查所有$ _POST是否存在:

function post( $var ) {
    return ( isset( $_POST[$var] ) ? $_POST[$var] : false );
}

$position = post('position');
$tel = post('tel'); 
...

......以及所有变种等等。

然后,如果你所有的vars都在那里(通过检查它们就知道了!== false),你可以尝试将它插入你的DDBB中。但之前,请注意SQL注入(检查http://www.php.net/manual/en/mysqli-stmt.bind-param.php以了解下面的内容)

$stmt = mysqli->prepare(" INSERT INTO tblreservation(position,tel, ... )
VALUES (?, ?, ... )");
$stmt->bind_param('stmt', $position, $tel, ... );
$stmt->execute();

你将能够插入! XD

答案 2 :(得分:0)

使用mysqli_real_escape_string并用$ var替换$ _POST []以防止sql注入

$position = mysqli_real_escape_string( $_POST['position'] );
$tel      = mysqli_real_escape_string( $_POST['tel'] );
$menu1    = mysqli_real_escape_string( $_POST['menu1'] );

$sql = "INSERT INTO tblreservation(position,tel,menu1,...)
VALUES('$_POST[position]','$_POST[tel]','$_POST[menu1]',...)"