2011-07-12 79 views
0

我剛開始使用php。我在php中編寫了一個簡單的代碼來將數據插入表客戶。我正在使用mssql數據庫。如何使用php在數據庫中插入數據

<?php 
function InsertData() 
{ 
    $link = mssql_connect('db','123','test'); 

    if (!$link || !mssql_select_db('php', $link)) 
     { 
     die('Unable to connect or select database!'); 
     } 

    $sql = " INSERT INTO customer ([Name],[Website])VALUES('$txtName','$txtWebsite')"; 
    $result = mysql_query($sql, $link); 
    if(!$result) 
     { 
     echo mysql_error(); 
     exit; 
     } 
    // close the connection 
    mysql_free_result($result); 
    mysql_close(); 
    echo "Data successfully inserted"; 
} 
?> 
     <table border="0" cellpadding="0" cellspacing="0" width="100%">     
     <tr> 
      <td colspan="3" height="10" valign="top" width="98%"></td> 
     </tr> 
     <tr> 
     <td width="22%"> &nbsp;Name:</td> 
     <td width="60%"><INPUT type="text" name="txtName" id="txtName" Width="200px" MaxLength="30" /> </td> 
     </tr> 
     <tr> 
     <td colspan="3" height="12" valign="top" width="98%"></td> 
     </tr> 
     <tr> 
     <td width="22%"> &nbsp;Company Website:</td> 
     <td width="60%"><INPUT type="text" name="txtWebsite" id="txtWebsite" width="200px" MaxLength="200" /> </td>     
     </tr> 
     <tr> 
     <td > </td> 
     <td > <input type="button" value="submit" id="imgBtnSubmit" click="InsertData()"/> </td> 
    </tr> 
</table> 

我寫上面的代碼將數據插入到表中,我對提交按鈕,但在單擊按鈕數據onClick事件調用InsertData功能沒有得到插入表中。請任何人告訴我這個代碼中的問題在哪裏。

+0

糟糕! PHP不是一種自然事件驅動的腳本語言。這是你的真實代碼嗎? – dpp

+0

它是你的真實代碼嗎?您不能使用'mssql_connect' +'mysql_query'並期望它的工作......這是不是在你的問題或一些印刷錯誤真的出現問題(即使mysql和mssql的法術類似,它們是不同的)。 – a1ex07

回答

1

看起來您的頁面中沒有<form> - 數據沒有傳到您的服務器。

另外,您並未在任何地方讀取輸入內容 - 您如何使用有意義的值填充$txtName$txtWebsite

此外,InsertData是一個PHP函數,這是對服務器端執行 - 你的HTML部分解釋在客戶端(瀏覽器)。你不能在那裏調用那個函數。

嘗試這樣的:

<?php 
function InsertData() 
{ 
    $link = mssql_connect('db','123','test'); 

if (!$link || !mssql_select_db('php', $link)) 
{ 
    die('Unable to connect or select database!'); 
} 
$txtName = $_REQUEST['txtName']; 
$txtWebsite = $_REQUEST['txtWebsite']; 

$sql = " INSERT INTO customer ([Name],[Website])VALUES('$txtName','$txtWebsite')"; 
$result = mssql_query($sql, $link); 
if(!$result) 
{ 
echo mssql_error(); 
exit; 
} 
// close the connection 
mssql_free_result($result); 
mssql_close(); 
echo "Data successfully inserted"; 
} 

if ($_REQUEST['txtName'] > ''){ 
InsertData(); 
} 
?> 
<form name="input" action="this_file_name.php" method="get"> 
     <table border="0" cellpadding="0" cellspacing="0" width="100%">     
     <tr> 
      <td colspan="3" height="10" valign="top" width="98%"></td> 
     </tr> 
     <tr> 
     <td width="22%"> &nbsp;Name:</td> 
     <td width="60%"><INPUT type="text" name="txtName" id="txtName" Width="200px" MaxLength="30" /> </td> 
     </tr> 
     <tr> 
     <td colspan="3" height="12" valign="top" width="98%"></td> 
     </tr> 
     <tr> 
     <td width="22%"> &nbsp;Company Website:</td> 
     <td width="60%"><INPUT type="text" name="txtWebsite" id="txtWebsite" width="200px" MaxLength="200" /> </td>     
     </tr> 
     <tr> 
     <td > </td> 
     <td > <input type="button" value="submit" id="imgBtnSubmit" /> </td> 
    </tr> 
</table> 
</form> 
+0

此外,我沒有看到whereInsertData被稱爲 –

+1

InsertData()調用提交按鈕onClick事件 – user99

+0

我認爲你不能調用InsertData()這樣的 –

3

由於以前的答案州,你實際上並沒有提交邏輯。至少同樣重要的是,你自己承認,沒有MySQL服務器。 MySQL和MS-SQL是不同的,完全不等價。您不能使用PHP MySQL命令或MySQL擴展與MS-SQL進行通信。

您正在使用MS-SQL的功能在這裏:

$link = mssql_connect('db','123','test'); 

    if (!$link || !mssql_select_db('php', $link)) 
     { 
     die('Unable to connect or select database!'); 
     } 

