2012-03-02 23 views
0

我有一個表單似乎並不想將其數據寫入我的數據庫。我有點新的PHP MySQL。當我測試腳本時,頁面只會顯示「0」而重新加載。我不確定我錯過了什麼?任何幫助表示讚賞。php形式的mysql錯誤到數據庫

形式

<form action="new.php" method="POST"> 
     <table> 
     <tr> 
      <td>Season Number: </td> 
      <td><input type="text" name="season_sum" size="50" value="<? echo "$season_num";?>"></td> 
     </tr> 
     <tr> 
      <td>Episode Number: </td> 
      <td><input type="text" name="eps_num" size="50" value="<? echo "$eps_num";?>"></td> 
     </tr> 
     <tr> 
      <td>Temp Episode Number: </td> 
      <td><input type="text" name="temp_eps_num" size="50"></td> 
     </tr> 
     <tr> 
      <td>Title: </td> 
      <td><input type="text" name="title" size="50"></td> 
     </tr> 
     <tr> 
      <td>Description: </td> 
      <td><textarea type="text" name="descrip" cols="50" rows="7"></textarea></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="hidden" name="id"> 
      <input type="Submit" value="New Item"></td> 
     </tr> 
     </table> 
    </form> 

new.php

<?php 
require "db.php"; 

//Test for user input 
if (!empty($_POST[season_sum])&& 
    !empty($_POST[eps_num])&& 
    !empty($_POST[temp_eps_num])&& 
    !empty($_POST[title])&& 
    !empty($_POST[descrip])) 

if (! empty($_POST['ID'])) 
$id = (int)$_POST['ID']; 
else $id = 'NULL'; 

//Insert new entry 
$query = "INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '{$descrip}')"; 

// Send user back to the list once the update is successfully finished 
header("Location: form.html"); 
?> 

回答

3

禁用new.php以下行事件中的PHP代碼拋出一個錯誤:

//header("Location: form.html") 

然後你將需要使用mysql_query執行$查詢。

$query = "INSERT INTO ... "; 

mysql_query($query); 
+0

我使你的修改建議,但仍然有一個「0」提交時 – 2012-03-02 23:57:32

+0

一旦我回到列表壽,數據被正確輸入 – 2012-03-03 00:00:21

0

不確定「0」,但一般來說,你的代碼看起來像是你爲了可讀性而切碎了東西。如果不是......

if (!empty($_POST[season_sum]) && !empty($_POST[eps_num]) && !empty($_POST[temp_eps_num]) && !empty($_POST[title]) && !empty($_POST[descrip])) 
    { 

     if (!empty($_POST['ID'])) 
      $id = (int)$_POST['ID']; 
     else 
      $id = 'NULL'; 

     // mysql_real_escape_string() example 
     $descrip = mysql_real_escape_string($_POST['descrip']); 

     //Insert new entry 
     $query = mysql_query("INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '$descrip')") or die(mysql_error()); 

     // Send user back to the list once the update is successfully finished 
     header("Location: http://www.yoursite.com/form.html"); 
     exit; 
    } 

我沒有把在避開,因爲它是更容易只是建議你mysql_real_escape_string()包裹你的數據庫插入的字符串。除了你從不真正運行一個查詢,並且你沒有用花括號包裝你的if語句。我甚至不知道頁面在這種情況下會怎麼想。

嘗試應用這些更改並讓我們知道錯誤是否持續。

注 - 我在您的標題位置後添加了退出。另外,我在不同的地方放了一個完整的url路徑,我聽說這是更好的練習。儘管我沒有支持這一說法。只是我在某處聽到的一件事。

mysql_real_escape_string()說明: 要使用它,您必須打開一個連接。這通常在你的db類中處理,所以如果你發現它什麼都不做,請查看mysql_connect(); 要使用,只需調用就像這樣:

$sql = mysql_query("SELECT * FROM `table` WHERE `id` = '".mysql_real_escape_string($string_to_escape)."'"); 

它不會添加單引號包裝。它所做的只是幫助清理字符串以防止常見的SQL注入攻擊。

+0

看起來像我確實需要,因爲一些描述有加mysql_real_escape_string()引號在他們 – 2012-03-03 00:12:54

+0

你可以提供一個代碼示例,我將如何添加轉義字符串以讓我的'$ descript'包含引號。 – 2012-03-03 00:22:33

+0

你的意思是像下面這樣的東西? 'mysql_real_escape_string($ query);' – 2012-03-03 00:26:08