如何使用PHP更新MySQL表中一行中的现有单元格
我有一个MySQL表,它有两列(名称和描述),我已经使用HTML表单在其中插入了一些数据 然而,我试图弄清楚,如果用户输入现有名称和不同的描述,是否有办法更新该现有名称的描述,而不是使用现有名称和新描述添加新行 以下是我的PHP代码:如何使用PHP更新MySQL表中一行中的现有单元格,php,html,Php,Html,我有一个MySQL表,它有两列(名称和描述),我已经使用HTML表单在其中插入了一些数据 然而,我试图弄清楚,如果用户输入现有名称和不同的描述,是否有办法更新该现有名称的描述,而不是使用现有名称和新描述添加新行 以下是我的PHP代码: <html> <head> <meta charset = "utf-8"> <title>Search Results</title> <style type = "text/css"&g
<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>
搜索结果
正文{字体系列:无衬线;}
表{背景色:浅蓝色;
边界塌陷:塌陷;
边框:1px实心灰色;}
td{padding:5px;}
tr:n个孩子(奇数){
背景色:白色;}
我们使用更新
"UPDATE tablename SET cloumnname = 'newname' WHERE ID = 'idoftheperson'"
也不要使用mysql\u query()
请使用PDO库或mysqli
下面是如何在php中使用UPDATE命令的教程
若要检查数据库中是否存在现有名称或id,则需要统计mysql中的数据。为此,请这样做
<?php
/* Open a connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) >0){
/* do the mysql update here */
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
谢谢我们使用更新
"UPDATE tablename SET cloumnname = 'newname' WHERE ID = 'idoftheperson'"
也不要使用mysql\u query()
请使用PDO库或mysqli
下面是如何在php中使用UPDATE命令的教程
若要检查数据库中是否存在现有名称或id,则需要统计mysql中的数据。为此,请这样做
<?php
/* Open a connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) >0){
/* do the mysql update here */
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
谢谢您应该看看MySQL中的UPDATE命令
埃尔德姆的回答是无效的
它应该是这样的:
$db_update = "UPDATE urltable SET description = \"New description\" WHERE url = \"http://urlyouwanttoupdate.com\"";
mysql_query($db_update);
补充乔说的话。出于安全目的,您不能允许用户的输入直接进入查询。这使您的数据库容易受到称为“SQL注入”的攻击。网上已经有很多关于这方面的内容
下面是一个安全的例子,说明您正在使用的update和insert函数
$safe_description = mysql_real_escape_string($Description);
$safe_url = mysql_real_escape_string($URL);
$db_update = "UPDATE urltable SET description=\"".$safe_description."\" WHERE url =\"".$safe_url."\"";
mysql_query($db_update);
为了安全插入:
$db_insert = "INSERT INTO urltable(URL, description) VALUES (\"".$safe_url."\", \"".$safe_description."\")";
$db_insert_data = mysql_query($db_insert);
您应该看看MySQL中的UPDATE命令
埃尔德姆的回答是无效的
它应该是这样的:
$db_update = "UPDATE urltable SET description = \"New description\" WHERE url = \"http://urlyouwanttoupdate.com\"";
mysql_query($db_update);
补充乔说的话。出于安全目的,您不能允许用户的输入直接进入查询。这使您的数据库容易受到称为“SQL注入”的攻击。网上已经有很多关于这方面的内容
下面是一个安全的例子,说明您正在使用的update和insert函数
$safe_description = mysql_real_escape_string($Description);
$safe_url = mysql_real_escape_string($URL);
$db_update = "UPDATE urltable SET description=\"".$safe_description."\" WHERE url =\"".$safe_url."\"";
mysql_query($db_update);
为了安全插入:
$db_insert = "INSERT INTO urltable(URL, description) VALUES (\"".$safe_url."\", \"".$safe_description."\")";
$db_insert_data = mysql_query($db_insert);
一旦你让它工作,修复SQL注入错误。一旦你让它工作,修复SQL注入错误。我的答案是正确的。无论如何,你不应该使用mysql\u查询。我看你已经修复了它。您之前说过:“UPDATE tablename SET columnname='newname'WHERE='ID'。我的答案是正确的。而且您无论如何都不应该使用mysql\U查询。我看到您已经修复了它。您之前说过:“UPDATE tablename SET columnname='newname'WHERE='ID'。来自原始帖子的查询工作正常。也不需要担心注入攻击,这只会在localhost上运行一两周。它永远不会上线。其突出部分用于测试目的。再次感谢!从你原来的帖子中的查询可以正常工作。也不需要担心注入攻击,这只会在localhost上运行一两周。它永远不会上线。其突出部分用于测试目的。再次感谢!