我有了在我已插入使用HTML表单的一些数据两列(名称和说明)MySQL表存在于行单元格中的MySQL表。如何更新使用PHP
但是我试图找出是否存在,如果用户输入现有的名称和不同的描述,以便更新为现有名称,而不是与现有的名称和新的说明中添加新行的描述方式。
这是我的PHP代码:
<html>
<head>
<meta charset = "utf-8">
<title>Search Results</title>
<style type = "text/css">
body { font-family: sans-serif; }
table { background-color: lightblue;
border-collapse: collapse;
border: 1px solid gray; }
td { padding: 5px; }
tr:nth-child(odd) {
background-color: white; }
</style>
</head>
<body>
<?php
$db_connect = mysql_connect("hostname", "name", "password");
$db_query = "SELECT * FROM urltable";
// connect to MySQL
if (!$db_connect)
{
die("Could not connect to database");
}
// open Products database
if (!mysql_select_db("urls", $db_connect))
die("Could not open products database </body></html>");
$db_insert = "INSERT INTO urltable(URL, description) VALUES ('$URL', '$Description')";
$db_insert_data = mysql_query($db_insert);
if ($db_insert_data)
{
echo("<br>Input data is Added</br>");
}
else
{
echo("<br>Input data not Added</br>");
}
// query Products database
if (!($result = mysql_query($db_query, $db_connect)))
{
print("<p>Could not execute query!</p>");
die(mysql_error() . "</body></html>");
} // end if
mysql_close($db_connect);
?><!-- end PHP script -->
<table border="1">
</br>
<caption>URL TABLE</caption>
</br>
<?php
// build table headings
print("<tr>");
print("<th>URL</th>");
print("<th>Description</th>");
print("</tr>");
// fetch each record in result set
while ($row = mysql_fetch_row($result))
{
// build table to display results
print("<tr>");
foreach ($row as $value)
print("<td>$value</td>");
print("</tr>");
} // end while
?><!-- end PHP script -->
</table>
</br>
<a href="insert.html">Home</a></br>
</body>
</html>
现在它只是插入输入任何数据,但我无法弄清楚如何做说明的更新只有在输入现有的名称。我可以使用条件语句来检查现有名称吗?
一旦你得到了它的工作,修复SQL注入漏洞。 – Joe