Dando Mando Nogger
Dando Mando Nogger

Reputation: 157

Trying to only update once received field from a form and the others turn blank in PHP

I'm trying to create an update PHP function, it is working but I am having some problems, I receive the username email and password from a form but If the user leaves one or 2 of these blank I want to update the only one that isn't left blank for example 'pass' but when I do this the other 2 fiels update to nothing or blank in the database, what are my errors in my code?

<?php 

    session_start();

    $id = $_SESSION['user_id'];
    $email =  $_POST['emailAlter'];
    $nome = $_POST['nameAlter'];
    $pass = $_POST['passAlter'];


    $con = mysqli_connect("localhost", "root", "", "smarttime");
    $query = mysqli_query($con,"UPDATE users SET use_name = '$nome', use_email = '$email', use_pass = '$pass' WHERE use_id = '$id'");

    $test = mysqli_query($con,"SELECT * from users");

    $row = mysqli_fetch_array($test);   

    if (!$con) {
        die('Erro de Acesso à BD' . mysqli_connect_error());
    }

    if(!isset($email) || trim($email) == '')
    {
        $email = mysqli_query($con,"SELECT use_email from users where use_id ='$id");
        $query = mysqli_query($con,"UPDATE users SET use_name = '$nome', use_email = '$email', use_pass = '$pass' WHERE use_id = '$id'");
    }

    if(!isset($nome) || trim($nome) == '')
    {
        $nome = mysqli_query($con,"SELECT use_name from users where use_id ='$id");
        $query = mysqli_query($con,"UPDATE users SET use_name = '$nome', use_email = '$email', use_pass = '$pass' WHERE use_id = '$id'");
    }

    if(!isset($pass) || trim($pass) == '')
    {
        $nome = mysqli_query($con,"SELECT use_pass from users where use_id ='$id");  
        $query = mysqli_query($con,"UPDATE users SET use_name = '$nome', use_email = '$email', use_pass = '$pass' WHERE use_id = '$id'");
    }

        $query;
        header('Location: logged.php');
        exit();



 ?>

Upvotes: 0

Views: 29

Answers (2)

Umar Khan
Umar Khan

Reputation: 132

The thing is you are updating first and then checking conditions.You have to check all the variables before updating

<?php 

    session_start();

    $id = $_SESSION['user_id'];
    $email =  $_POST['emailAlter'];
    $nome = $_POST['nameAlter'];
    $pass = $_POST['passAlter'];


    $con = mysqli_connect("localhost", "root", "", "smarttime");
    $query = "UPDATE users SET "; 

    if (!$con) {
        die('Erro de Acesso à BD' . mysqli_connect_error());
    }

    if(isset($email) && trim($email) != '')
    {
        $query .= "use_email = '$email' ";
    }

    if(isset($nome) && trim($nome) != '')
    {
        $query .= "use_name = '$nome' ";
    }

    if(isset($pass) && trim($pass) != '')
    {
        $query .= "use_pass = '$pass' ";
    }

        mysqli_query($con,$query." WHERE use_id = '$id'");;
        header('Location: logged.php');
        exit();



 ?>

Upvotes: 0

KIKO Software
KIKO Software

Reputation: 16688

If I assume that the user id is always there, and valid then you can build a query like this:

$setters = array();
if ($trim($email) != '') $setters[] = "use_email = '$email'";
if ($trim($nome)  != '') $setters[] = "use_name = '$nome'";
if ($trim($pass)  != '') $setters[] = "use_pass = '$pass'";
$query  = "UPDATE users SET ".implode(", ",$setters)." WHERE use_id = '$id'";
$result = mysqli_query($con,$query);

So the query depends on the presence of input.

Upvotes: 2

Related Questions