...

View Full Version : Database Update



Skippy
02-03-2010, 09:54 PM
<?php
require('connect.php');
if(!isset($_POST['submit'])){
$cresult = mysql_query("SELECT * FROM users");
$rusers = mysql_fetch_array($cresult);
$id = $_SESSION['id'];
?>
<h5>Edit Account Profile</h5>
<form method="post" action="">
Username: <?php echo $rusers['username']; ?><br/>
First Name: <input type="text" name="forename" class="form" value="<?php echo $rusers['forename']; ?>"><br/>
Surname: <input type="text" name="surname" class="form" value="<?php echo $rusers['surname']; ?>"><br/>
E-mail: <input type="text" name="email" class="form" value="<?php echo $rusers['email']; ?>"><br/>
Address: <input type="text" name="address1" class="form" value="<?php echo $rusers['address1']; ?>"><br/>
City: <input type="text" name="address2" class="form" value="<?php echo $rusers['address2']; ?>"><br/>
Postcode: <input type="text" name="postcode" class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
Country: <input type="text" name="country" class="form" value="<?php echo $rusers['country']; ?>"><br/>
<input type="submit" name="submit" value="Modify">
</form>
<?php
}else{
$id = $_SESSION['id'];
$forename = mysql_real_escape_string($_POST['forename']);
$surname = mysql_real_escape_string($_POST['surname']);
$email = mysql_real_escape_string($_POST['email']);
$address1 = mysql_real_escape_string($_POST['address1']);
$address2 = mysql_real_escape_string($_POST['address2']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$country = mysql_real_escape_string($_POST['country']);

mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");
echo "Your Account Profile has been updated.";
}
?>

I'm trying to update the forename, surname, email, address1, address2, postcode and country fields in a table in my database. However I can't seem to get it to update.
What am I doing wrong?

Thanks.

masterofollies
02-03-2010, 10:00 PM
Change to this


<?php
require('connect.php');
if(!isset($_POST['submit'])){
$cresult = mysql_query("SELECT * FROM users");
$rusers = mysql_fetch_array($cresult);
$id = $_SESSION['id'];

$forename = mysql_real_escape_string($_POST['forename']);
$surname = mysql_real_escape_string($_POST['surname']);
$email = mysql_real_escape_string($_POST['email']);
$address1 = mysql_real_escape_string($_POST['address1']);
$address2 = mysql_real_escape_string($_POST['address2']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$country = mysql_real_escape_string($_POST['country']);

mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");
echo "Your Account Profile has been updated.";



}
else {
?>
<h5>Edit Account Profile</h5>
<form method="post" action="">
Username: <?php echo $rusers['username']; ?><br/>
First Name: <input type="text" name="forename" class="form" value="<?php echo $rusers['forename']; ?>"><br/>
Surname: <input type="text" name="surname" class="form" value="<?php echo $rusers['surname']; ?>"><br/>
E-mail: <input type="text" name="email" class="form" value="<?php echo $rusers['email']; ?>"><br/>
Address: <input type="text" name="address1" class="form" value="<?php echo $rusers['address1']; ?>"><br/>
City: <input type="text" name="address2" class="form" value="<?php echo $rusers['address2']; ?>"><br/>
Postcode: <input type="text" name="postcode" class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
Country: <input type="text" name="country" class="form" value="<?php echo $rusers['country']; ?>"><br/>
<input type="submit" name="submit" value="Modify">
</form>
<?php
}
?>

Skippy
02-03-2010, 10:17 PM
I've updated my code to this but I still can't get it to update. :confused:


<?php
if(isset($_POST['submit'])){

$id = $_SESSION['id'];
$forename = mysql_real_escape_string($_POST['forename']);
$surname = mysql_real_escape_string($_POST['surname']);
$email = mysql_real_escape_string($_POST['email']);
$address1 = mysql_real_escape_string($_POST['address1']);
$address2 = mysql_real_escape_string($_POST['address2']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$country = mysql_real_escape_string($_POST['country']);

if(isset($error)){
echo "$error";
}else{
mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");

echo "Done.";
}
}else{
?>

<?php
$cresult = mysql_query("SELECT * FROM users");
$rusers = mysql_fetch_array($cresult);
$id = $_SESSION['id'];
?>
<h5>Edit Account Profile</h5>
<form method="post" action="">
Username: <?php echo $rusers['username']; ?><br/>
First Name: <input type="text" name="forename" class="form" value="<?php echo $rusers['forename']; ?>"><br/>
Surname: <input type="text" name="surname" class="form" value="<?php echo $rusers['surname']; ?>"><br/>
E-mail: <input type="text" name="email" class="form" value="<?php echo $rusers['email']; ?>"><br/>
Address: <input type="text" name="address1" class="form" value="<?php echo $rusers['address1']; ?>"><br/>
City: <input type="text" name="address2" class="form" value="<?php echo $rusers['address2']; ?>"><br/>
Postcode: <input type="text" name="postcode" class="form" value="<?php echo $rusers['postcode']; ?>"><br/>
Country: <input type="text" name="country" class="form" value="<?php echo $rusers['country']; ?>"><br/>
<input type="submit" name="submit" value="Modify">
</form>
<?php }
include("template/footer.php");
?>

tomws
02-03-2010, 10:54 PM
You're not testing whether the query is successful. A quick test would be to add or die(mysql_error()) after the mysql_query, like this:

mysql_query("your query here") or die(mysql_error());
It shouldn't be used like that in production, but since you have no error checking at all, it can't be any worse.

thekooliest
02-03-2010, 11:52 PM
Try replacing:


mysql_query("UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'");

echo "Done.";

With:


$query = "UPDATE users SET forename='$forename', surname='$surname', email='$email', address1 = '$address1', address2 = '$address2', postcode = '$postcode', country='$country' WHERE id='$id'";
echo $query;
mysql_query($query) or die(mysql_error());


And tell me what gets displayed...
-Sam



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum