所以我正在尝试使用html表单和一些PHP代码更新数据库字段,但是我无法让它工作,它抛出没有错误但不更新字段?,我不确定它是否因为我也回应网页上的那个字段?它似乎只是打印失败消息。
HTML:
<html>
<form method="post" name="update" action="updateform.php" />
Description:
<input type="text" name="description" />
<input type="submit" name="Submit" Value="update" />
</form>
</html>
PHP:
<?php
mysql_connect("localhost", "root", "*****") or die("Connection Failed");
mysql_select_db("Days")or die("Connection Failed");
$description = $_POST['description'];
$query = "UPDATE test SET description = '$description' ";
if(mysql_query($query)){ echo "updated";} else{ echo "fail";} ?>
我的回声(工作):
<?php
include("include/session.php");
//connect to the server
$connect = mysql_connect("localhost","root","*****");
//connect to the database
mysql_select_db("days");
//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
//ferch the results / convert results into an array
WHILE($rows = mysql_fetch_array($query)):
$description = $rows['description'];
echo "<div style ='font:15px/21px Arial,tahoma,sans-serif;color:#cf5c3f </h>'>$description";
endwhile;
?>
答案 0 :(得分:1)
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
更新查询示例,但我没有得到你的sql ..你错过了where
子句
答案 1 :(得分:0)
每当您尝试将某些内容更新为表格时,您都需要使用WHERE
条件。
这是我的代码:
<强>的test.html 强>
<html>
<form method="post" action="updateform.php" />
Name : <input type="text" name="name" /> </br>
<input type="submit" name="Submit" value="update" />
</form>
</html>
<强> updateform.php 强>
<?php
$name = $_POST['name'];
$connection = mysqli_connect("localhost", "root", "Enter Passwd Here","Enter db_name here");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit']))
{
$query = "UPDATE `test_table` SET `name` = '$name' WHERE `cost` = 500";
$result = mysqli_query($connection,$query);
if (!$result) {
die('Error' . mysqli_error($connection));
}
else
{
echo "Successfully updated";
}
}
?>
为了证明我已经创建了一个数据库&amp;表格test_table
,包含3个字段。 (ID,姓名,成本)
这是我桌子的结构:
在执行上述脚本之前,我们的表包含此数据
执行脚本后,第二行中的名称从ramu
更改为shiva
,因为我们在cost
条件中将WHERE
指定为500
$query = "UPDATE `test_table` SET `name` = '$name' WHERE `cost` = 500";