Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <?php
  2.  
  3. try
  4. {
  5.     //Open database connection
  6.     $con = mysql_connect("localhost","root","itesm");
  7.     mysql_select_db("sgv", $con);
  8.     //Getting records (listAction)
  9.     if($_GET["action"] == "list")
  10.     {
  11.         //Get record count
  12.         $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM ventas;");
  13.         $row = mysql_fetch_array($result);
  14.         $recordCount = $row['RecordCount'];
  15.  
  16.         //Get records from database
  17.         $result = mysql_query("
  18.             SELECT distinct ventas.ID_Venta, ventas.Fecha_Venta, clientes.ID_Cliente, clientes.Nombre, monedas.*
  19.             FROM ventas,clientes, monedas
  20.             WHERE clientes.ID_Cliente=ventas.ID_Cliente
  21.             AND monedas.ID_Moneda=ventas.ID_Moneda ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
  22.         //$result = mysql_query("SELECT * FROM ventas ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
  23.  
  24.         //Add all records to an array
  25.         $rows = array();
  26.        
  27.         while($row = mysql_fetch_array($result))
  28.         {
  29.             $rows[] = $row;
  30.         }
  31.  
  32.         //Return result to jTable
  33.         $jTableResult = array();
  34.         $jTableResult['Result'] = "OK";
  35.         $jTableResult['TotalRecordCount'] = $recordCount;
  36.         $jTableResult['Records'] = $rows;
  37.         print json_encode($jTableResult);
  38.     }
  39.     //Creating a new record (createAction)
  40.     else if($_GET["action"] == "create")
  41.     {
  42.         //Insert record into database
  43.         $result = mysql_query("INSERT INTO clientes(Nombre, Calle, NInt, NExt, Colonia, Ciudad, Estado, Pais, CP, Telefono1, Telefono2, EMail) VALUES('" . $_POST["Nombre"] . "', '" . $_POST["Calle"] . "'," . $_POST["NInt"] . "," . $_POST["NExt"] . ",'" . $_POST["Colonia"] . "','" . $_POST["Ciudad"] . "','" . $_POST["Estado"] . "','" . $_POST["Pais"] . "'," . $_POST["CP"] . "," . $_POST["Telefono1"] . "," . $_POST["Telefono2"] . ",'" . $_POST["EMail"] . "');");
  44.        
  45.         //Get last inserted record (to return to jTable)
  46.         $result = mysql_query("SELECT * FROM clientes WHERE ID_Cliente = LAST_INSERT_ID();");
  47.         $row = mysql_fetch_array($result);
  48.  
  49.         //Return result to jTable
  50.         $jTableResult = array();
  51.         $jTableResult['Result'] = "OK";
  52.         $jTableResult['Record'] = $row;
  53.         print json_encode($jTableResult);
  54.     }
  55.     //Updating a record (updateAction)
  56.     else if($_GET["action"] == "update")
  57.     {
  58.         //Update record in database
  59.    
  60.         $result = mysql_query("UPDATE clientes SET Nombre = '" . $_POST["Nombre"] . "', Calle= '" . $_POST["Calle"] . "', NExt= " . $_POST["NExt"] . ", NInt= " . $_POST["NInt"] . ", Colonia= '" . $_POST["Colonia"] . "', Ciudad= '" . $_POST["Ciudad"] . "', Estado= '" . $_POST["Estado"] . "', Pais= '" . $_POST["Pais"] . "',CP= " . $_POST["CP"] . ", Telefono1= " . $_POST["Telefono1"] . ", Telefono2= " . $_POST["Telefono2"] . ", EMail= '" . $_POST["EMail"] . "' WHERE ID_Cliente = " . $_POST["ID_Cliente"] . ";");
  61.  
  62.         //Return result to jTable
  63.         $jTableResult = array();
  64.         $jTableResult['Result'] = "OK";
  65.         print json_encode($jTableResult);
  66.     }
  67.     //Deleting a record (deleteAction)
  68.     else if($_GET["action"] == "delete")
  69.     {
  70.         //Delete from database
  71.         $result = mysql_query("DELETE FROM clientes WHERE ID_Cliente = " . $_POST["ID_Cliente"] . ";");
  72.  
  73.         //Return result to jTable
  74.         $jTableResult = array();
  75.         $jTableResult['Result'] = "OK";
  76.         print json_encode($jTableResult);
  77.     }
  78.    
  79.    
  80.  
  81.     //Close database connection
  82.     mysql_close($con);
  83.  
  84. }
  85. catch(Exception $ex)
  86. {
  87.     //Return error message
  88.     $jTableResult = array();
  89.     $jTableResult['Result'] = "ERROR";
  90.     $jTableResult['Message'] = $ex->getMessage();
  91.     print json_encode($jTableResult);
  92. }
  93.    
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement