2014-02-20 79 views
-2

其所以haaaaaaaaaaaaaard解決這個不確定:(..我該如何解決這個傢伙..未定義指數誤差PHP

這是我的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]')"; 

HELP ME GUYS :(謝謝

+4

Oy公司,OY OY OY OY ....請請請上閱讀起來(HTTP [SQL注入!]: //stackoverflow.com/questions/601300/what-is-sql-injection) - 你的代碼嚴重**易受攻擊。 – Lix

+1

SQL注入,我向你致敬! – raina77ow

+3

此外,您應該驗證您希望在'$ _POST'中的所有參數是否真的存在... – Lix

回答

0

將所有$ _POST變量賦值給另一個變量。那就是

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

並且在你的查詢中使用這些變量(第146行)。

0

首先,@Lix說,檢查所有的$ _ POST是否有:

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

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

...等等所有的瓦爾。

然後,如果你所有的變數都存在(你知道它通過檢查他們是!== 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

+0

最後,在編碼之前請先研究一下PHP和MySql,我們正在犯絕對的新手錯誤。從Sitepoint開始的一本很好的書是「PHP&MySQL:從新手到忍者(2012年5月,第5版)」。它會給你一個關於網站開發的良好基礎。 – Chococroc

0

使用mysqli_real_escape_string和替換$ _ POST []與$ VAR來防止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]',...)"