Updating a database using PHP

I am trying to update my database records with the following code, but I was not lucky that it was. Does anyone have to help? Thanks

<?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
}
?>
+3
source share
3 answers

You use the INSERT syntax to query UPDATE. The syntax should look like this:

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

The docs are here .

+3
source

You have not connected to the MySQL database, have you?

I have not seen this in this code ...

Or is it part of the included "base.php" on top of this script?

I am afraid that you need to first establish a connection to a specific MySQL database before trying to update a row in a table.

Edit:

, . :

echo "edit was " .$edit;

, ( ) ( ).

, , mysql_query.

0
$edit = mysql_query("UPDATE users SET Username='".$newusername."', EmailAddress='".$newemail."' WHERE  UserID=".$_SESSION['UserID']."");

try it

-1
source

Source: https://habr.com/ru/post/1757913/


All Articles