dongya9904 2016-08-19 01:51
浏览 29
已采纳

无论我尝试什么,sql都不返回数据

I would like my first field (entrynum) on update to perform a MySQLi search and if record found to auto populate the remaining fields.

I have updated the question and original post. I have a form field that onchange fires correctly but returns no data no matter what I try.

reg.php

<?php include("process.php"); ?>

<!doctype html>
<html>
<head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/entrynum.js"></script>
</head>
<body>
        <?php 
if (isset($_POST['reg-submit'])) {
    echo "<p id='notice' style='padding: .5em; border: 2px solid red;'>Entry $entrynum Saved!<br>$timenow on $datenow<br><a href='upload.php' style='font-size:xx-large;'>Upload Pictures</a></p>";
} else {
    echo "<p id='notice' style='display: none; padding: .5em; border: 2px solid red;'></p>";
}
?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <fieldset>
            <legend><h1>Registration</h1></legend>
            <label for="entrynum">Entry Number</label>
            <input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="entry_check()" />

            <label for="fname">First Name</label>
            <input type="text" name="fname" id="fname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />

            <label for="lname">Last Name</label>
            <input type="text" name="lname" id="lname" value="" required="true" placeholder="" list="" style="text-transform:capitalize" onkeyup="javascript:this.value=this.value.charAt(0).toUpperCase() + this.value.substr(1);" />

            <input type="submit" name="reg-submit" id="reg-submit" value="Submit" />
        </fieldset> 
    </form>
</body>
</html>

process.php

<?php
include("connect.php");
error_reporting(E_ALL);
ini_set('display_errors', 'On');
date_default_timezone_set('US/Central');
session_start();

$datenow = date("y-m-d");
$timenow = date("h:i:sa");

// registration
if (!empty($_POST['reg-submit'])) {
    $entrynum = $_POST['entrynum'];

    $_SESSION['entrynum'] = $entrynum;

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];


    $sql = "INSERT INTO HeatWaveData (entrynum, FName, LName)
        VALUES ('$_POST[entrynum]','$_POST[fname]','$_POST[lname]')";

    if (!$db->query($sql)) { die("Error: {$db->errno} : {$db->error}"); }

}
?>

entrynum.js

function entry_check() {


    var entrynum = $("#entrynum").val();

    // Send the AJAX call
    $.post(
        'entrysearch.php', // TO search.php page
        {entrynum: entrynum}, // the DATA sent with the request
        function(data) { // a CALLBACK function
            if (data == 'none') { // no rows were found or an error occurred
                document.getElementById("notice").innerHTML = "New Entry!";
                document.getElementById("notice").style.display = "block";
                return;
            } else {
                document.getElementById("notice").innerHTML = "Already Exists!";
                document.getElementById("notice").style.display = "block";
            }
            data = JSON.parse(data); // parse the array returned from the server

            // set the data in the matching fields (could be done manually if needed)
            // for (var i = 0; i < data.length; i++) {
            //  $("#data" + i).val(data[i]);
            // }
        }
    );

}

entrysearch.php

<?php
// check post data is received
if (!isset($_POST['entrynum'])) {
    echo 'none';
    die();
}
// create a prepared statement against sql-injection
$sql = $db->prepare("SELECT * FROM HeatWaveData WHERE entrynum=%d", $_POST['entrynum']);
// execute the query (depending on your db class type get the results otherwise)
$results = $db->query($sql);
// After this line results should be a matrix (change/add lined as needed)
// we need the first row returned (probably only 1 was returned...)
$result = $results[0];
// check if a row was returned
if (!$result) {
    echo 'none';
    die();
}
echo json_encode($result);
?>
  • 写回答

1条回答 默认 最新

  • donglang1976 2016-08-19 16:53
    关注

    what you need to do to achieve this is an onChange function for the input field which will send an ajax call to a script which will search for the data you need.
    Then using JavaScript you can update the relevant fields.

    I am writing here a code sample on the fly (not checked), but play with it around and it should work:
    note: I am using jQuery in my code

    foo.html

        <script>
        function input_change() {
            // Get the value from the input
            var entrynum = $("#entrynum").val();
    
            // Send the AJAX call
            $.post(
                    'search.php', // TO search.php page
                    {entrynum: entrynum}, // the DATA sent with the request
                    function(data) { // a CALLBACK function
                        if (data == 'none') { // no rows were found or an error occurred
                            return;
                        }
                        data = JSON.parse(data); // parse the array returned from the server
    
                        // set the data in the matching fields (could be done manually if needed)
                        for (var i = 0; i < data.length; i++) {
                            $("#data" + i).val(data[i]);
                        }
                    }
            );
        }
    </script>
    
    <input type="number" pattern="\d*" name="entrynum" id="entrynum" value="" required="true" placeholder="" autofocus onchange="input_change()" />
    
    <input type="text" name="data0" id="data0">
    <input type="text" name="data1" id="data1">
    <input type="text" name="data2" id="data2">
    <input type="text" name="data3" id="data3">
    

    search.php

    <?php
    //------------(maybe different depending on your db class type)----------------
    // begin sesstion and connect to db and stuff
    $db = //get database connection
    // check post data is received
    if (!isset($_POST['entrynum'])) {
        echo 'none';
        die();
    }
    // create a prepared statement against sql-injection
    $sql = $db->prepare("SELECT * FROM table WHERE entrynum=%d", $_POST['entrynum']);
    // execute the query (depending on your db class type get the results otherwise)
    $results = $db->query($sql);
    // After this line results should be a matrix (change/add lined as needed)
    // we need the first row returned (probably only 1 was returned...)
    $result = $results[0];
    // check if a row was returned
    if (!$result) {
        echo 'none';
        die();
    }
    echo json_encode($result);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?