【发布时间】:2018-03-01 10:56:03
【问题描述】:
我新安装了 Wamp Server,我正在尝试开发一个本地站点。我试图访问的数据库是 Shopping。但是,我在尝试访问它时遇到错误。有人可以帮忙吗?请告诉我这里写得不好? PS:我是 PHP 和 Wamp Server 环境的新手。谢谢!
<?php
$connect = mysqli_connect("localhost", "root", "gold", "shopping") or die
("Please, check the server connection.");
$email_address = $_POST['emailaddress'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$completename = $_POST['complete_name'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$zipcode = $_POST['zipcode'];
$phone_no = $_POST['phone_no'];
$sql = "INSERT INTO customers (email_address, password, complete_name,
address_line1, address_line2, city, state, zipcode, country, cellphone_no)
VALUES ('$email_address',(PASSWORD('$password')), '$completename', '$address1',
'$address2','$city', '$state', '$zipcode', '$country', '$phone_no')";
$result = mysqli_query($connect, $sql) or die(mysql_error());
if ($result)
{
?>
<p>
Dear, <?php echo $completename; ?> your account is successfully created
<?php
}
else
{
echo "Some error occurred. Please use different email address";
}
?>
【问题讨论】:
-
mysql_error()应该是mysqli_error()。您不能混合库。这可能会阻止您获得正确的错误报告。无论如何,您需要准确地告诉我们错误的内容。 “我遇到错误”对任何人都没有用。发生错误的原因有很多。 -
另外,这不是您的问题的原因,而是一个单独的问题:您的代码容易受到 SQL 注入攻击。您应该使用参数化查询和准备好的语句来帮助防止攻击者通过使用恶意输入值来破坏您的数据库。 bobby-tables.com 给出了风险解释,以及如何使用 PHP / mysqli 安全地编写查询的一些示例。切勿将未经处理的数据直接插入 SQL。
-
warning access denied for user 'root'@'localhost' 表示这是您的 MySQL 用户的权限问题,请检查
mysql数据库 - 特别是db和user表查看您的权限是否正常。您可能还想查看用于存储用户密码的 PHP 的 password_hash() 函数……一旦您纠正了其他 SQL 注入问题。 -
首先修复了@ADyson 所说的myql_error,我尝试执行相同的文件代码,在我的服务器上工作正常,检查您的数据库密码并检查您是否创建了相同的数据库和表名。跨度>
-
@ADyson 错误提示:警告:mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\wamp64\www \addcustomer.php 在第 2 行。我按照您的建议将 mysql_error() 更正为 mysqli_error() 但仍未解决。
标签: php mysql apache phpmyadmin wamp