2010-12-02 108 views
1

任何想法爲什麼下面的代碼在用戶填寫表單後沒有向數據庫添加任何內容?我真的很感激它。PHP&MySQL INSERT INTO問題

謝謝!

if($_SESSION['loginSuccess']==1) { 

     // ============================================================ 
     // = Create the table of current tasks stored in the database = 
     // ============================================================ 
     $userID = $_SESSION['userID']; 
     $result = mysql_query("SELECT * FROM Tasks WHERE userID = '$userID'"); 
     echo "<div id=\"draggable\" class=\"ui-widget-content\"><table border='5'><tr class=\"ui-widget-header\"><td><u>Task Name:</u></td><td><u>Class:</u></td><td><u>Due Date:</u></td><td><u>Task Type:</u></td></tr>"; 
     echo $_SESSION['userID']; 
     while($row = mysql_fetch_array($result)) { 
      $taskName = $row[1]; 

      $class = $row[2]; 

      $taskDueDate = $row[3]; 

      $taskType = $row[4]; 


      echo "<tr><td>'$taskName'</td><td>'$class'</td><td>'$taskDueDate'</td><td>'$taskType'</td></tr>"; 
     } 
     echo "</table>"; 

     function addNewTask ($name, $class, $dueDate, $type) { 
      mysql_query("INSERT INTO Tasks VALUES ('$userID','$name', '$class', '$dueDate', '$type')"); 
     } 

    if($_POST['taskName'] != NULL) { 
      addNewTask($_POST['taskName'], $_POST['class'], $_POST['dueDate'], $_POST['dueDate']); 
     } 

?> 


<!-- <img border="1" alt="New" src="/newTask.png" id="newTask" onmouseClick="showTaskField"/> --> 
<p><form name="newTask" method="post" action="index.php" id="newTask"><br> 
    Task Name: <input name="taskName" type="text"> (necessary)<br> 
    Class: <input name="class" type="text"><Br> 
    Due Date: <input name="dueDate" type="text" id="datepicker"><Br> 
    Type: 
    <input type="submit"></p></div> 
+6

哇,哇,哇! [SQL注入](http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php)警報!請仔細閱讀以確保您的查詢(我認爲使用[PDO](http://php.net/pdo)和[預準備語句](http://php.net/manual/en/pdo.prepared-statements.php )是最好的解決方案,但有一些),然後檢查1)是否正在運行addNewTask? 2)查詢是否返回錯誤? (打印`mysql_error()`找出) – Matchu 2010-12-02 03:03:31

+0

調試你的mysql_query`調用,通過檢查它是否返回true或不使用`mysql_error`來判斷返回false的錯誤。 – deceze 2010-12-02 03:03:51

回答

2

試着擺脫'圍繞插入語句中的變量'。如果沒有任何迴應mysql_error()