473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Need to use INSERT command using complete variables

Hi

I would apreciate it immensly if somebody can help me with the following problem. I need to create an insert using only variables. To explain what I am trying to do, I need to turn this:
Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO table (column1, column2) VALUES (value1, value2)";
  2.  
into this:

Expand|Select|Wrap|Line Numbers
  1. $table = table;
  2. $column = $column1.", ".$column2;
  3. $value = $value1.", ".$value2;
  4. $sql = "INSERT INTO `$table` (`$column`) VALUES (`$value`)";
  5.  
The reason for doing so is that it is impossible to know the number of actual columns to be inserted as the table itself is created dynamically.
I will add my code at the end so it is understanble.

I am really stuck and nned help on this one. I do apologise for my structure but I am not a programmer..

Thanks for any help..

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. # Set Variables and connections
  5.  
  6.             set_include_path('include/');
  7.             include 'db_connect.php';
  8.             $year = date("Y");
  9.             $year = $year + 1;
  10.             $nombre_u = $_SESSION['nombre_u'];
  11.             $apellidop_u = $_SESSION['apellidop_u'];
  12.             $depa = $_SESSION['depa2'];
  13.             $departamento2 = $_SESSION['depa'];
  14.             $_SESSION['departamento2'] = $departamento2;
  15.  
  16.             #$gest = $_SESSION['gest'];
  17.             $progr = $_SESSION['progr'];
  18.  
  19.  
  20. # Variable related to the category selected
  21.  
  22. $gestion = 3;
  23.  
  24. # Create table using variables to define number of columns <---
  25.  
  26. header('Content-Type: text/html; charset=UTF-8');  
  27.  
  28.         $data = "SELECT * FROM progdesc where gestion = '$gestion'" or die("¡Error No existe ese departamento!"); 
  29.         $result = mysql_query($data) or die("¡Error No existe ese dep!");
  30.  
  31.         while(($row = mysql_fetch_array($result)) !== FALSE)
  32.             {
  33.             $columna=$row['pn'];
  34.             $valor= $valor." pt".$columna." VARCHAR(50),";
  35.             $valor2= $valor2.", pt".$columna;
  36.             $gestion = '3';
  37.             $table = "sumag".$gestion.$year;
  38.              }
  39.  
  40.         $valor= "ano VARCHAR(4), id VARCHAR(4), idas VARCHAR(13), ".$valor." "."total VARCHAR(10)";
  41.  
  42.         mysql_query("CREATE TABLE `".$table."`(".$valor.")") or die(mysql_error());
  43.  
  44.     #End of table creation --->
  45.  
  46. # INSERT DATA INTO NEWLY CREATED TABLE
  47.  
  48. # Step 1 - Loop through the diferent fixed rows
  49.  
  50.     $data1 = "SELECT * FROM asigna" or die("¡Error No existe ese departamento!"); 
  51.     $result1 = mysql_query($data1) or die("¡Error1!");
  52.  
  53.     while(($row = mysql_fetch_array($result1)) !== FALSE)
  54.             {
  55.                 $id=$row['id'];
  56.                 $idas=$row['idas'];
  57.                 $sid=$row['sid'];
  58.                 $tit2 = NULL;
  59.                 $monto3 = 0;
  60.  
  61. # Step 2 - Select all the records that are related to the category selected
  62.  
  63.                 $data2 = "SELECT * FROM progdesc WHERE gestion = '$gestion' and ano = '$year' " or die("¡Error No existe ese departamento!"); 
  64.                 $result2 = mysql_query($data2) or die("¡Error2!");
  65.  
  66.                 while(($row2 = mysql_fetch_array($result2)) !== FALSE)
  67.                         {
  68.                             $dep=$row2['departamento'];
  69.                             $titulo=$row2['titulo'];
  70.                             $depf = $dep."final";
  71.                             $pn = $row2['pn'];
  72.                             $sidb = $row2['sid'];
  73.  
  74.  # Step 3 - Get all the records related to the programs related to the category and thier values.
  75.  
  76.                             $data3 = "SELECT * FROM $depf WHERE ano = '$year' and idas = '$idas' and gestion = '$gestion'and programa = '$pn'  " or die("¡Error No existe ese departamento!"); 
  77.                             $result3 = mysql_query($data3) or die("¡Error3!");
  78.  
  79.                             while(($row3 = mysql_fetch_array($result3)) !== FALSE)
  80.                                     {
  81.                                         $monto=$row3['monto'];
  82.  
  83.                                         if (!($idas2 == $idas and $monto == 0)){
  84.                                             $tit2 =  $tit2.", ".$monto;
  85.  
  86.                                         }elseif ($monto == 0){
  87.                                             $tit2 =  $tit2.", 0";
  88.  
  89.                                         }else{
  90.                                             $cero = 0;
  91.                                             $pn2 = 0;
  92.  
  93.                                         if (!($monto == 0)){
  94.  
  95.                                             for ($i = 0; $i < $pn2; $i++) {
  96.                                                 $cero = $cero.", 0";
  97.                                                 }
  98.  
  99.                                             $tit2 =  $tit2.", ".$cero.", ".$monto;
  100.  
  101.                                         }
  102.                                        }
  103.  
  104.                                     $idas2 = $idas;
  105.                                     $ln = $ln + 1;
  106.                                     $monto2 = $monto;
  107.  # Sum the values to get a total                                   
  108.                                     $monto3 = $monto3 + $monto;
  109.                         }
  110.                 }
  111.  
  112.  # Insert the values into the table using the information provided as a sting of values           
  113.  
  114.             $insert = "ano, id, idas".$valor2.", total";
  115.             $value = $year.", ".$id.", ".$idas.$tit2.", ".$monto3;
  116.             $sql = "INSERT INTO `$table` (`$insert`) VALUES (`$value`)";
  117.  
  118.             mysql_query($sql);
  119.        } 
  120.  
  121. ?>
  122.  
  123.  
Mar 27 '17 #1
0 1045

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. ...
3
by: Ed | last post by:
Hi, I want to load data to a table in Sql Server from a dataset table in my vb.net app using a dataAdapter. I know how to do this as follows (my question is to see if I can reduce the amount...
4
by: rudy | last post by:
I'm trying to insert text values into a table in a MSAccess DB the values are stored in string variables Imports System.Data.OleDb .... Dim name As String Dim position As String Dim site As...
0
by: Rob Dob | last post by:
How do I create a insert command that will take a datatable or datarow as a parameter instead of me having to supply it with parameters for each field value. I heard something about using the v2.0...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
3
by: iKiLL | last post by:
Hi all I am having problems getting my SqlCeDataAdapter to Update the SQL Mobile Data base. i am using C# CF2. I have tried this a number of different ways. Starting with the command builder...
3
by: Randy | last post by:
I think that this is a really simple question. I have a table adapter set up with the various sql queries. I'd like to allow the user to make changes to a given row on a form. Each of the...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
1
by: Freedolen | last post by:
Hi, I have checked with some sample data and found inserting of data using 'insert command' takes more time than using 'load data' command to load data from another file. What is the process...
1
by: Lawrence Pereira | last post by:
Can any one help me in inserting any date into the table using insert command because when i m trying to insert date using insert command the query executes but the date is totally wrong if i am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.