[英]Can't update database using PHP and JavaScript
我正试图建立一个非常简单的“预算”网站,仅用于练习,但无法使该网站更新我的数据库。
基本上,我有一个在MySQL的XAMPP中运行的数据库'budgetdb'。 我有一张表,其结构如下所示:
我有两个文件,“ index.html”和“ handleUserInput.php”。
Index.html:
<!DOCTYPE html>
<html>
<body>
<input type="text" id="description">
<input type="number" id="budgetin">
<input type="number" id="budgetout">
<button type="button" onclick="updateDB()">Add to database</button>
<script>
function updateDB() {
var description = document.getElementById('description').value;
var budgetin = document.getElementById('budgetin').value;
var budgetout = document.getElementById('budgetout').value;
var xmlhttp = new XMLHttpRequest();
var url = "handleUserInput.php?description='" + description + "'&budgetin='" + budgetin + "'&budgetout='" + budgetout + "'";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert('Variables sent to server!');
}
}
xmlhttp.open("POST", url);
xmlhttp.send();
}
</script>
</body>
</html>
handleUserInput.php:
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "budgetdb"
mysql_connect($host, $username, $password;
mysql_select_db($dbname);
$description = $_POST['description'];
$budgetin = $_POST['budgetin'];
$budgetout = $_POST['budgetout'];
$query = 'INSERT into budget VALUES ($description, $budgetin, $budgetout)';
mysql_query($query)
?>
显示消息提示,但数据库中未显示任何数据。 关于我在这里做错什么的任何线索吗?
更新 Chrome错误:
Notice: Undefined index: description in /Applications/XAMPP/xamppfiles/htdocs/handleUserInput.php on line 13
Notice: Undefined index: budgetin in /Applications/XAMPP/xamppfiles/htdocs/handleUserInput.php on line 14
Notice: Undefined index: budgetout in /Applications/XAMPP/xamppfiles/htdocs/handleUserInput.php on line 15
其他答案显示了编写查询的方式的问题。 应该是,每个值都带有引号。
$query= "INSERT into budget VALUES('$description',$budgetin,$budgetout)";
但是,创建URL的方式也存在问题。 不应在查询参数周围加上引号,而应使用encodeURIComponent
来确保特殊字符正确转义。
var url = "handleUserInput.php?description=" + encodeURIComponent(description) + "&budgetin=" + encodeURIComponent(budgetin) + "&budgetout=" + encodeURIComponent(budgetout);
为了防止SQL注入问题,需要在使用字符串作为SQL参数之前对其进行转义。 并且由于要在URL中而不是在POST
数据中发送参数,因此需要从$_GET
而不是$_POST
。
$description = mysql_real_escape_string($_GET['description']);
尽管如果您现在是第一次学习PHP,则应使用PDO或mysqli而不是过时的mysql扩展,并使用准备好的语句而不是字符串替换。
将执行查询的行更改为:
mysql_query($query) or die(mysql_error());
如果执行查询时出现问题,将显示错误消息。
采用
$query = "INSERT into budget VALUES ('$description', $budgetin, $budgetout)";
description是数据库中的字符串类型,因此您可以在PHP String中使用单引号或双引号将其关闭。
始终考虑安全性。 将代码重新写入为(有些安全):
$description = mysql_real_escape_string( $_POST['description'] );
$budgetin = intval( $_POST['budgetin'] );
$budgetout = intval( $_POST['budgetout'] );
$query = 'INSERT into budget VALUES ("$description", $budgetin, $budgetout)';
注意:不要使用mysql_ *函数。 在将来的版本中不推荐使用。 使用MySQLi或PDO
查询中的引号不正确。 应该是(请注意外部双引号和内部单引号):
$query= "INSERT into budget VALUES('$description',$budgetin,$budgetout)";
但是,请注意,这使您可以进行SQL注入,其中描述为
'); DROP budget;
另外,您的描述中可能包含标点符号和空格,这可能会使您的网址混乱。 无论如何,您都在发送POST,因此所有数据都应位于请求的正文中,而不是url中。
您将值作为GET参数而不是POST参数发送。
handleUserInput.php?description='" + description + "'&budgetin='" + budgetin + "'&budgetout='" + budgetout + "'"
您通过url传递给PHP的所有内容都将属于$ _GET变量。 不仅每种服务器端语言的PHP行为相同,一旦成为标准GET,则表示URL参数,而POST是请求有效负载
您可以将$ _POST更改为 $ _GET或将数据添加到xmlhttprequest对象
<!DOCTYPE html>
<html>
<body>
<input type="text" id="description">
<input type="number" id="budgetin">
<input type="number" id="budgetout">
<button type="button" onclick="updateDB()">Add to database</button>
<script>
function updateDB() {
var description = document.getElementById('description').value;
var budgetin = document.getElementById('budgetin').value;
var budgetout = document.getElementById('budgetout').value;
var params = "description=" + description + "&budgetin=" + budgetin + "&budgetout=" + budgetout;
var xmlhttp = new XMLHttpRequest();
xmlhttp .setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp .setRequestHeader("Content-length", params.length);
xmlhttp .setRequestHeader("Connection", "close");
var url = "handleUserInput.php?";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert('Variables sent to server!');
}
}
xmlhttp.open("POST", url,true);
xmlhttp.send(params);
}
</script>
</body>
</html>
为了最小化您的代码,请使用Jquery库而不是简单的JS代码
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<input type="text" id="description">
<input type="number" id="budgetin">
<input type="number" id="budgetout">
<button type="button" onclick="updateDB()">Add to database</button>
<script>
function updateDB() {
var description = $('#description');
var budgetin = $('#budgetin');
var budgetout = $('#budgetout');
$.post( "handleUserInput.php", { "description": description, "budgetin": budgetin, "budgetout": budgetout }, function( data ) {
alert( "Variables sent to server!" );
} );
}
</script>
</body>
</html>
使用mysql_real_escape_string
以避免将SQL注入到您的应用程序中。 最初的问题是由于php变量未正确添加到SQL查询的字符串中引起的,例如:
$input = 'aaa';
echo 'text $input';
该代码将输出
text $input
但是这个
$input = 'aaa';
echo "text $input";
或这个
$input = 'aaa';
echo "text ".$input.";
将输出
text aaa
请检查下面的代码
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "budgetdb"
mysql_connect($host, $username, $password;
mysql_select_db($dbname);
$description = $_POST['description'];
$budgetin = $_POST['budgetin'];
$budgetout = $_POST['budgetout'];
$description = mysql_real_escape_string($description);
$budgetin= mysql_real_escape_string($budgetin);
$budgetout= mysql_real_escape_string($budgetout);
$query = "INSERT into budget VALUES ('".$description."', ".$budgetin.", ".$budgetout.")";
mysql_query($query)
?>
使用如下所示的PHP变量使用单引号逗号。
$query = "INSERT into budget VALUES ('$description', $budgetin, $budgetout)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.