繁体   English   中英

查询SQL表以根据用户输入返回同一行的值

[英]Query an SQL table to return the value of the same row based on user input

假设我有以下SQL表:

DATE1      |    DATE2
--------------------------
4/21/2012  |  3/SHAVAT/5777
4/22/2012  |  4/SHAVAT/5777
4/23/2012  |  5/SHAVAT/5777 etc...

我想要做的是 - 根据表单AUTOMATICALLY的用户输入填写第二个字段。 他们是先做第1场还是第2场。

所以我开始这样做:

<input type="text" id="dname" name="dname" value="<?php echo $myrow['DATE1']?>"
<input type="text" id="dname" name="dname" value="<?php echo $myrow['DATE2']?>"

我试图使用的PhP是

<?php
$myrows = $wpdb->get_results( "SELECT heb_date, greg_date FROM calendar" )
?>

这就是我所得到的。 我想它检查表,找到任一字段的输入日期,然后用该结果填充OTHER字段。 基本上,如果Timmy在2007年4月12日进行了监听,则会对数据库进行检查,然后在同一行中的任何日期,但在第二列中,将其返回到另一个字段,反之亦然。

您需要在此处使用WHERE子句:

SELECT heb_date
FROM calendar
WHERE greg_date = '2007-12-04'

你也可能需要处理无法找到heb_date的情况,尽管理想情况下你的日历表中不应该有任何漏洞。

以下代码的作用是为您提供一个允许您填写日期的表单。 然后,它将使用该日期更新您的表,并将其设置为heb_dategreg_date如果这些列中的任何一列与输入的日期匹配。

你的HTML

<!-- Form -->
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="dname" id="dname" value="<?= $row['greg_date']; ?>" />
</form>

你的PHP / SQL

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $date = $_POST['dname'];

    $stmt = "UPDATE calendar
    SET heb_date='".$date."',
    SET greg_date='".$date."'
    WHERE heb_date='".$date."'
    OR greg_date='".$date."'
    ";

    // if you would like to select the dates however
    $stmt = "SELECT
    heb_date,
    greg_date
    FROM calendar
    WHERE greg_date = '".$date."'
    OR heb_date = '".$date."'
    ";
}

更新

这对我来说并不完全清楚。 如果您的意思是要将日期与行匹配并返回另一个日期,请执行以下操作。

// Do the following if the form submits
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $date = $_POST['dname'];

    $stmt = "SELECT
    heb_date,
    greg_date
    FROM calendar
    WHERE greg_date = '".$date."'
    OR heb_date = '".$date."'
    ";

    $myrows = $wpdb->get_results($stmt);

    // Not sure if its an array that is returned, but this will get the first value.
    $row = current($myrows);

    // Let's check which date is different
    if ($date != $row['greg_date']) {
        $date = $row['greg_date'];
    } else {
        $date = $row['heb_date'];
    }
}
?>

<!-- Form -->
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="dname" id="dname" value="<?= $date; ?>" />
</form>

使用AJAX更新

的index.html

<!-- Form -->
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="dname" id="dname" value="<?= $date; ?>" />
</form>

<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
    $(document).on('focusout', '#dname', function () {
        var dname = $(this).val();
        $.ajax({
            url: 'getdate.php',
            data: {
                'dname': dname
            },
            success:function(data) {
                // This outputs the result of the ajax request
                $('#dname').val(data);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    });
});
</script>

getdate.php

// Do the following if the form submits
if (isset($_REQUEST['dname'])) {
    $date = $_REQUEST['dname'];

    $stmt = "SELECT
    heb_date,
    greg_date
    FROM calendar
    WHERE greg_date = '".$date."'
    OR heb_date = '".$date."'
    ";

    $myrows = $wpdb->get_results($stmt);

    // Not sure if its an array that is returned, but this will get the first value.
    $row = current($myrows);

    // Let's check which date is different
    if ($date != $row['greg_date']) {
        $date = $row['greg_date'];
    } else {
        $date = $row['heb_date'];
    }

    echo $date;
} else {
    echo 'error';
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM