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>
我只想知道如何在php中的sql查询中将输入类型=文本值设置为列名。有3个输入文本,用户可以根据文本值的值查看sql数据。第一个输入包含sql列第二个输入搜索项和第三个输入顺序(asc或desc)
答案 0 :(得分:1)
只需删除引号
即可$fetchquery9 = "SELECT * FROM reservation WHERE $searchby = '$searchx' ORDER BY $order"; //sql query