gpt4 book ai didi

php - 表单停止提交到数据库

转载 作者:行者123 更新时间:2023-12-01 00:42:06 24 4
gpt4 key购买 nike

好吧,它之前工作......现在突然停止了。我不知道为什么。我唯一添加的是删除功能.. 之后它不再提交。我可以删除条目 =D

表单的php代码

 <?php

if (isset($_POST['submit'])){

$con = mysql_connect("localhost", "", "");
if (!$con){
die("Cannot connect:" . mysql_error());
}

$Firstname = $_POST['Firstname'];
$Email = $_POST['Email'];
$Prayer = $_POST['Prayer'];



//if there is no input these messages will come up//
if($Firstname==''){
echo "<script>alert('Please enter your name!')</script>";
exit();
}
if($Email==''){
echo "<script>alert('Please enter your email!')</script>";
exit();
}
if($Prayer==''){
echo "<script>alert('Please enter your prayer request!')</script>";
exit();
}


mysql_select_db("dxh6110",$con);

//if everything is good, information will be submitted to database
$sql = "INSERT INTO ChurchPrayer (Firstname, Email, Prayer) VALUES('$_POST[Firstname]','$_POST[Email]','$_POST[Prayer]')";



if(mysql_query($sql,$con)){

echo "<script>alert('Congratulations, You have successfully submitted your prayer requests. You will hear from us very soon!')</script>";


}

mysql_close($con);
}

?>

哦,我知道我应该使用准备好的语句来防止 SQL 注入(inject)...但我不确定它到底是什么或它看起来像什么。当我进一步进入我的学校项目时,我一定会在稍后添加它们。目前担心功能..

不确定还需要添加什么...我将添加我的 delete.php

<?php session_start(); //starting the session?>
<?php
//connecting to database
$con = mysql_connect("localhost","","","dxh6110");

//defining variable
$delete_id = $_GET['del'];
//command to remove input from SQL DB
$query = "delete from ChurchPrayer where id='$delete_id'";

if(mysql_query($con,$query)){

echo "<script>window.open('view_prayers.php?deleted=User has been deleted!','_self')</script>";

}



?>

我的管理员登录有效,当管理员登录时,它会将他们带到一个页面,该页面将允许他们查看条目和删除对数据库所做的条目。目前有两个,但是当我尝试添加更多请求时……它们不会转到数据库。单击提交时不会出现错误。

最佳答案

首先,("localhost","xxx","xxx","xxx") 并没有按照您的想法行事。

mysql_connect() 有 3 个参数,而不是 4 个。第四个用于其他用途。 mysqli_connect() 将使用四个参数,但这些不同的 MySQL API 不会相互混合,因此如果您要使用 mysql_ 函数,请不要使用该连接方法。

咨询:

按照您在 your other question 中所做的那样做:

$con = mysql_connect("localhost", "xxx", "xxx");
if (!$con){
die("Cannot connect:" . mysql_error());
}

mysql_select_db("your_db",$con);

然后这个 if(mysql_query($con,$query)){ 连接排在第二位。

另外,$_GET['del']?deleted=User 检查它。这两件事对我来说很突出。

如果您的删除链接是 ?deleted=XXX,则它需要是 ?del=XXX - 例如 XXX。

$_GET['del'] 需要匹配您方法中 ?parameter 中的参数。

即:view_prayers.php?del=XXX 如果 view_prayers.php 是您用来删除的文件。


另外,如评论中所述,此方法不安全。

最好使用mysqli with prepared statements , 或 PDO with prepared statements .

关于php - 表单停止提交到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925348/

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