Reputation: 23
Hey guys i have a problem updating input data, once I press to refresh load the page but the previous values remain and the database if it is updated. Neither refreshing the page appear the new data, I have to logout and re-enter.
use this
function account_profile( array $data ) {
if ( !empty( $data ) ) {
$trimmed_data = array_map( 'trim', $data );
$user_id = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'user_id' ] );
$first_name = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'first_name' ] );
$last_name = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'last_name' ] );
$address = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'address' ] );
$city = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'city' ] );
$telephone = mysqli_real_escape_string( $this->_con, $trimmed_data[ 'telephone' ] );
$query = "UPDATE users SET first_name = '$first_name', last_name = '$last_name', address = '$address', city = '$city', telephone = '$telephone' WHERE user_id = '$user_id'";
if ( mysqli_query( $this->_con, $query ) ) {
mysqli_close( $this->_con );
return true;
}
} else {
throw new Exception( FIELDS_MISSING );
}
}
and this to get the values
<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Sólo letras" value="<?php
if ( $_POST ) {
echo $_POST[ 'first_name' ];
} else {
echo $_SESSION[ 'first_name' ];
}
?>" required>
...
thanks for any advice
Upvotes: 1
Views: 62
Reputation: 136
I am not too sure if I understand your question correctly. but for your code
`<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Sólo letras" value="<?php
if ( $_POST ) {
echo $_POST[ 'first_name' ];
} else {
echo $_SESSION[ 'first_name' ];
}
?>" required>`
It is better to to use if (isset($_POST['first_name']))
to make sure the first_name is not empty when making POST request.
The main problem of your code is that you probably forgot to set the first_name value in your session every time you clicked the button, and that's why you dont see the newest value if you refresh the page because $_SESSION['first_name'] is not updated at all!.
so from my understanding, please try the following code
if (isset($_POST['first_name'])){ //First Name changed, so we update it
$_SESSION['first_name'] = $_POST['first_name'];
}
echo $_SESSION['first_name']; //Print data (first_name in this instance)
You probably dont need else for this because the old value will always be in the session if you press the button before anyway
Upvotes: 1