Firstly, you may want to change all $table_name
to $tbl_name
because as it stands, you're using two different variables for your table checking. Or to make it even simpler, changing all instances of $tbl_name
to $table_name
which will require a lot less work. So at this point, it's uncertain as to which variable you meant to use, or if you have more code that you may not be showing us.
I.e.: if($tbl_name=="dailymeal")
and if($table_name="infomeal")
Therefore it's more than likely you would want to use:
$table_name =$_POST['report'];
if($table_name=="dailymeal")
Now, you have quotes around FROM '$tbl_name'
those need to be removed or use backticks if you wish to escape it.
You should have used (or you meant to use) the same method you used in
SELECT * FROM `".$tbl_name."`
Plus, you're assigning =
instead of comparing ==
using if($table_name="infomeal")
and many others.
You also have a few missing braces for if($tbl_name=="dailymeal")
Comparison ==
: http://www.php.net/manual/en/language.operators.comparison.php
Assignment =
: http://www.php.net/manual/en/language.operators.assignment.php
Rewrite:
$tbl_name = $_POST['report']; // or $table_name
// or $table_name
if($tbl_name=="dailymeal"){
$select = "SELECT * FROM `".$tbl_name."` where a4>='$tanggal_awal' and a4 <='$tanggal_akhir'";
// alternate method
// $select = "SELECT * FROM $tbl_name where a4>='$tanggal_awal' and a4 <='$tanggal_akhir'";
}
if($table_name=="infomeal"){
$select = "SELECT * FROM `".$tbl_name."` where tanggal >=`".$tanggal_awal."` and tanggal <=`".$tanggal_akhir."`";
}
if($table_name=="keluhan"){
$select = "SELECT * FROM `".$tbl_name."` where tlapor >=`".$tanggal_awal."` and tlapor <=`".$tanggal_akhir."`";
}
if($table_name=="perjalanan"){
$select = "SELECT * FROM `".$tbl_name."` where request_date>=`".$tanggal_awal."` and request_date <=`".$tanggal_akhir."`";
}
if($table_name=="tamu"){
$select = "SELECT * FROM `".$tbl_name."` where jam_masuk>=`".$tanggal_awal."` and jam_masuk <=`".$tanggal_akhir."`";
}
if($table_name=="tiket"){
$select = "SELECT * FROM `".$tbl_name."` where waktu_input>=`".$tanggal_awal."` and waktu_input <=`".$tanggal_akhir."`";
}
if($table_name=="trx_kons"){
$select = "SELECT * FROM `".$tbl_name."` where date_trx>=`".$tanggal_awal."` and date_trx <=`".$tanggal_akhir."`";
}
if($table_name=="uniform"){
$select = "SELECT * FROM `".$tbl_name."` where reqtime >=`".$tanggal_awal."` and reqtime <=`".$tanggal_akhir."`";
}
if($table_name=="konsumable"){
$select = "SELECT * FROM `".$tbl_name."`";
}
mysql_query('SET NAMES utf8;');
$export = mysql_query($select);
Footnotes:
Your present code is open to SQL injection. Use prepared statements, or PDO.
mysql_*
functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
Debugging/Troubleshooting
Add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);