Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用ajax从mysql数据库中选择月份和年份_Php_Jquery_Mysql_Ajax_Json - Fatal编程技术网

Php 如何使用ajax从mysql数据库中选择月份和年份

Php 如何使用ajax从mysql数据库中选择月份和年份,php,jquery,mysql,ajax,json,Php,Jquery,Mysql,Ajax,Json,我正在使用以下脚本检索特定月份/年度的所有发票。 php从数据库中提取所有年份和日期,将它们组合在一起,并将它们放入一个选择菜单中,结果类似于: June - 2013 July - 2013 August - 2013 September - 2013 这是selectsummary.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/D

我正在使用以下脚本检索特定月份/年度的所有发票。 php从数据库中提取所有年份和日期,将它们组合在一起,并将它们放入一个选择菜单中,结果类似于:

June - 2013
July - 2013
August - 2013
September - 2013
这是selectsummary.php:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Sales Summary</title>
      <script type="text/javascript" src="../js/jquery/jquery.js"></script>
    <script type="text/javascript" src="../js/jqueryui/js/jquery-ui.js"></script>
       <link href="../js/select2/select2.css" rel="stylesheet"/>
        <script type="text/javascript" src="../js/select2/select2.js"></script>
         <script type="text/javascript">
            $(document).ready(function() { $("select").select2(); });
        </script>

        <?php
    include '../connectmysqli.php';
    include '../menu.php';
         echo '<link rel="stylesheet" href="../css/template/template.css" />';
    $salesID = rand().rand();
    $today = date("Y-m-d");

    ?>
    <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){
            $('#selectmonth').on('change', function (){

              // THIS IS WHERE I AM TRYING TO WORK OUT HOW TO RETRIEVE THE JSON AND THEN PLACE THE RESULTS INSIDE THE "summarycontent" DIV.     

                     $.getJSON('select.php', {monthyear: $(this).val()}, function(data){
                        var invoicerow = '';
                        for (var x = 0; x < data.length; x++) {
                            invoicerow += '<p>' + data[x]['invoiceID'] + '">' + data[x]['date'] + ' - ' + data[x]['grandtotal'] + ' - ' + data[x]['customerID'] + '</p>';
                        }
                        $('#summarycontent').html(invoicerow);
                      $("select").select2();
                    });

                    });     
                    });
            </script>
    </head>
    <body>
        <form method="post" action="addsalesubmit.php">
        <p>
          <select id="selectmonth">
            <option>Please Select A Monthly Summary To View</option>
            <?php

    $sql = <<<SQL
    SELECT YEAR(date) AS 'year', MONTHNAME(date) AS 'month'
    FROM `sales`
    GROUP BY YEAR(date), MONTHNAME(date) DESC
    SQL;

    if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
    while($row = $result->fetch_assoc()){
    echo '<option value="'.$row['month'].'-'.$row['year'].'">'.$row['month'].' - '.$row['year'].'</option>';
    }
    echo '</select>';
            ?>
            <br />
            <br />
        </form>
    <div id="summarycontent"></div>

    </body>
    </html>
所以日期出来了,但我不知道如何在另一端使用它来选择该月/年的日期,因为它的格式错误

数据库如下:

     id invoiceID   salesID customerID  vehicleID   date    comments    subtotal    vat grandtotal  description1    qty1T   linetotal1T stock1T stock2T description2    qty2T   linetotal2T stock3T description3    qty3T   linetotal3T stock4T description4    qty4T   linetotal4T stock5T description5    qty5T   linetotal5T discount
    68  1     1512428605    82428579    134722464   2013-07-08      22.48   4.50    26.98   Bridestone Pt34 - 175/55/18/W/63 - (99 In Stock) -...   1   22.48   711022407                                                               
编辑>>>

select.php现在如下所示:

                    <?php include '../connectmysqli.php';

    $date_convert = date('Y-m-d', strtotime($_POST['monthyear']));
试试这个

使用

Url:
select.php?monthyear=2013年7月

$date = mysql_real_escape_string($_POST['monthyear']);

$mysql_date = date('Y-m-d', strtotime($_GET['monthyear']));

$sql = 'SELECT * FROM sales WHERE date = ' . $mysql_date;
  • 另外,不要只是将参数连接到sql命令。我们已经不是90年代了
