Jump to content

Recommended Posts

Hello All,

 

So I've got a update user info form, the only problem is it's not updating. It draws the data from the database fine and displays it in the fields, but when you change the fields and click "update" it doesn't update.

 

Here's the update page:

<?
// Start a session
session_start();

// Sends the user to the login-page if not logged in
if(!session_is_registered('member_ID')) :
header('Location: index.php?msg=requires_login');
endif;

include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$user = $_SESSION["member_ID"];
$query="SELECT * FROM members WHERE id='$user";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$username=mysql_result($result,$i,"username");
$firstname=mysql_result($result,$i,"firstname");
$lastname=mysql_result($result,$i,"lastname");
$address=mysql_result($result,$i,"address");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
Student ID: <? echo "$username"?><br>
First Name: <? echo "$firstname"?><br>
Last Name: <? echo "$lastname"?><br>
Address: <input type="text" name="ud_address" value="<? echo "$address"?>"><br>
City: <input type="text" name="ud_city" value="<? echo "$city"?>"><br>
State: <input type="text" name="ud_state" value="<? echo "$state"?>"><br>
Zip: <input type="text" name="ud_zip" value="<? echo "$zip"?>"><br>
Phone: <input type="text" name="ud_phne" value="<? echo "$phone"?>"><br>
Email: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
} 
?>

 

 

And here's the updated.php script:

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);

$query="UPDATE members SET address='$ud_address', city='$ud_city', state='$ud_state', zip='$ud_zip', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

 

Any help would be great!! Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/156474-solved-update-users-info-not-updating/
Share on other sites

Display errors when you issue the update query...

 

$query="UPDATE members SET address='$ud_address', city='$ud_city', state='$ud_state', zip='$ud_zip', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";
mysql_query($query) or die(mysql_error());

There are a couple issues you have here:

 

1) Never use short-hand tags (), always use <?php.

2) Your form doesn't specify a method.

 

Change this line to:

 

</pre>
<form action="updated.php" method="POST"><

 

3) You also need to use that method on the action page (updated.php) i.e.:

 

$ud_address = mysql_real_escape_string($_POST['ud_address']);
$ud_city = mysql_real_escape_string($_POST['ud_city']);
$ud_state = mysql_real_escape_string($_POST['ud_state']);
// etc...

 

(The mysql_real_escape_string prevents SQL Injections.)

Maq,

I tried to PM you but it wouldn't work.

 

Do you mean my action page needs to look like this:

 

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);

$query="UPDATE members SET
$ud_address = mysql_real_escape_string($_POST['ud_address']);
$ud_city = mysql_real_escape_string($_POST['ud_city']);
$ud_state = mysql_real_escape_string($_POST['ud_state']);
$ud_zip = mysql_real_escape_string($_POST['ud_zip']);
$ud_phone = mysql_real_escape_string($_POST['ud_phone']);
$ud_email = mysql_real_escape_string($_POST['ud_email']);
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query) or die(mysql_error());
echo "Record Updated";
mysql_close();
?>

Sorry, I still can't figure out what you mean. I'm still new to all this PHP stuff. Should I be doing this:

 

<?php

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

 

$query="UPDATE members SET address='$ud_address', city='$ud_city', state='$ud_state', zip='$ud_zip', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";

$ud_address = mysql_real_escape_string($_POST['ud_address']);

$ud_city = mysql_real_escape_string($_POST['ud_city']);

$ud_state = mysql_real_escape_string($_POST['ud_state']);

$ud_zip = mysql_real_escape_string($_POST['ud_zip']);

$ud_phone = mysql_real_escape_string($_POST['ud_phone']);

$ud_email = mysql_real_escape_string($_POST['ud_email']);

@mysql_select_db($database) or die( "Unable to select database");

mysql_query($query);

echo "Record Updated";

mysql_close();

?>

localhost is not a constant.

 

<?php
include("dbinfo.inc.php");
mysql_connect('localhost',$username,$password) or die("Unable to connect to server");
mysql_select_db($database) or die( "Unable to select database");

$ud_address = mysql_real_escape_string($_POST['ud_address']);
$ud_city = mysql_real_escape_string($_POST['ud_city']);
$ud_state = mysql_real_escape_string($_POST['ud_state']);
$ud_zip = mysql_real_escape_string($_POST['ud_zip']);
$ud_phone = mysql_real_escape_string($_POST['ud_phone']);
$ud_email = mysql_real_escape_string($_POST['ud_email']);

$query="UPDATE members SET address='$ud_address', city='$ud_city', state='$ud_state', zip='$ud_zip', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

Ah, I figured it out!!! Thanks Maq for pointing me in the right direction!!!

 

For future reference here's the final code:

 

Update Page:

<?php
// Start a session
session_start();

// Sends the user to the login-page if not logged in
if(!session_is_registered('member_ID')) :
header('Location: index.php?msg=requires_login');
endif;

include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id = $_SESSION["member_ID"];
$query="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$username=mysql_result($result,$i,"username");
$firstname=mysql_result($result,$i,"firstname");
$lastname=mysql_result($result,$i,"lastname");
$address=mysql_result($result,$i,"address");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
?>

<form action="updated.php" method="POST">
<input type="hidden" name="ud_id" value="<?php echo "$id"; ?>">
Student ID: <?php echo "$id"?><br>
First Name: <?php echo "$firstname"?><br>
Last Name: <?php echo "$lastname"?><br>
Address: <input type="text" name="ud_address" value="<?php echo "$address"?>"><br>
City: <input type="text" name="ud_city" value="<?php echo "$city"?>"><br>
State: <input type="text" name="ud_state" value="<?php echo "$state"?>"><br>
Zip: <input type="text" name="ud_zip" value="<?php echo "$zip"?>"><br>
Phone: <input type="text" name="ud_phone" value="<?php echo "$phone"?>"><br>
Email: <input type="text" name="ud_email" value="<?php echo "$email"?>"><br>
<input type="Submit" value="Update">
</form>

<?php
++$i;
} 
?>

 

 

Action Script (updated.php):

<?
$ud_id=$_POST['ud_id'];
$ud_address=$_POST['ud_address'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_zip=$_POST['ud_zip'];
$ud_phone=$_POST['ud_phone'];
$ud_email=$_POST['ud_email'];

include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);

$query="UPDATE members SET address='$ud_address', city='$ud_city', state='$ud_state', zip='$ud_zip', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Thank You For Updating Your Records";
mysql_close();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.