Jump to content

Recommended Posts

Hi, I want to update user information in the database but it doesn't do anything. No data entered upon form submission. Please anyone if you can help would be great. Thank you.

 

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host		= "";//edited out
$database 	= "";
$username 	= "";
$password 	= "";

$tbl_name   = "users";
$link = mysqli_connect($host, $username, $password);
$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($database);

session_start();

IF (isset($_SESSION['userid'])){
$userid=$_SESSION['userid'];
echo $userid;
}

//$currentUser = $_SESSION['myusername']; 

//do some cleanup//
IF (isset($_POST['submit'])){

$first =  $_POST['first'];
$last = $_POST['last'];
$dob = $_POST['dob'];
$gender =  $_POST['gender'];

$country =  $_POST['country'];
$state =   $_POST['state'];
$town =   $_POST['town'];
$zip =  $_POST['zip'];
$email =  $_POST['email'];



$first = mysql_real_escape_string( '$first');
$last = mysql_real_escape_string(  '$last');
$dob = mysql_real_escape_string(  '$dob');
$gender = mysql_real_escape_string(  '$gender');

$country = mysql_real_escape_string(  '$country');
$state = mysql_real_escape_string(  '$state');
$town = mysql_real_escape_string(  '$town');
$zip = mysql_real_escape_string(  '$zip');
$email = mysql_real_escape_string(  '$email');


};
IF (isset($_SESSION['userid'])){
$userid=$_SESSION['userid'];
}
ELSE{
$getuserid=mysql_query ("SELECT id FROM users ORDER BY id DESC limit 1") or die(mysql_error());
WHILE ($gtuserid = mysql_fetch_array($getuserid)) {
$theuserid=$gtuserid['id'];
$userid=$theuserid;
$_SESSION['userid']=$theuserid;
$userid=$_SESSION['userid'];
}//$getuserid
}// IF ELSE (isset($_SESSION['userid']))


/////UPDATE SECTION///// 
IF (isset($_POST['submit'])){
mysql_query ( "UPDATE users SET firstname='$first', lastname='$last', dob = '$dob', gender='$gender',    country='$country', state='$state', town='$town', zip='$zip', email='$email'   WHERE id=$userid") or die(mysql_error());  
}//IF ($_POST['update']=="Update")



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Connection</title>
<style type="text/css">	
body {
font-family:Calibri;
font-size:1em;
}
.title {
font-size:1.6em;
font-weight:strong;
}
.links a{
font-size::1.2em;
text-decoration:none;
}
.links a:hover{
font-size::1.2em;
color:#0066FF;
text-decoration:none;
}
</style>
</head>
<body>
<p><span class="title">Add your personal information/span></p>

<form action="thebeast.php" method="post">
    <p>
    <input type="text" name="first" size="20" id="first"  /> 
  First name<br />
    <input type="text" name="last" size="20"  id="name" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob"  ; } ?>
    Date of Birth<br />
    <input type="text" name="gender" size="20" id="gender"   />
  Gender <br />
  <input type="text" name="country" size="20" id="country"   />
  Country<br />
  <input type="text" name="state" size="20" id="state"   />
  State<br />
    <input type="text" name="town" size="20" id="town"  />
    Town<br />
    <input type="text" name="zip" size="20" id="zip"  />
Zip Code<br />
    <input type="text" name="email" size="40" id="email"   />
    Email<br />
    <br />
     
  
     

      
   <input type="submit" name="submit" value="Add your information" />
      
        
</form> 





</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/252963-updating-user-information-based-on-id/
Share on other sites

Change the update line to:

mysql_query ( "UPDATE users SET firstname='".$first."', lastname='".$last."', dob = '".$dob."', gender='".$gender."', country='".$country."', state='".$state."', town='".$town."', zip='".$zip."', email='".$email."'   WHERE id=".$userid."") or die(mysql_error());

 

 

If that doesn't work let me know.

thank you Karl. It updates the info, but where the user data should be in the row fields for that user, things like this are in there, literally string names. like this "$name", "dob", "$city". It is putting the variable names in there instead. any help greatly appreciated. here is the code as I adjusted it.

 

/////UPDATE SECTION///// 
IF (isset($_POST['submit'])){
mysql_query ( "UPDATE users SET firstname='".$first."', lastname='".$last."', dob = '".$dob."', gender='".$gender."', country='".$country."', state='".$state."', town='".$town."', zip='".$zip."', email='".$email."'   WHERE id=".$userid."") or die(mysql_error());
}//IF ($_POST['update']=="Update")

