0

i am trying to update name ,email , image informations in form.

name, email was updating fine, but image was not saving in folder, so i removed ; in below line :

if ($user_home->update($uname,$email, $phone, $uid)); ,

now once we click on "save" button, images are saving in folders,

but name & emails are displaying old values, & after refreshing page displaying updated values. but i want to display updated values once we click on save button.

form

<form action="profile.php" method="POST" enctype="multipart/form-data">
Name : 
<?php echo $row['userName'] ?> <br/>
Email :
<?php echo $row['userEmail'] ?> <br>

<h3>photo</h3>

<input type="file" name="photo" id="fileSelect"><br> 
<input type="submit" name="submit" value="Save" />
</form> 

code for name ,email

<?php
include 'home.php';

// session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    header("Location: index.php");
die();
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>



    <?php

    $FORM['uname'] = "";
    $FORM['txtuname'] = "";
    if (isset($_POST['submit'])) {
    // new data
    $uname = $_POST['txtuname'];
    $email = $_POST['txtemail'];
    $phone = $_POST['phone'];
    $uid = (isset($_GET['userID']) ? intval($_GET['userID']) : -1);

    // query
    if ($user_home->update($uname,$email, $phone, $uid)); // This is the line
    {
        header("Location: profile.php");
        die(); 
    }
    }

    ?>

code for image

<?php 
if(isset($_FILES["photo"]["error"])){ 
if($_FILES["photo"]["error"] > 0){ 
echo "Error: " . $_FILES["photo"]["error"] . "<br>"; 

} else{ 
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png"); 
$filename = $_FILES["photo"]["name"]; 
$filetype = $_FILES["photo"]["type"]; 
$filesize = $_FILES["photo"]["size"]; 

// Verify file extension 
$ext = pathinfo($filename, PATHINFO_EXTENSION); 
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format."); 

// Verify file size - 5MB maximum 
$maxsize = 5 * 1024 * 1024; 
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit."); 

// Verify MYME type of the file 
if(in_array($filetype, $allowed)){ 
// Check whether file exists before uploading it 
if(file_exists("upload/" . $_FILES["photo"]["name"])){ 
echo $_FILES["photo"]["name"] . " is already exists."; 

} else{ 
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]); 

echo "Your file was uploaded successfully."; 
} 
} else{ 

echo "Error: There was a problem uploading your file - please try again."; 
} 

} 

} else{ 
echo "Error: Invalid parameters - please contact your server administrator."; 
} 

?>

1 Answers1

0

You need to do the select query after the update query, otherwise you are getting the old info and then update the record in the database.

<?php
include 'home.php';

// session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    header("Location: index.php");
    die();
}

$FORM['uname'] = "";
$FORM['txtuname'] = "";
if (isset($_POST['submit'])) {
// new data
$uname = $_POST['txtuname'];
$email = $_POST['txtemail'];
$phone = $_POST['phone'];
$uid = (isset($_SESSION['userSession']) ? intval($_SESSION['userSession']) : 0);

// query
if ($uid > 0 && $user_home->update($uname,$email, $phone, $uid)) // This is the line
{
    header("Location: profile.php");
    die(); 
}
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

Or you can use this custom update:

// query
if ($uid > 0)
{
    $stmt = $user_home->runQuery("UPDATE tbl_users SET userName=:uname, 
                              userEmail=:email, phone=:phone WHERE userID=:uid");
    $stmt->execute(array(
                 ":uid"=>$_SESSION['userSession'],
                 ":email" => $email,
                 ":phone" => $phone,
                 ":uname" => $uname
    ));
    header("Location: profile.php");
    die(); 
}
krasipenkov
  • 2,031
  • 1
  • 11
  • 13
  • you saved my day buddy, now its working fine, one more issue i am getting is once i upload image and click on `save` button, images are saving in folder, thats fine. but its displaying error : `SQLSTATE[HY000]: General error` , i guess some problem with code part of image, can you please help me for that too ? –  Oct 24 '16 at 10:02
  • What is the query that fails – krasipenkov Oct 24 '16 at 10:20
  • no , its working fine, its uploading images to folder fine, but it display error message : `SQLSTATE[HY000]: General error` once we click on save button, please check `code for image` part in question.... i checked [this link](http://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database) , but i did't got what to do in my case..... –  Oct 24 '16 at 10:25
  • Try with my updated code. `$_GET['userID']` is unnecessary given you have `$_SESSION['userSession']` – krasipenkov Oct 24 '16 at 10:35
  • i replaced this line : `$uid = (isset($_GET['userID']) ? intval($_GET['userID']) : -1);` with this line `$uid = (isset($_SESSION['userSession']) ? intval($_SESSION['userSession']) : 0);` , but it did't worked for me.... –  Oct 24 '16 at 10:44
  • And did you add `$uid > 0 && ` in the if – krasipenkov Oct 24 '16 at 10:45
  • add `echo 111;` before the if and `echo 222;` after the if and see where warning message appears - before or after which echo – krasipenkov Oct 24 '16 at 10:50
  • code : `echo 111; if ($uid > 0 && $user_home->update($uname,$email, $phone, $uid)); // This is the line { header("Location: profile.php"); die(); } } echo 222; , error : `222 SQLSTATE[HY000]: General error 333` –  Oct 24 '16 at 10:57
  • sorry, check this one : code : echo 111; if ($uid > 0 && $user_home->update($uname,$email, $phone, $uid)); // This is the line { header("Location: profile.php"); die(); } } echo 222; , error : `111 SQLSTATE[HY000]: General error 222` –  Oct 24 '16 at 10:59
  • it seems the error comes from `$user_home->update`. Check if you have unique columns set in the table or just execute the update query manually with the data that you are submitting in the form – krasipenkov Oct 24 '16 at 11:01
  • please check here : http://prnt.sc/cy8uk9 , also please tell me `how to execute the update query manually with the data that i am submitting in the form` –  Oct 24 '16 at 11:06
  • I edited my answer + I noticed another error in the code before `// This is the line` :) there is `;` which means that the body of the `if statement` was executed each time no matter if it is true or not :) – krasipenkov Oct 24 '16 at 11:13
  • i updated your code, images are not uploading , can you please check... –  Oct 24 '16 at 12:26
  • may be it's because you are getting redirected by `header("Location: profile.php");` – krasipenkov Oct 24 '16 at 12:42
  • instead of `custom update` if i remove `;` from `if ($uid > 0 && $user_home->update($uname,$email, $phone, $uid))` , than images will upload but this error is still there : `SQLSTATE[HY000]: General error` –  Oct 24 '16 at 12:48
  • now i removed `redirection` & using same file in header as like `header("Location: profile1.php"); but still the error exists.... –  Oct 24 '16 at 12:52
  • Debug the code in $user_home->update. If you can try to print the query that is executed in this function. – krasipenkov Oct 24 '16 at 14:26
  • Ok, you got it :) – krasipenkov Oct 25 '16 at 08:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126594/discussion-between-abcd-and-krasipenkov). –  Oct 25 '16 at 09:26