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 6220135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:54:18+00:00 2026-05-24T07:54:18+00:00

include(connect.php); $username = mysql_real_escape_string($_REQUEST[username]); function updateData($key){ $field = $$key; if($field == ) { }

  • 0
    include("connect.php");
    $username = mysql_real_escape_string($_REQUEST[username]);


    function updateData($key){
    $field = "$$key";
    if($field == "") {
    } else {
    $sql= "UPDATE users SET $key = '$field' WHERE username ='$username'";
    mysql_query ( $sql , $conexion );
    }
    }

    updateData('password');
    updateData('firstname');

What I’m trying to make is an automatic way for this PHP file to recognize the fields and not save them if the input field value is empty.

If I mannually put the name of the field on the $key spaces, it works, but once I turn it into a function an run it, it doesn’t work.

  • 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-05-24T07:54:19+00:00Added an answer on May 24, 2026 at 7:54 am

    Don’t write your code like this.

    • For starters, it’s not readable – it makes whoever is maintaining the code have to go find this function to figure out where the data being used is coming from.

    • Secondly, you’ll run into issues with variable scope – inside the function, none of your variables are defined unless you reference them with the global keyword to bring them into scope.

    Instead, write your function like this:

    function updateData($username, $key, $value){
        if($key != "" && $value != "") {
            $sql = "UPDATE users SET $key = '$value' WHERE username ='$username'";
            mysql_query($sql, $conexion);
        }
    }
    

    and pass in the relevant values; that’s what function arguments are designed for. You might also want to consider escaping the contents of the arguments before interpolating them.

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

Sidebar

Related Questions

<?php include(../package/mysql-connect.php); //connect- succeeds //cleanin' data $username = trim(mysql_real_escape_string($_POST['username'])); $password = hash('sha512', $passround); $fname
<?php session_start(); include(connect.php); $timeout = 60 * 30; $fingerprint = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']); if(isset($_POST['userName']))
I have this code : include(connect.php); // Call database function $p = 100; $sth
when I connect to my database I include('connect.php') the document to connect. Now I
Here is my complete code.... Connect.php $connect = @mysql_connect ($host, $username, $password, $db_name) or
Why wont this work? <?php include top.php; include connect.php; if(isset($_POST[submit])) { $error = ;
include(conn.php); $result = mysql_query(SELECT * FROM sggame); while($row = mysql_fetch_assoc($result)); { $id = $row['id'];
I am trying to connect to a mysql db through php running on my
I have a problem with include pages in PHP. Picture shows what I want
This is my login script; <?php $username = $_POST['username']; //kullanici adi $password = $_POST['password'];

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.