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
- $sql = "INSERT INTO table (column1, column2) VALUES (value1, value2)";
Expand|Select|Wrap|Line Numbers
- $table = table;
- $column = $column1.", ".$column2;
- $value = $value1.", ".$value2;
- $sql = "INSERT INTO `$table` (`$column`) VALUES (`$value`)";
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
- <?php
- # Set Variables and connections
- set_include_path('include/');
- include 'db_connect.php';
- $year = date("Y");
- $year = $year + 1;
- $nombre_u = $_SESSION['nombre_u'];
- $apellidop_u = $_SESSION['apellidop_u'];
- $depa = $_SESSION['depa2'];
- $departamento2 = $_SESSION['depa'];
- $_SESSION['departamento2'] = $departamento2;
- #$gest = $_SESSION['gest'];
- $progr = $_SESSION['progr'];
- # Variable related to the category selected
- $gestion = 3;
- # Create table using variables to define number of columns <---
- header('Content-Type: text/html; charset=UTF-8');
- $data = "SELECT * FROM progdesc where gestion = '$gestion'" or die("¡Error No existe ese departamento!");
- $result = mysql_query($data) or die("¡Error No existe ese dep!");
- while(($row = mysql_fetch_array($result)) !== FALSE)
- {
- $columna=$row['pn'];
- $valor= $valor." pt".$columna." VARCHAR(50),";
- $valor2= $valor2.", pt".$columna;
- $gestion = '3';
- $table = "sumag".$gestion.$year;
- }
- $valor= "ano VARCHAR(4), id VARCHAR(4), idas VARCHAR(13), ".$valor." "."total VARCHAR(10)";
- mysql_query("CREATE TABLE `".$table."`(".$valor.")") or die(mysql_error());
- #End of table creation --->
- # INSERT DATA INTO NEWLY CREATED TABLE
- # Step 1 - Loop through the diferent fixed rows
- $data1 = "SELECT * FROM asigna" or die("¡Error No existe ese departamento!");
- $result1 = mysql_query($data1) or die("¡Error1!");
- while(($row = mysql_fetch_array($result1)) !== FALSE)
- {
- $id=$row['id'];
- $idas=$row['idas'];
- $sid=$row['sid'];
- $tit2 = NULL;
- $monto3 = 0;
- # Step 2 - Select all the records that are related to the category selected
- $data2 = "SELECT * FROM progdesc WHERE gestion = '$gestion' and ano = '$year' " or die("¡Error No existe ese departamento!");
- $result2 = mysql_query($data2) or die("¡Error2!");
- while(($row2 = mysql_fetch_array($result2)) !== FALSE)
- {
- $dep=$row2['departamento'];
- $titulo=$row2['titulo'];
- $depf = $dep."final";
- $pn = $row2['pn'];
- $sidb = $row2['sid'];
- # Step 3 - Get all the records related to the programs related to the category and thier values.
- $data3 = "SELECT * FROM $depf WHERE ano = '$year' and idas = '$idas' and gestion = '$gestion'and programa = '$pn' " or die("¡Error No existe ese departamento!");
- $result3 = mysql_query($data3) or die("¡Error3!");
- while(($row3 = mysql_fetch_array($result3)) !== FALSE)
- {
- $monto=$row3['monto'];
- if (!($idas2 == $idas and $monto == 0)){
- $tit2 = $tit2.", ".$monto;
- }elseif ($monto == 0){
- $tit2 = $tit2.", 0";
- }else{
- $cero = 0;
- $pn2 = 0;
- if (!($monto == 0)){
- for ($i = 0; $i < $pn2; $i++) {
- $cero = $cero.", 0";
- }
- $tit2 = $tit2.", ".$cero.", ".$monto;
- }
- }
- $idas2 = $idas;
- $ln = $ln + 1;
- $monto2 = $monto;
- # Sum the values to get a total
- $monto3 = $monto3 + $monto;
- }
- }
- # Insert the values into the table using the information provided as a sting of values
- $insert = "ano, id, idas".$valor2.", total";
- $value = $year.", ".$id.", ".$idas.$tit2.", ".$monto3;
- $sql = "INSERT INTO `$table` (`$insert`) VALUES (`$value`)";
- mysql_query($sql);
- }
- ?>