Home > Net >  how to properly display records by product id in php
how to properly display records by product id in php

Time:08-21

The Query Below displays all products from database and its working fine.

$sql_query .= "SELECT * FROM my_products ";
if(strip_tags(isset($_POST["search"]["value"]))){

$search_value= strip_tags($_POST["search"]["value"]);
$sql_query .= 'WHERE  first_name LIKE "%'.$search_value.'%" ';
$sql_query .= 'OR last_name LIKE "%'.$search_value.'%" ';
$sql_query .= 'OR product_name LIKE "%'. $search_value.'%" ';
  }



//ensure that order post is set
$start = $_POST['start'];
$length = $_POST['length'];
$draw= $_POST["draw"];
if(strip_tags(isset($_POST["order"]))){
$order_column = strip_tags($_POST['order']['0']['column']);
$order_dir = strip_tags($_POST['order']['0']['dir']);

$sql_query .= 'ORDER BY '.$order_column.' '.$order_dir.' ';
}
else{
$sql_query .= 'ORDER BY id DESC ';
}
if($length != -1){
$sql_query .= 'LIMIT ' . $start . ', ' . $length;
}

$pstmt = $db->prepare($sql_query);
$pstmt->execute();
$rows_count = $pstmt->rowCount();


while($row = $pstmt->fetch()){
$rows1 = array();

$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
}

Here is my issue: I want to display the products records where product id is 100.

I have tried code below but its still showing all the record. can someone help me out.

$product_id= '100';

$sql_query .= 'WHERE product_id ="'.$product_id.'" AND  first_name LIKE "%'.$search_value.'%" ';
$sql_query .= 'OR last_name LIKE "%'.$search_value.'%" ';
$sql_query .= 'OR product_name LIKE "%'. $search_value.'%" ';

CodePudding user response:

Check your query and run it in database.

I think this is a bracket issue of OR condition. Try this -

$product_id= '100';

$sql_query .= 'WHERE (product_id ="'.$product_id.'" AND  first_name LIKE "%'.$search_value.'%" )';
$sql_query .= 'OR last_name LIKE "%'.$search_value.'%" ';
$sql_query .= 'OR product_name LIKE "%'. $search_value.'%" ';
  •  Tags:  
  • php
  • Related