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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:09:40+00:00 2026-05-25T22:09:40+00:00

I hate to type the INSERT query, you always miss out with some stuff,

  • 0

I hate to type the INSERT query, you always miss out with some stuff, and get syntax error. Therefor, I want to create my own function, to let me do this. This is what I got so far:

$data['test'] = array('username' => 'john', 
              'password' => 'hello',
              'userlevel' => '__d');

$table = 'users';

$numItems = count($data['test']);
$i = 0;

$sql = "INSERT INTO " . $table . "(". implode(", ", array_keys($data['test'])) .")";


$sql .= " VALUES (";

foreach ($data['test'] as $value) {

    if ($i+1 == $numItems and $value == '__d') {
        $sql .= "" . 'NOW()' . ")";
    } else if ($i+1 == $numItems) {
        $sql .= "'" . $value . "')";
    } else if ($value == '__d') {
        $sql .= "" . 'NOW()' . ", ";
    } else {
        $sql .= "'" . $value . "', ";
    }

            $i++;


}

echo $sql;

Umm, yeah. Any tips on how I can improve this code?

  • 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-25T22:09:41+00:00Added an answer on May 25, 2026 at 10:09 pm
    <?php
    //test data
    $columns = array(
        'username'=>'john',
        'password'=>'hello',
        'userlevel'=>1,
        'date'=>'__d'
    );
    $table = 'users';
    
    // replace keys and values with SQL delimeters
    foreach($columns as $k=>$v) {
        unset($columns[$k]);
    
        if ($v != '__d' && !is_int($v))
            $v = "'$v'";
    
        if ($v == '__d')
            $v = 'NOW()';
    
        $columns["`$k`"] = $v;
    }
    
    // create the query
    $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
                $table,
                implode(",", array_keys($columns)),
                implode(",", $columns)
           );
    
    echo $sql;
    ?>
    

    Output:

    INSERT INTO users (`username`,`password`,`userlevel`,`date`) VALUES ('john','hello',1,NOW())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

include/TestBullet.h:12: error: expected constructor, destructor, or type conver sion before '(' token I hate
I hate it when I try to apply some logic to positioning elements on
I'm using the Drupal Quiz module, and have setup some basic quizzes and stuff
I'm attempting to create a type-agnostic vector in C++ that is distinguished by two
query = Files .Where(file => file.Fileinfo.Name.ToUpper().Contains(textBox1.Text.ToUpper())) .Take(7).ToList(); I hate asking this question, but I
For some reason, my boss likes to create custom types to represent a generic
I hate to ask for help on such a common error but I've been
This one's turning out to be a brain teaser for me. I almost hate
I hate to ask these type of questions but I am in a bind
I hate to submit a new question, but everyone else has some slight thing

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.