1

Here is my PHP code which includes the MySQL database. In the MySQL database, there is one column 'amount' and another column is 'date'. I want to sum of the column amount between two dates. But I can't get my result. Please help me out.

<?Php
error_reporting(0);
include "config_1.php";
echo "<form  method=post action='getcollectionbagnan.php' ><input type=hidden  name=todo value=search>
<input type=date  name=search_text value='$search_text' ><br>
<input type=hidden  name=todo2 value=search2>
<input type=date  name=search_text2 value='$search_text2' ><input type=submit value=Search><br>
</form>
";
$todo=$_POST['todo'];
$search_text=$_POST['search_text'];
$todo2=$_POST['todo2'];
$search_text2=$_POST['search_text2'];
if(isset($todo) and $todo=="search" and isset($todo2) and $todo2=="search2"){
$type=$_POST['type'];
$search_text=ltrim($search_text);
$search_text=rtrim($search_text);
$search_text2=ltrim($search_text2);
$search_text2=rtrim($search_text2);
if($type<>"any"){
$query="select sum(amount) from billbagnan where date between $search_text and $search_text2 ";
$count=$dbo->prepare($query);
$count->execute();
$sumAmount = 0;
while($row = $count->fetch()) {
    $sumAmount = $row[0];
}
echo $sumAmount;
}
}
?>
Umar Abdullah
  • 1,282
  • 1
  • 19
  • 37
  • 2
    `$search_text` and `$search_text2` need to be inside single quotes in your query. – Nick Jan 21 '19 at 05:32
  • 1
    Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – Nick Jan 21 '19 at 05:32
  • 1
    Also learn how to use parameters/prepared statements. It would fix this problem as well keep you safe from users inputting malicious values. – Sami Kuhmonen Jan 21 '19 at 05:40
  • Thank you for your information. Now it works. But when I search only one item then if I didn't put single quotation then it works, but when I put search between two values it doesn't work. why? – Debayan Mondal Jan 21 '19 at 05:55

0 Answers0