然後MySQL的位置:

$sql = " INSERT INTO customer ([Name],[Website])VALUES('$txtName','$txtWebsite')"; 
    $result = mysql_query($sql, $link); 
    if(!$result) 
     { 
     echo mysql_error(); 
     exit; 
     } 
    // close the connection 
    mysql_free_result($result); 
    mysql_close(); 
    echo "Data successfully inserted"; 

...而且,作爲另一個回答狀態,你想使用Javascript來調用一個PHP函數。

因此,您有兩個三個問題 - 好吧,從技術上說一個問題。我建議你至少在PHP中閱讀關於MS-SQL的FAQ,並且理想地完成Javascript和PHP教程。沒有辦法,我們可以提供一個答案,你可以用你的程序爲你寫短文,因爲你 - 我發誓我沒有在這裏不愉快,我知道你正在盡力 - 只是沒有知道如何把它放在一起呢。谷歌是你的朋友。祝你好運。

4

你的代碼有幾個問題。

  • 您錯過了Tudor Constantin指出的<form>
  • 另一個問題是,您不能使用元素的onclick事件觸發PHP代碼,因爲您已完成上述操作。 onclick事件適用於JavaScript(或其他客戶端腳本語言,如VBScript)。
  • 此外,如果您使用Microsoft SQL Server,請確保您使用的功能以mssql開頭,而不是mysql。這些功能不可互換。

這裏有一個簡單的例子:

<?php 

// The request method is POST. 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    // Connect to the database 
    $link = mssql_connect('db','123','test'); 

    // Do more stuff with the database. 
} 

?> 

<form method="POST" action="/path/to/this/file.php"> 
    <input type="text" name="example" /> 
    <input type="submit" value="Send" /> 
</form> 

當你點擊 「發送」 按鈕,表單將被張貼到/path/to/this/file.php。通過檢查請求方法是「POST」,您可以連接到數據庫並相應地插入記錄。

檢查$_SERVER['REQUEST_METHOD']是許多方法可以做到這一點。您還可以使用if (! empty($_REQUEST['input_name'])) { ... }來檢查特定輸入的值。

+0

@ user99:還請記住,您發佈的代碼很容易sql注入。避免這種情況的一種方法是使用準備好的語句/命名參數,就像您可以使用Microsoft的PDO_SQLSRV驅動程序一樣,請參閱http://msdn.microsoft.com/zh-cn/library/ff628175.aspx – VolkerK

2

正如許多人已經提到有代碼中的一些錯誤。我對您的問題的解決方案提供了不同的方法。

而不是喊你可以做一些事情的一個PHP文件中否則InsertData()方法。

這是通過使用if/else語句完成後,分配「提交」到提交按鈕的名稱值,以及特殊的PHP變量$ _ SERVER [「PHP_SELF」]。

首先,如果表單被提交,服務器檢查。
第一次加載頁面時,將返回false並顯示錶單以供他們填寫

當用戶單擊提交按鈕時,表單將重新加載頁面並再次運行PHP腳本。這次if語句返回true,因爲表單HAS已經被提交,所以它執行腳本的部分插入MSSQL中的數據並顯示成功的消息。

看看下面的代碼:

<?php 
if (isset($_POST['submit'])){ 
    //connect to the database 
    $link = mssql_connect('db','123','test');  
    //display error if database cannot be accessed 
    if (!$link || !mssql_select_db('php', $link)) 
    { 
     die('Unable to connect or select database!'); 
    } 
    //assign form input to variables 
    $txtName = $_POST['txtName']; 
    $txtWebsite = $_POST['txtWebsite']; 
    //SQL query to insert variables above into table 
    $sql = " INSERT INTO customer ([Name],[Website])VALUES('$txtName','$txtWebsite')"; 
    $result = mssql_query($sql, $link); 
    //if the query cant be executed 
    if(!$result) 
    { 
     echo mssql_error(); 
     exit; 
    } 
    // close the connection 
    mssql_free_result($result); 
    mssql_close(); 
    echo "Data successfully inserted"; 
} 
else { ?> 
    <form name="input" action="$_SERVER['PHP_SELF']" method="POST"> 
     <table border="0" cellpadding="0" cellspacing="0" width="100%">     
      <tr> 
       <td colspan="3" height="10" valign="top" width="98%"></td> 
      </tr> 
      <tr> 
       <td width="22%">Name:</td> 
       <td width="60%"><INPUT type="text" name="txtName" id="txtName" Width="200px" MaxLength="30" /> </td> 
      </tr> 
      <tr> 
       <td>Company Website:</td> 
       <td><INPUT type="text" name="txtWebsite" id="txtWebsite" width="200px" MaxLength="200" /></td>     
      </tr> 
      <tr> 
       <td><input type="button" name="submit" value="submit" /></td> 
      </tr> 
     </table> 
    </form> 
<?php } ?> 

給一個嘗試。這裏有一些非常棒的PHP教程,它們覆蓋了基礎知識: http://devzone.zend.com/article/627

希望所有的幫助。

相關問題