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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:37:25+00:00 2026-06-15T04:37:25+00:00

I have an array like this $a = array( ‘phone’ => 111111111, ‘image’ =>

  • 0

I have an array like this

  $a = array( 'phone' => 111111111, 'image' => "sadasdasd43eadasdad" );

When I do a var-dump I get this ->

 { ["phone"]=> int(111111111) ["image"]=> string(19) "sadasdasd43eadasdad" }

Now I am trying to add this to the DB using the IN statement –

 $q = $DBH->prepare("INSERT INTO user :column_string VALUES :value_string");
 $q->bindParam(':column_string',implode(',',array_keys($a)));
 $q->bindParam(':value_string',implode(',',array_values($a)));
 $q->execute();

The problem I am having is that implode return a string. But the ‘phone’ column is an integer in the database and also the array is storing it as an integer. Hence I am getting the SQL error as my final query look like this —

INSERT INTO user 'phone,image' values '111111111,sadasdasd43eadasdad';

Which is a wrong query. Is there any way around it.

My column names are dynamic based what the user wants to insert. So I cannot use the placeholders like :phone and :image as I may not always get a values for those two columns. Please let me know if there is a way around this. otherwise I will have to define multiple functions each type of update.

Thanks.

  • 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-15T04:37:26+00:00Added an answer on June 15, 2026 at 4:37 am

    Last time I checked, it was not possible to prepare a statement where the affected columns were unknown at preparation time – but that thing seems to work – maybe your database system is more forgiving than those I am using (mainly postgres)

    What is clearly wrong is the implode() statement, as each variable should be handled by it self, you also need parenthesis around the field list in the insert statement.

    To insert user defined fields, I think you have to do something like this (at least that how I do it);

    $fields=array_keys($a); // here you have to trust your field names! 
    $values=array_values($a);
    $fieldlist=implode(',',$fields); 
    $qs=str_repeat("?,",count($fields)-1);
    $sql="insert into user($fieldlist) values(${qs}?)";
    $q=$DBH->prepare($sql);
    $q->execute($values);
    

    If you cannot trust the field names in $a, you have to do something like

    foreach($a as $f=>$v){
       if(validfield($f)){
          $fields[]=$f;
          $values[]=$v;
       }
    }
    

    Where validfields is a function that you write that tests each fieldname and checks if it is valid (quick and dirty by making an associative array $valfields=array(‘name’=>1,’email’=>1, ‘phone’=>1 … and then checking for the value of $valfields[$f], or (as I would prefer) by fetching the field names from the server)

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

Sidebar

Related Questions

I have an array like this. int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003, R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006}; Right now I
I have an array like this var array = [{ order: 3, sub -
I have an array like this: var arr1 = [a, b, c, d]; How
I have an array like this: typedef struct INSTR { char* str; int argc;
I have array defined like this : ArrayList<String> items = new ArrayList<String>(); I have
I have an array like this. What i want is to get the value
I have an array like this: var anArray = [ { name: scala, type:
I have array like this var Arr = [ 'h78em', 'w145px', 'w13px' ] I
I have an array like this: var movies = [ { Name: The Red
I have an Array like this: var colors = { 1: '#FFFF00', 2: '#FF0000',

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.