2012-10-15 44 views
-1

我想輸入數據到數據庫,但我不斷收到錯誤。我不知道問題出在哪裏。以下是代碼:使用php輸入數據從表格到數據庫使用php

<?php 
    $sql = "INSERT INTO $db_table (title,group,overlapok,userid,name,phone,email,description,managers,location,notify,notifyman, 
remind,remindman,appmsg,viewaccess,viewulist,viewclist,makeaccess,makeulist,makeclist,showappinfo) 
VALUES ('".mysql_real_escape_string(stripslashes($_REQUEST['txtTitle'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkGroupCal'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkAllowOverlap'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtUserID'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtOwner'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtPhone'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtEmail'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtDesc'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtManagers'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtLocation'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkNotifyMe'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkNotifyMgrs'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkRemind'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['chkRemindMan'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['txtAppText'])). 
"','".mysql_real_escape_string(stripslashes($_REQUEST['selViewBlockRestr'])). 
    "','".mysql_real_escape_string(stripslashes($_REQUEST['txtViewBlocksUserID'])) 
    "','".mysql_real_escape_string(stripslashes($_REQUEST['txtViewBlocksCourseID']))."','".mysql_real_escape_string(stripslashes($_REQUEST['selMakeApptRestr']))."','".mysql_real_escape_string(stripslashes($_REQUEST['txtMakeApptUserID']))."','".mysql_real_escape_string(stripslashes($_REQUEST['txtMakeApptDptID']))."','".mysql_real_escape_string(stripslashes($_REQUEST['chkShowAppInfo']))."')"; 

if($result = mysql_query($sql,$db)) 
    { 
    echo '<h1>Thank you</h1> Your information has been entered into our database<br><br> 
     <p> <a href="viewcalendar.page.php"> View Calendar </a> </p> 
     <img src="eDiary.jpg"'; 

    } 
    else 
    { 
    echo "ERROR: "; 
    } ?> 

該表單包含可以留空的複選框的部分。

我遵循了你的建議並使用了PDO。保留返回以下錯誤:警告:PDO :: __構造()[pdo .--構造]:[2002]在/ opt/lampp/htdocs/Scheduler/pages/enterCal中無效參數(嘗試通過unix連接://) .net.php on line 72 連接失敗:SQLSTATE [HY000] [2002]無效參數 致命錯誤:在/ opt/lampp/htdocs/Scheduler/pages/enterCal中調用非對象的成員函數exec .page.php on line 80.

我認爲$ db不是一個對象,並且連接沒有被執行。我該如何照顧?我試過谷歌,但似乎無法提出任何有效的方法。

<?php 

$dsn = 'mysql:host=localhost;dbname=eDiary'; 
$user = 'root'; 
$password = ''; 

try 
{ 
     $db = new PDO($dsn,$user,$password); 
     $db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);}     
catch (PDOException $e) { 
    echo 'Connection Failed: ' . $e->getMessage(); 
    } 

$result = $db->exec("INSERT INTO wassCalendar(title, group, overlapok, userid, name, phone, email, description, managers, location, notify, notifyman, remind, 
remindman, appmsg, viewaccess, viewulist, viewclist, makeaccess, makeulist, makeclist, showappinfo) 
VALUES($txtTitle, $chkGroupCal, $chkAllowOverlap, $txtUserID, $txtOwner, $txtPhone, $txtEmail, $txtDesc, $txtManagers, 
$txtLocation, $chkNotifyMe, $chkNofityMgrs, $chkRemind, $chkRemindMan, $txtAppText, $selViewBlockRestr, 
$txtViewBlocksUserID, $txtViewBlocksCourseID, $selMakeApptRestr, $txtMakeApptUserID, $txtMakeApptDptID, 
$chkShowAppInfo)"; 

$insert_id = mysql_insert_id(); 

?> 
+0

那麼,錯誤是什麼? http://php.net/manual/en/function.mysql-error.php 另外,使用mysql_函數是不鼓勵的,「改用PDO或MySQLi」。 –

+2

您的代碼以反引號開頭。這是錯誤的。另外,你得到的錯誤是什麼? –

+1

先用小得多的查詢嘗試。實際上還要檢查您發佈的代碼的語法(也可能是您在使用本網站的編輯工具時遇到困難,讓自己更適合使用它們)。並且確切的錯誤信息丟失,將其添加到您的問題中,以便清楚您實際運行哪個錯誤。 – hakre

回答

0

當需要使用在查詢用戶輸入的情況下,你會想參數化查詢。

Here是另一個話題,我涵蓋了同樣的問題。

依靠消毒輸入是非常糟糕的做法,並最終咬你。

相關問題