plant-rec-update.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. //Database connection
  3. //$con = mysqli_connect("localhost", "root", "R3M0T31", "cropmonitor");
  4. $con = mysqli_connect("localhost", "cropmonitor", "brvnCcaEYxlPCS3", "cropmonitor");
  5. /* check connection */
  6. if (mysqli_connect_errno()) {
  7. printf("Connect failed: %s\n", mysqli_connect_error());
  8. exit();
  9. }
  10. $empid = mysqli_real_escape_string($con, $_REQUEST["empid"]);
  11. $user_id = $modx->user->get('id');
  12. $query = "CREATE TEMPORARY TABLE `plant_temp`
  13. SELECT id, modx_user_id, plant_type, plant_stage, n_min, n_max, P_min, P_max, K_min, K_max, S_min, S_max, Ca_min, Ca_max, Mg_min, Mg_max, Na_min, Na_max, Cu_min, Cu_max, Zn_min, Zn_max, Mn_min, Mn_max, B_min, B_max, Fe_min, Fe_max, M_min, M_max, Co_min, Co_max, se_min, se_max, cl_min, cl_max, c_min, c_max
  14. FROM `plant_specifications`
  15. WHERE `modx_user_id` = '" . $user_id . "'
  16. AND `plant_type` = '" . $empid . "'; ";
  17. $query .= "ALTER TABLE `plant_temp`
  18. DROP `id`,
  19. DROP `modx_user_id`,
  20. DROP `plant_type`
  21. ; ";
  22. $query .= "SELECT * FROM `plant_temp`; ";
  23. $result = mysqli_multi_query($con, $query);
  24. if ($result) {
  25. do {
  26. // grab the result of the next query
  27. if (($result = mysqli_store_result($con)) === false && mysqli_error($con) != '') {
  28. echo "Query failed: " . mysqli_error($con);
  29. }
  30. } while (mysqli_more_results($con) && mysqli_next_result($con)); // while there are more results
  31. } else {
  32. echo "First query failed..." . mysqli_error($con);
  33. }
  34. //get results from database
  35. $all_property = array(); //declare an array for saving property
  36. //showing property
  37. echo '<div class="table-responsive text-nowrap">
  38. <table class="table table-sm table-striped table-hover table-bordered">
  39. <tbody>
  40. <tr>'; //initialize table tag
  41. while ($property = mysqli_fetch_field($result)) {
  42. $header = str_replace("_"," ",$property->name);
  43. echo '<th scope="col" class="text-center text-capitalize" >' . $header . '</th>'; //get field name for header
  44. array_push($all_property, $property->name); //save those to array
  45. }
  46. echo '</tr>'; //end tr tag
  47. //showing all data
  48. while ($row = mysqli_fetch_array($result)) {
  49. echo "<tr>";
  50. foreach ($all_property as $item) {
  51. if ($row[$item] == "" ) { $this_value = "0.0" ; } else { if (is_numeric($row[$item]) ) { $this_value = number_format((float)$row[$item], 2, '.', ''); } else { $this_value = $row[$item]; } }
  52. echo '<td scope="row" class="text-center" contenteditable="true" onBlur="updateDatabase(this,"'. $row[$item->id] . '","' . $row['id'] . '")" onClick="showEdit(this);" >' . $this_value . '</td>'; //get items using property value
  53. }
  54. echo '</tr>';
  55. }
  56. echo "</tbody></table></div>";
  57. echo "
  58. <div class='row'>
  59. <div class='col-2'>
  60. <div class='row '>
  61. ";
  62. $img_query = 'SELECT * FROM `plant_images` ORDER BY `id` DESC';
  63. $img_result = mysqli_query($con, $img_query);
  64. while($img_row = mysqli_fetch_array($img_result)) {
  65. echo ' <img src="data:image/jpeg;base64,'.base64_encode($img_row['name'] ).'" class="img-thumbnail rounded float-left border-success" alt="" /> ';
  66. }
  67. echo "
  68. </div>
  69. <br>
  70. <div class='row'>
  71. <form method='post' enctype='multipart/form-data'>
  72. <div class='form-group'>
  73. <input type='file' class='form-control-file' name='image' id='image' />
  74. </div>
  75. <input type='submit' name='insert' id='insert' value='Insert' class='btn btn-info btn-sm' />
  76. </form>
  77. </div>
  78. </div>
  79. <div class='col-10 border border-success'>
  80. <div class='row'>
  81. </div>
  82. </div>
  83. </div>
  84. ";
  85. $query .= "DROP TEMPORARY TABLE `temp`; ";
  86. mysqli_query($con, $query);
  87. mysqli_close($con);
  88. ?>