我剛開始使用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%"> 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%"> 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功能沒有得到插入表中。請任何人告訴我這個代碼中的問題在哪裏。
糟糕! PHP不是一種自然事件驅動的腳本語言。這是你的真實代碼嗎? – dpp
它是你的真實代碼嗎?您不能使用'mssql_connect' +'mysql_query'並期望它的工作......這是不是在你的問題或一些印刷錯誤真的出現問題(即使mysql和mssql的法術類似,它們是不同的)。 – a1ex07