Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:34:33+00:00 2026-06-15T08:34:33+00:00

Im tryin to make my update form and query to update by id. But

  • 0

Im tryin to make my update form and query to update by id. But when i put the data in the form he updates all the fields even tho it dont have any data so in the database he get gets “0”.
I need to update just the field that have data.

Can you guys help me?

Its giving me always that i have Undefined variables! And only updates the first and second fields!

Thanks, here´s my code:

    if (isset($_POST['alterar'])) {

    $id_cliente = $_POST["id_cliente"];
    $nome_cliente = $_POST["nome_cliente"];
    $telefone_cliente = $_POST["telefone_cliente"];
    $morada_cliente = $_POST["morada_cliente"];
    $email_cliente = $_POST["email_cliente"];
    $servico = $_POST["servico"];
    $n_pecas = $_POST["n_pecas"];
    $tp_arranjo = $_POST["tp_arranjo"];
    $descricao = $_POST["descricao"];

    } 

    $query = "UPDATE `clientes` SET ";
if ($nome_cliente) $columns[] = "`nome_cliente`        = '{$nome_cliente}'";
if ($telefone_cliente) $columns[] = "`telefone_cliente`= '{$telefone_cliente}'";
if ($morada_cliente) $columns[] = "`morada_cliente`  = '{$morada_cliente}'";
if ($email_cliente) $columns[] = "`email_cliente`   = '{$email_cliente}'";
if ($servico) $columns[] = "`servico`         = '{$servico}'";
if ($n_pecas) $columns[] = "`n_pecas`         = '{$n_pecas}'";
if ($tp_arranjo) $columns[] = "`tp_arranjo`      = '{$tp_arranjo}'";
if ($descricao) $columns[] = "`descricao`       = '{$descricao}'";

$columns = implode(",",$columns);
$query .= $columns . " WHERE id_cliente='$id_cliente'";


    mysql_query($query);

    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:34:35+00:00Added an answer on June 15, 2026 at 8:34 am

    You need to change your query to only list columns to be updated if you have values to update them with. This requires building the query string dynamically based on conditions.

    Example:

    $query = "update `clientes` set ";
    if ($nome_cliente) $columns[] = "`nome_cliente`= '{$nome_cliente}'";
    if ($telefone_cliente) $columns[] = "`telefone_cliente`= '{$telefone_cliente}'";
    //etc..
    
    $columns = implode(",",$columns);
    $query .= $columns . " where id_cliente='$id_cliente'";
    

    NOTE: As others have mentioned, this is not secure! You should always sanitize user input before passing it to your database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make an update form. The update part is already working, but
I'm trying to make the following form in JSF 2.0 but a have some
I'm trying to make update form, that is going to retrieve the data for
I'm trying to make an AJAX form that will update a twitter status when
Hi iam trying to make an update trigger in my database. But i get
I have an otherwise functioning rails project where I'm trying to update some form
I'm trying to make a dynamic PDF form using Adobe LiveCycle designer and have
Im trying to make a simple downloader, but i cant make it to update
I am trying to make a form using html and php to update mysql
I am trying to make an edit page where the form fields are populated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.