PHP 和 MySQL 基于用户输入的查询不起作用
PHP and MySQL query based on user input not working
我正在创建一个用户数据库,其中有 4 个字段:ID、用户名、密码和职业。这是一个测试数据库。我尝试查询数据库 table 并且它有效,但我在用户输入和基于它的 MySQL 查询时遇到了很多麻烦。我在 Linux(Debian,Ubuntu)中 运行 一个 Apache 服务器。
我有 2 页。第一个是简单的测试索引页。这是人们输入简单信息以在数据库中注册他们的信息的文本框。这是它的代码:
<html>
<form action="reg.php" method="POST">
Username:
<input type="text" name="u">Password:
<input type="password" name="p">Occupation:
<input type="text" name="o">
<input type="submit" value="register">
</form>
</html>
点击提交按钮后。它转到 reg.php 文件。这就是它变得复杂的地方。页面变成空白!!!数据库中没有显示或输入任何内容。正常查询运行良好,但是当添加用户交互时,就会出现问题。这是 reg.php:
的代码
<?php
$un = $_POST["u"]
$pk = $_POST["p"]
$ok = $_POST["o"]
$u = mysql_real_escape_string($un);
$p = mysql_real_escape_string($pk);
$o = mysql_real_escape_string($ok);
$link = mysql_connect('localhost', 'root', 'randompassword');
if (!$link){
die(' Oops. We Have A Problem Here: ' . mysql_error());
}
if ($link){
echo 'connected succesfully';
}
mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error());
$data = mysql_query("INSERT INTO users (username, password, occupation) VALUES ('{$u}', '{$p}', '{$o}')");
?>
任何人都可以帮助我更正此代码以使其正常工作吗?
非常感谢您的参与。非常感激。
编辑:
我注意到我没有在前 3 行中添加分号。这样做之后我得到了这个错误:"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 '{'', '', '')' at line 1."有人可以解释为什么吗?
编辑:该网站就在我的本地机器上...
在 linux
上的 apache 服务器上
尝试将此添加到脚本的顶部:
error_reporting(E_ALL);
ini_set("display_errors", 1);
这样您将看到您在句法上或什至在您的 SQL 内犯下的所有错误。
您在前三行中缺少分号。
$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];
mysql_real_escape_string()
需要数据库连接。
试试这个....
<?php
$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];
$link = mysql_connect('localhost', 'root', 'randompassword');
if (!$link){
die(' Oops. We Have A Problem Here: ' . mysql_error());
}
if ($link){
echo 'connected succesfully';
}
mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error());
$u = mysql_real_escape_string($un);
$p = mysql_real_escape_string($pk);
$o = mysql_real_escape_string($ok);
$sql = "INSERT INTO users (username, password, occupation) VALUES ('$u', '$p', '$o')";
$ins_sql = mysql_query($sql);
IF($ins_sql) {
echo 'Inserted new record.';
}ELSE{
echo 'Insert Failed.';
}
?>
我正在创建一个用户数据库,其中有 4 个字段:ID、用户名、密码和职业。这是一个测试数据库。我尝试查询数据库 table 并且它有效,但我在用户输入和基于它的 MySQL 查询时遇到了很多麻烦。我在 Linux(Debian,Ubuntu)中 运行 一个 Apache 服务器。
我有 2 页。第一个是简单的测试索引页。这是人们输入简单信息以在数据库中注册他们的信息的文本框。这是它的代码:
<html>
<form action="reg.php" method="POST">
Username:
<input type="text" name="u">Password:
<input type="password" name="p">Occupation:
<input type="text" name="o">
<input type="submit" value="register">
</form>
</html>
点击提交按钮后。它转到 reg.php 文件。这就是它变得复杂的地方。页面变成空白!!!数据库中没有显示或输入任何内容。正常查询运行良好,但是当添加用户交互时,就会出现问题。这是 reg.php:
的代码<?php
$un = $_POST["u"]
$pk = $_POST["p"]
$ok = $_POST["o"]
$u = mysql_real_escape_string($un);
$p = mysql_real_escape_string($pk);
$o = mysql_real_escape_string($ok);
$link = mysql_connect('localhost', 'root', 'randompassword');
if (!$link){
die(' Oops. We Have A Problem Here: ' . mysql_error());
}
if ($link){
echo 'connected succesfully';
}
mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error());
$data = mysql_query("INSERT INTO users (username, password, occupation) VALUES ('{$u}', '{$p}', '{$o}')");
?>
任何人都可以帮助我更正此代码以使其正常工作吗? 非常感谢您的参与。非常感激。
编辑: 我注意到我没有在前 3 行中添加分号。这样做之后我得到了这个错误:"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 '{'', '', '')' at line 1."有人可以解释为什么吗?
编辑:该网站就在我的本地机器上... 在 linux
上的 apache 服务器上尝试将此添加到脚本的顶部:
error_reporting(E_ALL);
ini_set("display_errors", 1);
这样您将看到您在句法上或什至在您的 SQL 内犯下的所有错误。
您在前三行中缺少分号。
$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];
mysql_real_escape_string()
需要数据库连接。
试试这个....
<?php
$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];
$link = mysql_connect('localhost', 'root', 'randompassword');
if (!$link){
die(' Oops. We Have A Problem Here: ' . mysql_error());
}
if ($link){
echo 'connected succesfully';
}
mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error());
$u = mysql_real_escape_string($un);
$p = mysql_real_escape_string($pk);
$o = mysql_real_escape_string($ok);
$sql = "INSERT INTO users (username, password, occupation) VALUES ('$u', '$p', '$o')";
$ins_sql = mysql_query($sql);
IF($ins_sql) {
echo 'Inserted new record.';
}ELSE{
echo 'Insert Failed.';
}
?>