MySQL 查询可以包含 HTML 值吗?

问题描述 投票:0回答:3

修改后的问题:

如何在填写表单时引用一行 MySQL 数据。换句话说,使用 my ('Select * FROM... 如何指定从中提取字段的行,而无需处理表单来获取 PHP 字段?

所需的过程 1) 用户输入第一个值,2) 侧边栏 MySQL 查询查找该值并使用该行中的数据填充 div,3) 用户继续填写表单,同时能够看到包含 MySQL 数据的 div

原问题:

我很好奇您是否可以在 MySQL 查询中包含对 HTML 输入的引用。

以下面为例。

('SELECT * FROM MyTableName WHERE MyColumnID="MyHTMLInputValue"');

我想根据输入字段的值引用行中的所有字段,但不确定如何引用此类查询。

谢谢!

php mysql sql search
3个回答
1
投票

Ajax 是必经之路。

此示例使用 jQuery。

首先我们需要设置一个 php 脚本来获取我们的值并搜索它,然后返回我们的数据。

PHP

myPhpScript.php

<?php
 $search_value = $_POST["field1"]; //Get our value from the post.
 //Make sure to do some authentication on the value so that bad values dont get through
 $connection = new mysqli(Stuff here);
 $rows = $connection->query(
 "SELECT * FROM someTable where searchColumn LIKE %".$search_value."%"
 );

 //Now all we need to do is send out our data. Lets use json.
 $out = $rows[0];//Get the first row. NOTE: Make sure to remove any columns that you dont want the client to see.

 echo json_encode($out); //Send the client the data.
?>

Javascript

Some script on the page.

var textbox = $("#mySearchBox"); //This is the jQuery object for our text box.
var viewbox = $("#myViewer"); //This is the jQuery object for the div that holds the results

textbox.on("input", function () {
$.ajax({
  url: "myPhpScript.php", //This is the php script that we made above.
  method: "POST", //This set our method to POST.
  data: {field1: textbox.val()}, //set the textbox value to field1
  dataType: "json", //So jquery formats the json into an object.
  success: function (phpDataObject) { //Now its up to you to decide what to do with the data. Example below
    viewbox.html("");//Clear the viewbox.
    viewbox.append("Id: "+phpDataObject["id"]); //Show the id of row that was found.
  }
});
});

此代码可能不起作用。只需修复出现的任何语法错误即可。

希望这有帮助。

如果您需要更多帮助,请评论。


0
投票

这听起来不是一个好主意。通常您希望站点和数据库之间有一定的分离。你可以接受“MyHTMLInputValue”,处理它,然后你就可以完成你的查询了。

var myValue = MyHTMLInputValue;
//validate input
('SELECT * FROM MyTableName WHERE MyColumnID="' + myValue + '"')

0
投票

是的,它可以包含任何 HTML。下面是它的示例:

SELECT concat('<b><u><i>',businessname,'</i></u></b>') as businessURL FROM tblbusiness
© www.soinside.com 2019 - 2024. All rights reserved.