MAMP 无法建立数据库连接
MAMP can't establish database connection
我正在尝试将多页表单中的数据保存到数据库中。我关注了 this tutorial 但连接总是失败。我不得不将功能从 mysql_connect 更改为 mysqli_connect,因为我是 运行 PHP7,所以这可能是问题的一部分。这是代码:
<?php
session_start();
if (isset($_POST['state'])) {
if (!empty($_SESSION['post'])){
if (empty($_POST['address1'])
|| empty($_POST['city'])
|| empty($_POST['pin'])
|| empty($_POST['state'])){
// Setting error for page 3.
$_SESSION['error_page3'] = "Mandatory field(s) are missing, Please fill it again";
header("location: finder-step-3.php"); // Redirecting to third page.
} else {
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']); // Function to extract array.
$connection = mysqli_connect("localhost", "root", "root");
$db = mysqli_select_db($connection, "finder_form"); // Storing values in database.
$query = mysqli_query($db, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
unset($_SESSION['post']); // Destroying session.
}
} else {
header("location: finder-step-1.php"); // Redirecting to first page.
}
} else {
header("location: finder-step-1.php"); // Redirecting to first page.
}
?>
谁能发现我哪里出错了?提前致谢!
更新 1:
@Damon Swayn,我已将其更改为以下但仍然收到表单提交失败消息:
$connection = mysqli_connect("localhost", "root", "root", "finder_form");
$query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
@lps,我在同一目录的 test.php 页面上设置了以下内容并成功连接:
<?php
$con = mysqli_connect('localhost', 'root', 'root') or die('Could not connect the database : Username or password incorrect');
mysqli_select_db($con, 'finder_form') or die ('No database found');
echo 'Database Connected successfully';
?>
更新 2:已解决
Damon Swayn 建议的更改奏效了,我只需要删除查询末尾的 $connection。这是工作代码:
$connection = mysqli_connect("localhost", "root", "root", "finder_form");
$query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')");
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
mysqli_connect()
可以带4个参数,第四个是数据库名。
您正在使用 mysqli_select_db()
的 return 值作为每个后续调用的连接参数,mysqli_select_db()
return 是一个布尔值 true/false,请尝试将 mysqli_select_db()
之后的以下调用中的 $db
参数替换为 $connection
变量。
我正在尝试将多页表单中的数据保存到数据库中。我关注了 this tutorial 但连接总是失败。我不得不将功能从 mysql_connect 更改为 mysqli_connect,因为我是 运行 PHP7,所以这可能是问题的一部分。这是代码:
<?php
session_start();
if (isset($_POST['state'])) {
if (!empty($_SESSION['post'])){
if (empty($_POST['address1'])
|| empty($_POST['city'])
|| empty($_POST['pin'])
|| empty($_POST['state'])){
// Setting error for page 3.
$_SESSION['error_page3'] = "Mandatory field(s) are missing, Please fill it again";
header("location: finder-step-3.php"); // Redirecting to third page.
} else {
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']); // Function to extract array.
$connection = mysqli_connect("localhost", "root", "root");
$db = mysqli_select_db($connection, "finder_form"); // Storing values in database.
$query = mysqli_query($db, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
unset($_SESSION['post']); // Destroying session.
}
} else {
header("location: finder-step-1.php"); // Redirecting to first page.
}
} else {
header("location: finder-step-1.php"); // Redirecting to first page.
}
?>
谁能发现我哪里出错了?提前致谢!
更新 1:
@Damon Swayn,我已将其更改为以下但仍然收到表单提交失败消息:
$connection = mysqli_connect("localhost", "root", "root", "finder_form");
$query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')", $connection);
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
@lps,我在同一目录的 test.php 页面上设置了以下内容并成功连接:
<?php
$con = mysqli_connect('localhost', 'root', 'root') or die('Could not connect the database : Username or password incorrect');
mysqli_select_db($con, 'finder_form') or die ('No database found');
echo 'Database Connected successfully';
?>
更新 2:已解决
Damon Swayn 建议的更改奏效了,我只需要删除查询末尾的 $connection。这是工作代码:
$connection = mysqli_connect("localhost", "root", "root", "finder_form");
$query = mysqli_query($connection, "insert into detail (name,email,contact,password,religion,nationality,gender,qualification,experience,address1,address2,city,pin,state) values('$name','$email','$contact','$password','$religion','$nationality','$gender','$qualification','$experience','$address1','$address2','$city','$pin','$state')");
if ($query) {
echo '<p><span id="success">Form Submitted successfully..!!</span></p>';
} else {
echo '<p><span>Form Submission Failed..!!</span></p>';
}
mysqli_connect()
可以带4个参数,第四个是数据库名。
您正在使用 mysqli_select_db()
的 return 值作为每个后续调用的连接参数,mysqli_select_db()
return 是一个布尔值 true/false,请尝试将 mysqli_select_db()
之后的以下调用中的 $db
参数替换为 $connection
变量。