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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:38:35+00:00 2026-06-08T19:38:35+00:00

Trying to build a function that can insert into a SQL database regardless of

  • 0

Trying to build a function that can insert into a SQL database regardless of how many or how few variables there are. Currently my function is not working.

function insert($table, $columns, $results){
    global $dbh;
    $results = explode(',',$results); 
    $val= ''; 
    $ex = '';
    foreach($results as $result){ 
        $val .= ":" . $result . ","; 
        $ex = "':" . $result . "' => $result,"; 
    }
    $val = rtrim($val, ','); 
    $ex = rtrim($ex, ',');
    $sql = "INSERT INTO $table ($columns) VALUES ($val)";
    $q = $dbh->prepare($sql);
    $q->execute(array($ex));
}

Called like this: insert('users','email,pwd,forename,surname,level,status',$vals);

Where as I managed to get it working for my Select function.

function check($table,$columns,$results){
    global $dbh;
    $columns = explode(',',$columns);
    $results = explode(',',$results); 
    $whr= ''; 
    $int = 0;
    foreach($columns as $column){ 
        $whr.= " AND " . $column . " = '{$results[$int]}'"; 
        $int += 1; 
    }
    $sql = "SELECT * FROM $table WHERE status != 'D' $whr";
    $return = 0;
    foreach($dbh->query($sql) as $check){ 
        $return = 1; 
    } 
    return $return;
}
  • 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-08T19:38:38+00:00Added an answer on June 8, 2026 at 7:38 pm

    First, I would probably pass your columns and values into the function as an associative array. This helps force the fact that the function caller must pass an equal number of both. At the very least you should have logic that compares the number of the passed in columns and value elements to make sure they are equal. You might try something like this (note I am also passing the DB connection in as a parameter, which is better coding practice).

    function insert($db, $table, $key_value_array) {
        $sql = 'INSERT INTO ' . $table . ' '; 
        $columns = '(';
        $values = '(';
        foreach ($key_value_array as $k => $v) {
            $columns .= '`' . $k . '`, ';
            $values .= "'" . $v . "', ";
        }
        $columns = rtrim($columns, ', ') . ')';
        $values = rtrim($values, ', ') . ')';
        $sql .= $columns . ' VALUES ' . $values;
        $q = $db->prepare($sql);
        $q->execute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build an edit page for a database record that can be
I am trying to build a function that extracts information from a database and
I'm trying to build a function that will remove all the files that start
I'm trying to build a PHP function that allows me to have an array
I am currently trying to build a program where I have a recursive function
I'm trying to build generic function that get from user string and try to
I'm trying to build a progressbar class that can have an arbitrary number of
I'm trying to build a AS3 socket server that can handshake with html5 websockets.
I am trying to build a simple abstract Cache class that can enforce methods
I'm currently trying to build a filter search function using JQuery and Ajax. The

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.