连接到我的数据库时出错
Error connecting to my database
您好,我正在尝试使用 phpmyadmin 并连接到我的数据库。我以前做过一次,但我从未对文本框应用样式,所以我不知道它是否正确。出于某种原因,我收到一条错误消息,我告诉它是否无法连接。我不明白为什么它没有连接。我正在使用 MAMP 服务器端口:PHP 5.6.21
这是我的 PHP 代码:
<?php
if($_POST['formSubmit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>You need to enter your name</li>";
}
if(empty($_POST['formEmail'])) {
$errorMessage .= "<li>You need to enter your email</li>";
}
if(empty($_POST['formSubject'])) {
$errorMessage .= "<li>Please enter a subject.</li>";
}
if(empty($_POST['formComment'])) {
$errorMessage .= "<li>Please enter your question.</li>";
}
$varname = $_POST['formName'];
$varemail = $_POST['formEmail'];
$varsubject = $_POST['formSubject'];
$varcomment = $_POST['formComment'];
if(empty($errorMessage)) {
$db = mysql_connect("localhost","root","root");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("three_cats_database" ,$db);
$sql = "INSERT INTO contact_form (name, email, subject, comment) VALUES (".
PrepSQL($varname) . ", " .
PrepSQL($varemail) . ", " .
PrepSQL($varsubject) . "," .
PrepSQL($varcomment) . ") ";
mysql_query($sql);
header("Location: thankyou.php");
exit();
}
}
// function: PrepSQL()
// use stripslashes and mysql_real_escape_string PHP functions
// to sanitize a string for use in an SQL query
//
// also puts single quotes around the string
//
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
这是我的 HTML:
<p>Please fill out this form completely to contact us with any concerns or suggestions.</p><br>
<div class="imgbg"><div class="img">
<!-- FORM IS HERE -->
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="contact-form margin-top">
<label for='formName'><span>Name:</span>
<input type="text" class="input_text" name="formName" id="name" maxlength="50" value="<?=$varname;?>"/>
</label>
<label for='formEmail'><span>Email:</span>
<input type="text" class="input_text" name="formEmail" id="email" maxlength="50" value="<?=$varemail;?>"/>
</label>
<label for='formSubject'><span>Subject:</span>
<input type="text" class="input_text" name="formSubject" id="subject" maxlength="50" value="<?=$varsubject;?>"/>
</label>
<label><span>Comment</span>
<textarea class="message" name="formComment" id="feedback"><?php echo htmlspecialchars($varcomment);?></textarea>
</label>
<input type="submit" class="button" name="formSubmit" value="Submit" />
</label>
</div>
</form>
最后一张我的数据库设置图片用于姓名证明
编辑此行以获取有关您的错误的更多信息:
if(!$db) die("Error connecting to MySQL database.");
可以变成:
if(!$db) die("Error connecting to MySQL database : ".mysql_error($db));
崩溃时你会看到更多信息:)
注意:为您的 table 创建一个用户,最好不要在您的 php 文件中写入 root 密码
这是您遇到问题的代码。希望这可以帮助。我做了评论,解释了整个代码中发生的事情,所以如果你想继续阅读它们,那会让你理解得更好一点。希望这有帮助。
if($_POST['formSubmit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>You need to enter your name</li>";
}
if(empty($_POST['formEmail'])) {
$errorMessage .= "<li>You need to enter your email</li>";
}
if(empty($_POST['formSubject'])) {
$errorMessage .= "<li>Please enter a subject.</li>";
}
if(empty($_POST['formComment'])) {
$errorMessage .= "<li>Please enter your question.</li>";
}
$varname = $_POST['formName'];
$varemail = $_POST['formEmail'];
$varsubject = $_POST['formSubject'];
$varcomment = $_POST['formComment'];
if(empty($errorMessage)) {
try
{
// I used PDO because I prefer it over mysqli
$db = new PDO('mysql:host=localhost:3306;dbname=three_cats_database, root, root');
}
catch(PDOException $e)
{
die('Unable to connect to database');
}
// Here is the sql statement that you provided.
$sql = "INSERT INTO contact_form (name, email, subject, comment)
VALUES (?,?,?,?)";
// Prepare helps with sql injections ect. It is still not completely hacker proof.
$stmt = $db->prepare($sql);
// Each question mark (?) means they increment from 1-4 in this case.
$stmt->bindValue(1, $varname, PDO::PARAM_STR);
$stmt->bindValue(2, $varemail, PDO::PARAM_STR);
$stmt->bindValue(3, $varsubject, PDO::PARAM_STR);
$stmt->bindValue(4, $varcomment, PDO::PARAM_STR);
//If statement to check if the sql command went through.This is also good for error checking.
if($stmt->execute())
{
header("Location: thankyou.php");
}
else
{
// There was an error in the statement
}
}
}
您好,我正在尝试使用 phpmyadmin 并连接到我的数据库。我以前做过一次,但我从未对文本框应用样式,所以我不知道它是否正确。出于某种原因,我收到一条错误消息,我告诉它是否无法连接。我不明白为什么它没有连接。我正在使用 MAMP 服务器端口:PHP 5.6.21
这是我的 PHP 代码:
<?php
if($_POST['formSubmit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>You need to enter your name</li>";
}
if(empty($_POST['formEmail'])) {
$errorMessage .= "<li>You need to enter your email</li>";
}
if(empty($_POST['formSubject'])) {
$errorMessage .= "<li>Please enter a subject.</li>";
}
if(empty($_POST['formComment'])) {
$errorMessage .= "<li>Please enter your question.</li>";
}
$varname = $_POST['formName'];
$varemail = $_POST['formEmail'];
$varsubject = $_POST['formSubject'];
$varcomment = $_POST['formComment'];
if(empty($errorMessage)) {
$db = mysql_connect("localhost","root","root");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("three_cats_database" ,$db);
$sql = "INSERT INTO contact_form (name, email, subject, comment) VALUES (".
PrepSQL($varname) . ", " .
PrepSQL($varemail) . ", " .
PrepSQL($varsubject) . "," .
PrepSQL($varcomment) . ") ";
mysql_query($sql);
header("Location: thankyou.php");
exit();
}
}
// function: PrepSQL()
// use stripslashes and mysql_real_escape_string PHP functions
// to sanitize a string for use in an SQL query
//
// also puts single quotes around the string
//
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
这是我的 HTML:
<p>Please fill out this form completely to contact us with any concerns or suggestions.</p><br>
<div class="imgbg"><div class="img">
<!-- FORM IS HERE -->
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="contact-form margin-top">
<label for='formName'><span>Name:</span>
<input type="text" class="input_text" name="formName" id="name" maxlength="50" value="<?=$varname;?>"/>
</label>
<label for='formEmail'><span>Email:</span>
<input type="text" class="input_text" name="formEmail" id="email" maxlength="50" value="<?=$varemail;?>"/>
</label>
<label for='formSubject'><span>Subject:</span>
<input type="text" class="input_text" name="formSubject" id="subject" maxlength="50" value="<?=$varsubject;?>"/>
</label>
<label><span>Comment</span>
<textarea class="message" name="formComment" id="feedback"><?php echo htmlspecialchars($varcomment);?></textarea>
</label>
<input type="submit" class="button" name="formSubmit" value="Submit" />
</label>
</div>
</form>
最后一张我的数据库设置图片用于姓名证明
编辑此行以获取有关您的错误的更多信息:
if(!$db) die("Error connecting to MySQL database.");
可以变成:
if(!$db) die("Error connecting to MySQL database : ".mysql_error($db));
崩溃时你会看到更多信息:)
注意:为您的 table 创建一个用户,最好不要在您的 php 文件中写入 root 密码
这是您遇到问题的代码。希望这可以帮助。我做了评论,解释了整个代码中发生的事情,所以如果你想继续阅读它们,那会让你理解得更好一点。希望这有帮助。
if($_POST['formSubmit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>You need to enter your name</li>";
}
if(empty($_POST['formEmail'])) {
$errorMessage .= "<li>You need to enter your email</li>";
}
if(empty($_POST['formSubject'])) {
$errorMessage .= "<li>Please enter a subject.</li>";
}
if(empty($_POST['formComment'])) {
$errorMessage .= "<li>Please enter your question.</li>";
}
$varname = $_POST['formName'];
$varemail = $_POST['formEmail'];
$varsubject = $_POST['formSubject'];
$varcomment = $_POST['formComment'];
if(empty($errorMessage)) {
try
{
// I used PDO because I prefer it over mysqli
$db = new PDO('mysql:host=localhost:3306;dbname=three_cats_database, root, root');
}
catch(PDOException $e)
{
die('Unable to connect to database');
}
// Here is the sql statement that you provided.
$sql = "INSERT INTO contact_form (name, email, subject, comment)
VALUES (?,?,?,?)";
// Prepare helps with sql injections ect. It is still not completely hacker proof.
$stmt = $db->prepare($sql);
// Each question mark (?) means they increment from 1-4 in this case.
$stmt->bindValue(1, $varname, PDO::PARAM_STR);
$stmt->bindValue(2, $varemail, PDO::PARAM_STR);
$stmt->bindValue(3, $varsubject, PDO::PARAM_STR);
$stmt->bindValue(4, $varcomment, PDO::PARAM_STR);
//If statement to check if the sql command went through.This is also good for error checking.
if($stmt->execute())
{
header("Location: thankyou.php");
}
else
{
// There was an error in the statement
}
}
}