Ad

Select Input Type=text Value As Column Name

- 1 answer

php

  $searchby =mysqli_real_escape_string($conn,$_GET['searchby']);
  $searchx =mysqli_real_escape_string($conn,$_GET['searchx']);
  $order=mysqli_real_escape_string($conn,$_GET['order']);

  $fetchquery9 = "SELECT * FROM reservation WHERE '$searchby' = '$searchx' ORDER BY '$order'"; //sql query
  $fetch9 = mysqli_query($conn, $fetchquery9);

ajax

  function sortdata(){
  var searchby = $('#searchby').val();
  var searchx = $('#searchx').val();
  var order = $('#order').val();


  $.ajax({
  type: "POST",
  url: "getdatasort.php?searchby="+searchby+"&searchx="+searchx+"&order="+order
}).done(function( data ) {
  $('#viewdata').html('');
  $('#viewdata').html(data);
 });
 }//passing value using ajax

html

<div class="row">
<div class="col-md-6">
</div>

<div class="col-md-2">
<select type="text" class="form-control" id="searchby" name="searchby"     placeholder="searchby" onChange="sortdata();">
<option value="reservationno">Reservation No.</option>
<option value="name">Full Name</option>
<option value="number">Contact Number</option>
<option value="email">Email</option>
<option value="course_reservation">Course</option>
<option value="class_code_reservation">Class Code</option>
<option value="startdate">Start Date</option>
<option value="enddate">End Date</option>
<option value="desired_date">Desired Date</option>
</select>
</div>

<div class="col-md-2">
<input type="text" class="form-control" id="searchx" name="searchx"             placeholder="Searh here" onChange="sortdata();">
</div>

<div class="col-md-2">
<select type="text" class="form-control" id="order" name="order">
<option value="ASC">ascending</option>
<option value="DSC">descending</option>
</select>
</div>

i just wanna know how to set a input type=text value as column name in sql query in php. Having 3 input text where a user can view sql data depending on the value of text value. first input contains sql column second input search item and third input the order (asc or desc)

Ad

Answer

Just need to remove the quotes

$fetchquery9 = "SELECT * FROM reservation WHERE $searchby = '$searchx' ORDER BY $order"; //sql query
Ad
source: stackoverflow.com
Ad