简体   繁体   English

具有动态信息的SQL文本框/自动填充文本框

[英]SQL text boxes with dynamic information / auto fill text boxes

I am trying to make a webpage that connects to my SQL database and fetches information from a table and place it into a text box. 我正在尝试制作一个连接到SQL数据库并从表中获取信息并将其放入文本框的网页。 The information it fetches is dependent on what is entered into another text box. 它获取的信息取决于在另一个文本框中输入的内容。 Essentially its an "autofill" of information. 本质上,它是信息的“自动填充”。 I was curious whether this was possible. 我很好奇这是否可能。

To create a better understanding here is a layout of text boxes: 为了更好地理解,这里是文本框的布局:

<html>
<head>
<?php
$con = mssql_connect("abc", "123", "password");
if (!$con)
  {
  die('Could not connect: ' . mssql_get_last_message());
  }

mssql_select_db("db1", $con);

$result = mssql_query("SELECT * FROM FormData");
<head>

<title>test</title>

</head>

<body>
    <h1>test</h1>

    <form action="#">

        <p>Enter ID</p>

        <input type="text" name="ID"/> 
        <input type="text" name="NAME"/> 
        <input type="text" name="LASTNAME"/> 
        <input type ="button" value="submit" onclick="check(ID)" />



    </form>
</body>

Here an ID would be entered into a text box and then once the submit button was pressed, it would fetch the rest of the information from my sql database into the next boxes. 在这里,将在一个文本框中输入一个ID,然后按下提交按钮,它将从我的sql数据库中获取其余信息到下一个框中。 I don't even know whether this is possible, whether I can use Javascript in conjunction with this or something. 我什至不知道这是否可行,我是否可以将Javascript与之结合使用。 I searched but couldn't find much help on the subject. 我进行了搜索,但在该主题上找不到太多帮助。

I'm not even sure if its possible to do "if" statements within SQL - whenever I try I end up with a 500 error due to improper syntax. 我什至不知道是否有可能在SQL中执行“ if”语句-每当尝试时,由于语法不正确,我都会遇到500错误。

If I understand you correctly, all you need to do is on the submission of the form capture the ID they entered. 如果我对您的理解正确,那么您所要做的就是在提交表单时捕获他们输入的ID。

If the ID has not been submitted yet (either their first time hitting the page, or they failed to enter an ID) you should not perform any query and create a blank $row array like so: 如果尚未提交ID(他们第一次访问该页面或他们输入ID失败),则不应执行任何查询并创建一个空白的$ row数组,如下所示:

$row = array("ID" => "", "NAME" => "", "LASTNAME" => "");

You should then fill your form fields using this array <input type="text" name="NAME" value="<?php echo $row['NAME']; ?>"/> . 然后,您应该使用此数组<input type="text" name="NAME" value="<?php echo $row['NAME']; ?>"/>填写表单字段。

If the ID has been submited you then use this ID in your SQL query "SELECT * FROM FormData WHERE ID = $_POST['ID']" (Note: This is an example. You will need to escape the POST data to prevent SQL injection). 如果已经提交了ID,则可以在SQL查询"SELECT * FROM FormData WHERE ID = $_POST['ID']"使用此ID(注意:这是一个示例。您需要转出POST数据以防止SQL注射)。

You need to then pull this result into your $row array $row = mysql_fetch_row($result) which in turn is used by your fields to populate the value. 然后,您需要将此结果放入$ row数组$row = mysql_fetch_row($result)中,字段又将其用于填充值。

This entire procedure could be done with AJAX so that you don't have to perform a full page submit however based on your example the solution I have provided should be good enough. 整个过程可以使用AJAX完成,因此您不必执行整页提交,但是根据您的示例,我提供的解决方案应该足够好。

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

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