php - 使用 PHP 更新数据库

标签 php mysql database

我正在尝试使用以下代码更新我的数据库记录,但一直没有运气。有人愿意帮忙吗?谢谢

<?php include "base.php"; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Project Sproom</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
if(!empty($_POST['username']) && !empty($_POST['email']))
{

$newusername = mysql_real_escape_string($_POST['username']);
$newemail = mysql_real_escape_string($_POST['email']);
$edit = mysql_query("UPDATE users (Username, EmailAddress) VALUES('".$newusername."', '".$newemail."') WHERE UserID=".$_SESSION['UserID']."");
// }
?>
<div id="container">
<div id="homemenu">
<ul id="navlist">
<li id="active"><a href="index.php" id="current">Home</a></li>
<li><a href="profile.php">Edit Profile</a></li>
</ul>
</div>
<div id="homemain">
<h1>Edit Profile</h1>
<p>This will be the edit profile when i have figured out how to do it...</p>
<br />
<form method="post" action="profile.php" name="editprofile" id="editprofile">
<label for="username">Username: </label> <input type="text" name="username" id="username" value="<?=$_SESSION['Username']?>"/><br />
<label for="email">E-Mail: </label> <input type="text" name="email" id="email" value="<?=$_SESSION['EmailAddress']?>"/> <br />
<input type="submit" name="editprofile" id="editprofile" value="Submit" />
</fieldset>
</form>
</div>
</div>
<?php 
} 
else
{
?>
<meta http-equiv="refresh" content="0;index.php">
<?php
}
?>

最佳答案

您正在为 UPDATE 查询使用 INSERT 语法。语法应该是这样的:

UPDATE users SET Username = 'username', EmailAddress = 'email' WHERE UserID = 1;

Docs here .

关于php - 使用 PHP 更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401410/

相关文章:

php - 如何使用 IN () 参数执行 PostgreSQL PREPARE 查询

php - 建议使用 php/mysql 将各种管道分隔文件导入基于缓冲区表的数据库中?

mysql - 扩展 CodeIgniter Security.php 以启用日志记录

java - 将 mysql 数据库值检索到数组中

mysql - 连接表上的附加外键

php - 尝试使用 PHP 和 html 显示来自 mysql 的数据

php - 查询 MySQL 数据库以插入特定记录

php - 关于使用 phpmyadmin 在 mysql 中创建关系的建议

mysql - 如果数字不在另一个表的逗号分隔列表中,是否可以将行插入到表中?

php - 将数据库中的数据显示为选择中的选项