2015-04-28 34 views
0

好吧,它正在工作......現在它突然停止了。我不知道爲什麼。我添加的唯一東西是刪除功能..之後,它不再提交。我雖然可以刪除條目= 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注入......但我不知道到底它是什麼或它看起來像什麼。當我進一步進入我的學校項目時,我一定會在後面添加它們。目前擔心的功能..

不知道需要添加些什麼......我要添加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>"; 

} 



?> 

我的管理員登錄的作品,當在管理日誌它將它們帶到一個頁面,這將允許他們查看條目並刪除對數據庫的條目。目前有兩個,但是當我嘗試添加更多的請求....他們不去數據庫。單擊提交時不會發生錯誤。

+2

請,[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。瞭解[準備的陳述](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://jayblanchard.net/demystifying_php_pdo.html)。 服務器升級了嗎? –

+0

在打開'<?php'標記'error_reporting(E_ALL)後立即在文件頂部添加錯誤報告; ini_set('display_errors',1);' 向查詢添加錯誤檢查,例如'或die(mysql_error())'。您會在當前的錯誤日誌中找到問題。 –

+1

'(「localhost」,「dxh6110」,「tcqfoz7」,「dxh6110」)'不符合您的想法。另外,'$ _GET ['del']'和'?deleted = User'檢查。這些是我突出的兩件事。 –

回答

5

首先,("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這是最好的。