使用php mysql在第1行附近使用正确的语法

时间:2015-04-02 11:46:02

标签: php mysql

我从表tbl_permintaan获得了一个简单的过滤器tgl代码。

当我点击Cari时出现错误

  

查询permintaan salah:你的SQL语法有错误;校验   与您的MySQL服务器版本对应的手册   在'第1行'附近使用的语法

和代码

<script type="text/javascript">
$(document).ready(function(){
    $('#datepicker-examples14').Zebra_DatePicker({
        format: 'Y-m-d'
    });
    $('#datepicker-examples15').Zebra_DatePicker({
        format: 'Y-m-d'
    });
});
</script>
<br><br><br>
<br><br>
<div class="panel-heading">
    <h3 class="panel-title">
    Laporan Permintaan Bulanan
    </h3>
</div>
<div class="panel-body">
    <form action="?page=Permintaan-bulanan" method="post" class="form-horizontal" role="form">
        <div class="form-group">
            <label class="col-lg-2 col-md-3 control-label">Dari tangal</label>
            <div class="col-lg-3 col-md-2">
                <input type="text" id="datepicker-examples14" name="mulai">
            </div>
            <label class="col-lg-2 col-md-3 control-label">Sampai tangal</label>
            <div class="col-lg-3 col-md-2">
                <input type="text" id="datepicker-examples15" name="akhir">
            </div>
        <input type="submit" class="btn btn-primary" name="btnCari" value="Cari">
        </div>
    </form>
</div>
<table class="table table-hover table-stripped">
    <thead>
        <tr>
            <th>#</th>
            <th>Identitas</th>
            <th>Nama Kegiatan</th>
            <th>Kegiatan</th>
            <th>Banyak</th>
            <th>Tanggal</th>
            <th>Status</th>
        </tr>
    </thead>
    <?php 
    if(isset($_POST["btnCari"])){
        $mulai = $_POST["mulai"];
        $akhir = $_POST["akhir"];

        $userSql = "SELECT * FROM tbl_permintaan INNER JOIN tbl_kegiatan ON id_kegiatan = id_k INNER JOIN tbl_souvenir ON id_souvenir = id_s INNER JOIN tbl_loker ON id_loker = pt_pengirim WHERE tgl BETWEEN '".$_POST['mulai']."' AND '".$_POST['akhir']."' LIMIT $hal, $row";
        $userQry = mysql_query($userSql, $koneksidb)  or die ("Query permintaan salah : ".mysql_error());
        while ($userRow = mysql_fetch_array($userQry)) {
        $nomor = 1;
        $Kode = $userRow['id_permintaan'];
        $banyak = $userRow['banyak']+$userRow['bkedua']+$userRow['bketiga']+$userRow['bkeempat']+$userRow['bkelima']+$userRow['brosur_banyak'];
    ?>
    <tbody>
        <tr>
            <td><?php echo $nomor; ?></td>
            <td><small>Nama <b><?php echo $userRow['nama_pengirim']; ?></b>, dari <b><?php echo $userRow['nama_loker']; ?></b></small></td>
            <td><small><?php echo $userRow['nama_kegiatan']; ?></small></td>
            <td><small>Kegiatan <?php echo $userRow['nm_kegiatan']; ?></small></td>
            <td><small><?php echo $banyak; ?></small></td>
            <td><small><?php echo $userRow["tgl"];?></small></td>
            <td><small><?php
            if($userRow['stts']==1){
                echo '<a href="?page=Edit-Permintaan&amp;Kode='.$Kode.'" target="_self" title="Edit Permintaan"><span class="label label-info">Pending</span></a>';
            }elseif($userRow['stts']==2)
            {
                echo '<span class="label label-success">Diterima</span>';
            }elseif ($userRow['stts']==0) {
                echo '<span class="label label-danger">Ditolak</span>';
            }
            ?></small></td>
        </tr>
    </tbody>
    <?php
        $nomor++;}}
    ?>
</table>

1 个答案:

答案 0 :(得分:0)

你加入了许多表,你必须写一个值tablePrefixes

示例sql:

$userSql = "SELECT * FROM `tbl_permintaan` AS `tp` 
INNER JOIN `tbl_kegiatan` AS `tk` ON `tk`.`id_kegiatan` = `tp`.`id_k` 
INNER JOIN `tbl_souvenir` AS `ts` ON `ts`.`id_souvenir` = `tp`.`id_s` 
INNER JOIN `tbl_loker` AS `tl` ON `tl`.`id_loker` = `tp`.`pt_pengirim` 
WHERE `tp`.`tgl` BETWEEN '" . $mulai . "' AND '" . $akhir . "' LIMIT " . $hal.", " " . $row . ";";

警告: 并且您必须以php

验证和转义您的值

你可能会在将来发现语法错误

注意:

$userQry = mysql_query($userSql, $koneksidb)  or die ("Query permintaan salah : ".mysql_error()); 
        var_dump($userQry);

在php5.5上弃用了mysql_query

请使用PDO 并尝试catch来调试sql