我不明白爲什麼這個PHP文件的值沒有被插入到我的SQL數據庫中。該代碼幾乎與我用於其他表格的正在運行的PHP文件完全相同。代碼如下:值沒有被插入SQL數據庫
<?php
mysql_connect("xxx","xxx","xxx");
mysql_select_db("andrew_history");
$Name=$_POST['Name'];
$Year=$_POST['Year'];
$Press=$_POST['Press'];
$Description = $_POST['Description'];
$Title = $_POST['Title'];
$dbLink = mysql_connect("xxxx", "xxxx", "xxxxxx");
mysql_query("SET character_set_client=utf8", $dbLink);
mysql_query("SET character_set_connection=utf8", $dbLink);
if($submit)
{
if($Name&&$Year&&$Press&&$Description&&$Title)
{
$insert=mysql_query("INSERT INTO historytable (Name,Year,Press,Description,Title) VALUES ('$Name','$Year','$Press', '$Description', '$Title') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=history.php'>";
}
else
{
echo "please fill out all fields";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>History</title>
</head>
<body>
<center>
<form action="history.php" method="POST">
<table>
<tr><td>Name: <input type="text" name="Name"/></td></tr>
<tr><td>Year: <input type="text" name="Year"/></td></tr>
<tr><td>Press: <input type="text" name="Press"/></td></tr>
<tr><td>Title: <input type="text" name="Title"/></td></tr>
<tr><td colspan="2">Description: </td></tr>
<tr><td colspan="5"><textarea name="Description" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Description"></td></tr>
</table>
</form>
<?php
$dbLink = mysql_connect("xxxx", "xxxx", "xxxxx");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
$getquery=mysql_query('SELECT * FROM historytable ORDER BY id DESC');
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
$Name=$rows['Name'];
$Year=$rows['Year'];
$Press=$rows['Press'];
$Description=$rows['Description'];
$Title=$rows['Title'];
echo $Name . $Year . '<br/>' . '<br/>' . $Press . '<br/>' . '<br/>' . $Description . $Title;
}
?>
</body>
</html>
鏈接到網站:http://aswanson.net/Work/history.php
您可以查看任何錯誤的'mysql_error'。但!! mysql_ *函數從PHP7開始已被棄用和刪除。而這只是不好的做法,因爲你已經開放了SQL注入。 (想象一下名叫O'Leary的人)。現在切換到PDO或mysqli,然後再過分。 – aynber
您正在測試$ submit,但您在設置POST中的其他變量時未設置$ submit。另外,您需要過濾您的輸入並參數化您的查詢。並且使用PDO或mysqli_作爲您正在使用的方法已被棄用 – VikingBlooded
'if($ submit)'我認爲這是錯誤。您沒有定義$提交變量 –