[英]HTML(input)->php->mysql->php->json->HTML render
Description of problem: I want to get value from HTML input with php and to run query on mysql database: SELECT * FROM WHERE (value=value of ID in HTML input) 问题描述:我想用php从HTML输入中获取值并在mysql数据库上运行查询:SELECT * FROM WHERE(值= HTML输入中ID的值)
WHAT I TRY TO DO: I write HTML input: 我要做的事情:我编写HTML输入:
<input id="akt_djubrenje" type="text" value="1">
THEN I WRITE PHP: 然后我写PHP:
try {
/* Establish the database connection */
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $conn->query("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));
...
...
etc.
ALSO I give a php file data of input fields and call php file with AJAX: 我还提供了输入字段的php文件数据,并使用AJAX调用了php文件:
function tabela() {
$.ajax({
url: 'getdjubrenje.php', // make this url point to the data file
dataType: 'json',
data:{akt_djubrenje:$("#akt_djubrenje").val()},
async: false,
success:function(json){
var data = new google.visualization.DataTable(json);
alert (json);
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('tabela_djubrenje'));
visualization.draw(data, {'allowHtml': true});
}
});
}
My problem is with HOW TO GET INPUT field VALUE from HTML and fetch with php/ajax 我的问题是如何从HTML获取输入字段值并使用php / ajax获取
When I type exactly value etc. "1" instead $_POST
... all works fine, so problem is with this code. 当我键入确切的值等时,用“ 1”代替$_POST
...一切正常,因此此代码存在问题。 Anybody have some idea to solve my problem? 有人有解决我问题的想法吗?
Your problem is that you use PDO::query()
and then try to bind variables. 您的问题是您使用PDO::query()
,然后尝试绑定变量。 That's now how query()
works: 这就是query()
工作方式:
PDO::query() executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object. PDO :: query()在单个函数调用中执行一条SQL语句,以PDOStatement对象的形式返回该语句返回的结果集(如果有)。
Hence it tries to execute the query right away, not waiting for your execute()
call. 因此,它尝试立即执行查询,而不等待您的execute()
调用。 This is why you get " You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near :akt_djubrenje
at line 1. ". 这就是为什么出现“ 您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以:akt_djubrenje
在第1行:akt_djubrenje
附近使用正确的语法 ”。
What you should do is use PDO::prepare()
in order to prepare queries and bind variables: 您应该做的是使用PDO::prepare()
来准备查询和绑定变量:
$result = $conn->prepare("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
// ^^^^^^
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));
I think you are submitting your request as GET instead of POST. 我认为您是以GET而不是POST的形式提交请求。 Because you haven't included the sending type as POST in $.ajax. 因为您尚未在$ .ajax中将发送类型包括为POST。 Please correct it. 请更正。
add option type="POST"
in your $.ajax({...,}); 在$ .ajax({...,})中添加选项type="POST"
;
instead of 代替
$result = $conn->query("SELECT * FROM akt_djubrenje WHERE ID_akt = :akt_djubrenje");
$result->execute(array(':akt_djubrenje' => $_POST['akt_djubrenje']));
add 加
$result = $conn->prepare("SELECT * FROM akt_djubrenje WHERE ID_akt = ?");
$result->execute(array($_POST['akt_djubrenje']));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.