在 php/mysql 中简单发布到数据库 - 编辑
很简单的东西,我知道,但出现错误
<?php
session_start();
$dbhost = "###"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "###"; // the name of the database that you are going to use for this project
$dbuser = "###"; // the username that you created, or were given, to access your database
$dbpass = "###"; // the password that you created, or were given, to access your database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
mysql_select_db("###", $con);
$safe_email = mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO register (email) VALUES ('{$safe_email}')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Pretty simple stuff, I know, but am getting an error
<?php
session_start();
$dbhost = "###"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "###"; // the name of the database that you are going to use for this project
$dbuser = "###"; // the username that you created, or were given, to access your database
$dbpass = "###"; // the password that you created, or were given, to access your database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
mysql_select_db("###", $con);
$safe_email = mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO register (email) VALUES ('{$safe_email}')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在查询调用中使用
$con
之前,它未在代码中的任何位置定义。您应该:除此之外,您的代码对 SQL 注入攻击很开放。你应该有:
$con
is not defined anywhere in your code, before you use it in the query call. You should have:Beyond that, your code is WIDE open to SQL injection attacks. You should have:
假设以下情况:
$dbname
的值、$dbhost
、$dbuser
和$dbpass
请尝试以下操作,如果屏幕上出现任何输出,请报告:
Assuming the following:
$dbname
,$dbhost
,$dbuser
and$dbpass
Please try the following and report back if you get any output at all to screen:
这可能只是我的问题,但你应该将字符串放在一行上,或者对字符串使用串联 (.)
除此之外,你应该在将信息添加到数据库之前清理信息,否则你会要求进行 sql 注入攻击。
现在讨论实际问题。您使用了一个未定义的变量 conn。我假设您引用所建立的连接,但您从未像这样将该变量设置为连接
this may just be me but you should but strings on a single line or use the concatenation (.) for the strings
Other than that you should clean the information before adding it into the database otherwise your asking for sql injection attacks.
now to the actual problem. your using a variable that is undefined called conn. i am assuming your referencing the connection made, but you never set that variable to the connection like so