2013-05-29 63 views
1

夥計們這是我寫的代碼。我有兩個文件。PHP哎呀變量傳球錯誤

文件一。

$regname=$_POST['name']; -----> here the variable passed is john suppose.. 
$sponserid=$_POST['sname']; 
$regemail=$_POST['email']; 
$regmobile=$_POST['mobile']; 
include 'dbcon.php'; 
$obj = new dbcon; 
$obj->createUser($regname,$sponserid,$regemail,$regmobile); 
echo $obj; 

在上面的代碼中,我從窗體a獲取變量並存儲它們。然後我實例化一個對象並將所有這些傳遞給一個方法。

我的班級代碼id像這樣。

class dbcon 
{ 
    public function __construct() //This is the connection construct. 
{ 

     $server = "localhost"; 
     $user = "eplu"; 
     $pass = "123456"; //Change on hosting server 
     $db = "epl"; 
     mysql_connect($server, $user, $pass) or die("Error connecting to sql server: ".mysql_error()); 
     mysql_select_db($db); 
     } 


public function createUser($regname,$sponserid,$regemail,$regmobile){ 
     $sql = "INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES (`$regname`,`$sponserid`,`$regemail`,`$regmobile`)"; 
     mysql_query($sql) or die(mysql_error()); 
     return "Registration Success"; 
     } 

} 

我在'字段列表'中收到錯誤,如未知列'john'。新的OOPS請幫助... Thnx提前.....

+0

您正在使用錯誤的引號。看到這個問題:http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks –

+0

[**請不要在新的'mysql_ *'函數中使用代碼**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

3

立即嘗試。這是不是OOPS相關的錯誤,只是數據庫的東西。

class dbcon 
{ 
    public function __construct() //This is the connection construct. 
{ 

     $server = "localhost"; 
     $user = "eplu"; 
     $pass = "123456"; //Change on hosting server 
     $db = "epl"; 
     mysql_connect($server, $user, $pass) or die("Error connecting to sql server: ".mysql_error()); 
     mysql_select_db($db); 
     } 


public function createUser($regname,$sponserid,$regemail,$regmobile){ 
     $sql = "INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES ('$regname','$sponserid','$regemail','$regmobile')"; 
     mysql_query($sql) or die(mysql_error()); 
     return "Registration Success"; 
     } 

} 
2

它是因爲您在SQL查詢值中使用反引號。您需要使用單引號,而不是反引號,否則查詢就會認爲你正在引用另一列,在這種情況下,「約翰」

變化:

INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES (`$regname`,`$sponserid`,`$regemail`,`$regmobile`) 

到:

INSERT INTO onlinereg (names,sid,emails,mobiles) VALUES ('$regname','$sponserid','$regemail','$regmobile') 
+0

我現在試着引用這些錯誤。 缺少dbcon :: createUser()的參數1,在第24行中調用C:\ wamp \ www \ backup \ process \ dbcon.php並在C:\ wamp \ www \ backup \ process \ dbcon.php中定義第17行 – DarthVader

+0

這是一個完全不同的問題。我會建議更新您的原始問題。另外,確保你實際上將一個值傳遞給$ regname,IE確保'$ _POST ['name']'不是空的。 –

1

簡單改變

`name` 

'name' 你是使用錯誤的引號。此問題與OOP無關。