gpt4 book ai didi

PHP 不发布到 mysql 数据库

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:43 25 4
gpt4 key购买 nike

自从我使用 PHP 以来已经有一段时间了,所以我寻求一些帮助。我给了它一个很好的机会,但无论出于何种原因,我的表格都没有发布到我的数据库中。我点击提交并清除表格。我没有收到任何错误,但数据库仍然是空的。

如有任何帮助或建议,我们将不胜感激。

这是我的 HTML

                <form id="contact-form" method="post"> 

<div>
<label> <span>Name: *</span>
<input type="text" tabindex="1" name="postName" required autofocus />
</label>
</div>
<div>
<label> <span>Email: *</span>
<input type="email" tabindex="2" name="postEmail" required />
</label>
</div>
<div>
<label> <span>Telephone:</span>
<input type="tel" tabindex="3" name="postPhone"/>
</label>
</div>
<div>
<label> <span>Message: *</span>
<textarea placeholder="Include all the details you can" tabindex="5" name="postMessage" required></textarea>
</label>
</div>
<div>
<input name="formSubmit" type="submit" id="contact-submit" value="Submit" />
</div>
</form>

这是我的PHP

<?php
//show all possible errors. should be ALWAYS set to that level
error_reporting(E_ALL);
echo "landed at form handler<br>";

// sometimes buttons not being sent or gets misspelled
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
echo "here goes POST processing<br>";
$host = 'localhost';
$username = 'name';
$pass = 'password';
$dbname = 'dbname';

mysql_connect($host,$username,$pass);
mysql_select_db($dbname);

// all strings should be escaped
// and it should be done after connecting to DB
$name = mysql_real_escape_string($_POST['postName']);
$email = mysql_real_escape_string($_POST['postEmail']);
$phone = mysql_real_escape_string($_POST['postPhone']);
$message = mysql_real_escape_string($_POST['postMessage']);

$query = "INSERT INTO ContactUs
(NAME, EMAIL, PHONE, MESSAGE)
VALUES ('$name','$email','$phone','$message')";

echo $query;
// always run your queries this way to be notified in case of error
$result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
var_dump($result);
}
?>

最佳答案

试试这个。我使用 MySQLi 重写了您的代码并使用了准备好的语句。您可能应该使用 PDO,但就目前而言,这比您目前拥有的要好得多。

$link = new MySQLi('localhost','username','password','database');

if(isset($_POST['postName'],$_POST['postEmail'],$_POST['postPhone'],$_POST['postMessage'])&& $_POST['postName'] !="" && $_POST['postEmail'] !="" && $_POST['postPhone'] !="" && $_POST['message'] !=""){
$name = $_POST['postName'];
$email = $_POST['postEmail'];
$phone = $_POST['postPhone'];
$message = $_POST['postMessage'];
if($query = $link->prepare("INSERT INTO ContactUs (name,email,phone,message) VALUES(?,?,?,?)")){
$query->bind_param('ssss',$name,$email,$phone,$message);
$query->execute();
$query->close();
return true;
}else{
return false;
}
}else{
return false;
}

如果成功,此脚本返回 true。

如果失败则返回 false。

如果任何输入留空,则返回 false。

您可以将它们从 return_true/false 更改为 echo 'Success'; echo 'Failure'; 如果你愿意。

希望对您有所帮助。

关于PHP 不发布到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com