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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:20:46+00:00 2026-05-26T06:20:46+00:00

I have a function addIt, which adds data to the database whatever is the

  • 0

I have a function addIt, which adds data to the database whatever is the name or the number of fields of the table.

this function gets the name of the table, and the fields names, and builds the string of the command, then it scans the POST variables coming from the form.

then it needs to bindParams using a statement , before execution.

foreach($data as $k=>$v){

   $stmt->bindParam(':'.$result[$i],$v);
   $i++;
}

$data is the table that contains all the $_POST variables.
$result is the table that contains all the fields names

if title is the table field’s name , then ‘:’.result[$i] , will be ‘:title’, to be bound with $v (for e.g $v==$_POST[‘title’])

the foreach is used to repeat the same process for all variables.

but I have the problem , it’s that the fields in the tables get the values of the last POST variable .

so if the form sends those variables : $_POST[‘title’] , $_POST[‘TEXTE’] , and title=”hello” , texte=”world”

the fields in the table will take those values : title=”world”, texte=”world”

if you want to see a full version of the class code :

<?php
class add{

/*******************FUNCTION GET FIELDS**********************/
private function getFields($tbl){
try{
require 'global/connection.inc.php';
$cmd='DESCRIBE '.$tbl;
$fields=array();
$i=0;
foreach($pdo->query($cmd) as $r){
if ($r[0]!='views' && $r[0]!='votes'){
$fields[$i]=$r[0];
}
$i++;
}
return $fields;
}
catch(PDOException $e){}
}

/***********************FUNCTION GETDATA************************/

private function getData(){

$data=array();
$i=0;
foreach($_POST as $k){
$data[$i]=$k;
$i++;
}


return $data;
}


/**************************FUNCTION ADDIT************************/
function addIt($tbl){
require 'global/connection.inc.php';
try{

/*********create object add and get Fields names and POST data*****************/
$object=new add();
$result=$object->getFields($tbl);
$data=$object->getData();

/***************Build the sql command*****************************************/
$fields='';
$cmd='INSERT INTO '.$tbl;

$i=0;
foreach($result as $k=>$v){
     if ($i<count($result)-1 && $i!=0){
        $fields.=$v.',';
     }
     if ($i==count($result)-1){
       $fields.=$v;
     }
     $i++;
}

$values='';
$i=0;
foreach($result as $k=>$v){
      if ($i<count($result)-1 && $i!=0){
        $values.=':'.$v.',';
     }
     if ($i==count($result)-1){
       $values.=':'.$v;
     }
     $i++;
}

$cmd=$cmd.'('.$fields.')VALUES('.$values.')';

echo $cmd.'<br/>';

/**************************bind params and execute command******************************/
$stmt=$pdo->prepare($cmd);

$i=1;
foreach($data as $k=>$v){
    $n=$v.'';
   $stmt->bindParam(':'.$result[$i],$n);   //The problem is here
   $i++;
}

$stmt->execute();


}
catch(PDOException $e){echo $e->getMessage();}
}



}

Thank you in advance.

  • 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-26T06:20:47+00:00Added an answer on May 26, 2026 at 6:20 am

    I am not sure but i guess problem is due to use of starting index $i in getFields as 0;
    but in your problematic loop $i is starting from 1. Also maybe in getFields method $i increment has to be in if block.

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

Sidebar

Related Questions

Currently we have a table with a bunch of data from a database which
I have a dll which includes a function called ReadPort that reads data from
If I have function names stored as strings in a Hashtable. Is there a
In my Java code I have function that gets file from the client in
I have a function that gets x(a value) and xs(a list) and removes all
I have this function twitter_tweets_per_day($user, $rounding = 1) { // Helper function to calculate
I have an Addin for MS Excel which needs a singleton to share data
I have some NUnit tests which uses a TestCaseSource function. Unfortunately, the TestCaseSource function
I have an update panel in my page which runs a function and sends
I have a triggered javascript function to hide dds which have the same class

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.