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

  • Home
  • SEARCH
  • 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 7999833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:36:57+00:00 2026-06-04T15:36:57+00:00

Is there any way to make the below-given code reusable for different tables, e.g.

  • 0

Is there any way to make the below-given code reusable for different tables, e.g. using foreach? So, how to send and use arrays of column names and values? Any example is highly appreciated.

<?php
    include_once 'include/DatabaseConnector.php';
    if(isset($_POST['flightNum'])) {
        $flightNum=$_POST['flightNum'];
        $from=$_POST['from'];
        $STADate=$_POST['STADate'];
        $query = 'UPDATE flightschedule 
                  SET frm="'.$from.'",STADate="'.$STADate.'"
                  WHERE flightNum="'.$flightNum.'"';
        DatabaseConnector::ExecuteQuery($query);
        echo '1';
    } else { 
        echo '0'; 
    }
?>

UPDATE: What if I don’t know column names apriori? How to create flexible UPDATE statement?

  • 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-04T15:36:59+00:00Added an answer on June 4, 2026 at 3:36 pm

    you can convert your code into a reusable function. for example.

    function updateDB($tableName, $flightNum, $from, $STADate) {
        include_once 'include/DatabaseConnector.php';
        $query = 'UPDATE ' . $tableName 
                  SET frm="'.$from.'",STADate="'.$STADate.'"
                  WHERE flightNum="'.$flightNum.'"';
        $execute = DatabaseConnector::ExecuteQuery($query);
        return $execute;
    }
    

    and to use it

    if(isset($_POST['flightNum']) {
        $update = updateDB('flightschedule', $_POST['flightNum'], $_POST['from'], $_POST['STADate']);
        echo $update;
    }
    

    Update:

    I want to send an array of column names to the function ‘updateDB’.
    Let’s say these are column names for SET and WHERE parts of UPDATE
    statement. And then I could use FOREACH, but I need some example for
    this.

    this is how you can do it.

    function updateDB($tableName, $columns, $where) {
        //do some validation here to check if proper data is being passed like
        if(!isarray($columns)) {
            throw new Exception('argument two $columns should be an associative array');
        }
        include_once 'include/DatabaseConnector.php';
        $query  = 'UPDATE ' . $tableName;
        foreach($columns as $column => $data) {
            $query .= ' SET ' . $column . ' = ' . $data . ', ';
        }
        //remove last comma from the update query string
        $query = substr($query, 0, -1);
        $query .= ' WHERE ' . $where['column'] . ' = ' . $where['value'];
        $execute = DatabaseConnector::ExecuteQuery($query);
        return $execute;
    }
    

    and to use it.

    if(isset($_POST['flightNum']) {
        $columns = array(
            'frm' => $_POST['frm'],
            'STADate' => $_POST['STADate']
        );
        $where = array(
            'column'=> 'flightNum', 
             'value' =>  $_POST['flightNum']
        );
        $update = updateDB('flightschedule', $columns, $where);
        echo $update;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to make two controls that are in different containers the
Is there any way to make a DataTemplate reference itself just from XAML? In
Is there any way to make the form semi-transparent while it is being moved
Is there any way to make the program sleep until an event occurs, but
Is there any way to make this work in Java? public static void change(List<?
Is there any way to make the background of the google charts transparent. Google
Is there any way to make a list of classes behave like a set
Is there any way to make only a non-transparent portion of an image to
Is there any way to make extension to pop up when event occurs? Let's
Is there any way to make the option group selectable? <select> <optgroup value=0 label=Parent

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.