更好的选择是使用
bind_param()


转换数据

$date_convert = date('Y-m-d', strtotime($_POST['monthyear']));

// Or, use LIST which would allow you to use the two date parts later.

list($month, $year) = explode('-', $_POST['monthyear']);
获取结果

$SQL = "SELECT * FROM myTable WHERE date = :date";
$STH = $DBH->prepare($SQL);
$STH->bindParam(':date', $date_convert);

经过多次尝试,select.php终于成功了:

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

            include '../connectmysqli.php';

            list($month, $year) = explode('-', $_GET['monthyear']);

             $date = date_parse($month);
      $month = $date['month'];


    $sql = "SELECT * FROM sales WHERE MONTH(date) = '$month' AND YEAR(date) = '$year'";
    $result = $db->query($sql);

    $json = array();
            while ($row = $result->fetch_assoc()) {
              $json[] = array(
                'invoiceID' => $row['invoiceID'],
                'date' => $row['date'],
                'grandtotal' => $row['grandtotal'],
                'customerID' => $row['customerID']
              );
            }
            echo json_encode($json);

            ?>

请看我的答案。当我使用上面的方法时,我得到:select.php?monthyear=August-2013/manda/salessummmary get 500 Internal Server Error text/html jquery.js:8526 Script 226 B 4 B 966 ms 954 ms in console这是chrome上的开发者控制台,我猜它来自ajax,来自php select文件iv刚刚注意到iv使用了你的底层PDO方法,但是我用mysqli连接到数据库,并在其他所有事情上使用mysqli,因为我无法计算PDO。@IainSimpson已更新。我得到的只是这个日志,没有返回:select.php?monthyear=August-2013/manda/salessumary我完全不懂PDO,就像对我说西班牙语一样哈哈,我使用mysqliThank!,我在dreamweaver的这一行上得到一个错误:$date_convert=date($Y-m-d',strotime($\u POST['monthyear'));它应该是$date_convert=date($Y-m-d',strotime($\u POST['monthyear');在我得到的服务器上:[2013年9月12日06:33:42美国/丹佛]PHP通知:未定义索引:第3行[2013年9月12日06:33:42美国/丹佛]PHP通知:未定义偏移量:第7行[2013年9月12日06:33:42美国/丹佛]PHP通知:未定义偏移量:1 in/home3/websit52/public_html/manda/salessemmary/select.PHP在第11Iv行的/home3/websit52/public_html/manda/salessummmary/select.php中的非对象上,刚刚添加了上面的更新代码,现在获得:[12-Sep-2013 06:36:19 America/Denver]php注意:未定义索引:第3行[12-Sep-2013 06:36:19 America/Denver]中的monthyear in/home3/websit52/public_html/manda/salessumsummary/select.php]PHP注意:未定义的偏移量:第7行的/home3/websit52/public_html/manda/salessummmary/select.PHP中的1[2013年9月12日06:36:19 America/Denver]PHP致命错误:在第11行的/home3/websit52/public_html/manda/salessummmary/select.PHP中对非对象调用成员函数bindParam()
$mysql_date = date('Y-m-d', strtotime($_GET['monthyear']));

$sql = 'SELECT * FROM sales WHERE date = ?';

$sql->bind_param("s", $mysql_date);
$date_convert = date('Y-m-d', strtotime($_POST['monthyear']));

// Or, use LIST which would allow you to use the two date parts later.

list($month, $year) = explode('-', $_POST['monthyear']);
$SQL = "SELECT * FROM myTable WHERE date = :date";
$STH = $DBH->prepare($SQL);
$STH->bindParam(':date', $date_convert);
    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

            include '../connectmysqli.php';

            list($month, $year) = explode('-', $_GET['monthyear']);

             $date = date_parse($month);
      $month = $date['month'];


    $sql = "SELECT * FROM sales WHERE MONTH(date) = '$month' AND YEAR(date) = '$year'";
    $result = $db->query($sql);

    $json = array();
            while ($row = $result->fetch_assoc()) {
              $json[] = array(
                'invoiceID' => $row['invoiceID'],
                'date' => $row['date'],
                'grandtotal' => $row['grandtotal'],
                'customerID' => $row['customerID']
              );
            }
            echo json_encode($json);

            ?>