您好我正在尝试使用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>
答案 0 :(得分:-1)
以下是您遇到问题的代码。希望这可以帮助。我发表了评论,解释了整个代码中发生的事情,所以如果你想继续阅读它们,那会让你理解得更好一点。希望这会有所帮助。
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
}
}
}
答案 1 :(得分:-2)
编辑此行以获取有关错误的更多信息:
if(!$db) die("Error connecting to MySQL database.");
可以成为:
if(!$db) die("Error connecting to MySQL database : ".mysql_error($db));
崩溃时您会看到更多信息:)
注意:为您的表创建一个用户,最好不要在您的php文件中写入root密码