Jump to content

mysql database update


Atlis

Recommended Posts

I'm having problems updating my database, I have 4 fields i want to change. I checked all the { on the page, that's not the problem, I tried to echo information from the database and it displayed my information so that's not the problem, i tried yelling at my computer, that didn't work, i tried to input data into the database with the insert function it worked but is not practical in my situation. I'm probably going to face palm when i find out whats wrong, help please  :confused:

 

btw, the $_SESSION['usr'] was set in another page and works.

 

 


<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Info</title>
    
    <link rel="stylesheet" type="text/css" href="demo.css" media="screen" />
    
</head>

<body>

<div id="main">


<div class="container">
<font size="5" face="sans-serif">Change Settings <?php echo "{$_SESSION['usr']}"; ?></font>
	<form action="" method="POST">		
	<table cellpadding="3" cellspacinf="4" border="0">

<tr>
<td>Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="text" name="mf" /></td>
</tr>
<tr>
<td>Location</td>	
<td><input type="text" name="loc" /></td>	
</tr>
<tr>	
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>



<?php
if ($_POST['submit']){
define('INCLUDE_CHECK',true);
require 'connect.php';

$usr = $_SESSION['usr'];

$sql = 
mysql_query("UPDATE members 
SET name='{$_POST['name']}', age='{$_POST['age']}, mf='{$_POST['mf']}', loc='{$_POST['loc']}' 
WHERE usr='{$_SESSION['usr']}'");

if($sql){
echo 'Changes Saved!';

}else{
echo 'Error';
} 
}

?>
</div>	
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/253345-mysql-database-update/
Share on other sites

change this line:

$sql = 
mysql_query("UPDATE members 
SET name='{$_POST['name']}', age='{$_POST['age']}, mf='{$_POST['mf']}', loc='{$_POST['loc']}' 
WHERE usr='{$_SESSION['usr']}'");

 

TO:

$sql = 
mysql_query("UPDATE members 
SET name='$_POST[name]', age='$_POST[age]', mf='$_POST[mf]', loc='$_POST[loc]' 
WHERE usr='$_SESSION[usr]' ");

There's no need to change the query string; the syntax is fine as it's written.

Remove the query string from the query execution and assign it to a variable.

Use that variable in the query execution instead.

While developing, rather than simply echoing a generic error message, echo the query string along with mysql_error().

You aren't escaping or otherwise sanitizing any of the form data being used in your query string. That leaves you open to SQL injection, and at the very least, can cause query errors.

i figured it out, i changed some things around, and i put it in my functions file, and made it check for sql injection.

 

$usr = $_SESSION['usr'];
$name = $_POST['name'];
$age = $_POST['age'];
$mf = $_POST['mf'];
$loc = $_POST['loc'];


$sql = mysql_query("UPDATE `tz_members` SET `name` = '$name', `age` = '$age', `mf` = '$mf', `loc` = '$loc' 
WHERE `usr` = '$usr'");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.