我非常新的PHP和MySQL。我想創建一個PHP形式 數據將被存儲在插入HTML表格數據MySQL數據庫
我已經創建了兩個PHP文件
後端Mysql數據庫- form.php的
- insert.php
而且這兩個文件有一個require_once(DB_Login.php)函數 連接s到MySQL數據庫
form.php的的內容---
<html>
<head>
<body>
<form action="insert.php"
method="POST">
<?php
require_once('DB_Login.php');
$sql=mysql_query("Select * from Department");
echo '<table border=1px>';
echo '<th>DepartmentId</th> <th>Department_Name</th><th>
Description</th>';
$datas=array();
while($data=mysql_fetch_array($sql))
{
$datas[]=$data;
//Running a loop all contents in the Mysql Table
echo '<tr>';
echo '<td>.$datas[\'Dept_Id\'] </td> <td>.$datas[\'Dname\']</td>
<td>.$datas[\'Description\']</td>';
echo '</tr>';
}
echo'<tr>';
echo'<td><input type=\"text\" name=\"Dept_Id\" id=\"Dept_Id\"></td><td>
<input type=\"text\" name=\"Dname\" id=\"Dname\"></td> <td><input
type=\"text\" name=\"Description\" id=\"Description\"></td>';
echo'</tr>';
echo '</table>';
echo'<input type="submit" value="SEND">';
?>
</form>
</body>
</head>
</html>
insert.php--
<?php
require_once('DB_Login.php');
$Didtext = mysql_real_escape_string($_POST['Dept_Id']);
$Dnametext=mysql_real_escape_string($_POST['Dname']);
$Desctext=mysql_real_escape_string($_POST['Description']);
echo $Didtext;
echo $Dnametext;
$adddept="INSERT INTO Department('Dept_Id','Dname','Description')
values('$Didtext','$Dnametext','$Desctext')";
$result = mysql_query($adddept);
if($result)
{
echo("<br>Input data is succeed");
}
else
{
echo("<br>Input data failed");
}
?>
問題思考的內容是值未被傳遞給insert.php
$Didtext = mysql_real_escape_string($_POST['Dept_Id']);
$Dnametext=mysql_real_escape_string($_POST['Dname']);
$Desctext=mysql_real_escape_string($_POST['Description']);
我運行此代碼後,我收到消息輸入數據失敗。 我做了很多關於這個問題的搜索。 我正在使用ID屬性傳遞元素值insert.php
請幫忙!!!
對於初學者來說,實際上並沒有在SQL語句中使用變量。你使用硬編碼的字符串,每次都使用相同的字符串。除此之外,這個失敗究竟如何? 「我收到信息」是什麼意思?什麼不起作用? – David
在insert.php中,我處理了在Mysql中插入數據的失敗。 –
您將不得不更加具體。瞭解我們無法爲您調試。請執行一些調試並準確解釋失敗的原因。 – David