Hmm, try this instead:

mysql_query ( "UPDATE users SET firstname='{$first}', lastname='{$last}', dob = '{$dob}', gender='{$gender}', country='{$country}', state='{$state}', town='{$town}', zip='{$zip}', email='{$email}'   WHERE id={$userid}) or die(mysql_error());

Had a bit of a cleanup with your code so try this:

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host        = "";//edited out
$database     = "";
$username     = "";
$password     = "";

$tbl_name   = "users";
$link = mysqli_connect($host, $username, $password);
$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($database);

session_start();

if (isset($_SESSION['userid'])){
    $userid=$_SESSION['userid'];
    echo $userid;
}

//$currentUser = $_SESSION['myusername']; 

//do some cleanup//
if (isset($_POST['submit'])){

    $first =  $_POST['first'];
    $last = $_POST['last'];
    $dob = $_POST['dob'];
    $gender =  $_POST['gender']; 
    $country =  $_POST['country'];
    $state =   $_POST['state'];
    $town =   $_POST['town'];
    $zip =  $_POST['zip'];
    $email =  $_POST['email'];
    //Clean them
    $first = mysql_real_escape_string($first);
    $last = mysql_real_escape_string($last);
    $dob = mysql_real_escape_string($dob);
    $gender = mysql_real_escape_string($gender);
     
    $country = mysql_real_escape_string($country);
    $state = mysql_real_escape_string($state);
    $town = mysql_real_escape_string($town);
    $zip = mysql_real_escape_string($zip);
    $email = mysql_real_escape_string($email);

    mysql_query ( "UPDATE users SET firstname='{$first}', lastname='{$last}', dob = '{$dob}', gender='{$gender}', country='{$country}', state='{$state}', town='{$town}', zip='{$zip}', email='{$email}'   WHERE id={$userid}") or die(mysql_error());

}

if (isset($_SESSION['userid'])){
    $userid=$_SESSION['userid'];
} else {
    $getuserid=mysql_query ("SELECT id FROM users ORDER BY id DESC limit 1") or die(mysql_error());
    
    while ($gtuserid = mysql_fetch_array($getuserid)) {
        $theuserid=$gtuserid['id'];
        $userid=$theuserid;
        $_SESSION['userid']=$theuserid;
        $userid=$_SESSION['userid'];
    }//$getuserid
}// IF ELSE (isset($_SESSION['userid']))
  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Connection</title>
<style type="text/css">    
    body {
    font-family:Calibri;
    font-size:1em;
    }
    .title {
    font-size:1.6em;
    font-weight:strong;
    }
    .links a{
    font-size:1.2em;
    text-decoration:none;
    }
    .links a:hover{
    font-size:1.2em;
    color:#0066FF;
    text-decoration:none;
    }
</style>
</head>
<body>
<p><span class="title">Add your personal information/span></p>

<form action="thebeast.php" method="post">
    <p>
    <input type="text" name="first" size="20" id="first"  /> 
    First name<br />
    <input type="text" name="last" size="20"  id="name" />
    Last name<br />
    <input name="dob" type="text" size="20" id="dob"  ; } ?>
    Date of Birth<br />
    <input type="text" name="gender" size="20" id="gender"   />
    Gender <br />
    <input type="text" name="country" size="20" id="country"   />
    Country<br />
    <input type="text" name="state" size="20" id="state"   />
    State<br />
    <input type="text" name="town" size="20" id="town"  />
    Town<br />
    <input type="text" name="zip" size="20" id="zip"  />
    Zip Code<br />
    <input type="text" name="email" size="40" id="email"   />
    Email<br />
    <br /> 
   <input type="submit" name="submit" value="Add your information" />   
</form> 





</body>
</html>

hahaha Karl! you are awesome that totally made my night. It works perfectly. LOL. its the little things in php that make me happy, like seeing my data pop into a database all nice n tidy. thanks so much for helping me with that. have a great day/night.. Nice avatar btw.  :D :D :D

You're very welcome silverglade, glad I could help.

 

I remember starting out knowing nothing, coding a little bit and being happy, and then breaking it, getting annoyed because I couldn't fix it again. Then eventually fixing it and being over the moon ;)

i think the best part for me is that i finally read code like english. but it is the coming up with solved problems that is tough. I am posting another problem on the board next, I have a question about uploading images to a directory, I want to make an image gallery.

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.