2010-12-06 102 views
3

我有一個PHP,它會檢查一定的值,如果它存在於MySQL數據庫中。如果該值不存在,它將簡單地添加該值並刷新頁面一次以再次加載頁面,現在它在數據庫中具有值,將繼續添加其他值。當它被調用時,我如何刷新頁面一次?刷新PHP頁面只調用一次

<?php 
$sname = "W3 schools C# tutorials";//$_POST["sitename"]; 
$stype = "C#";//$_POST["sitetype"]; 
$saddy = "www.w3schools.com";//$_POST["siteaddress"]; 
$scomm = "W3 schools C# tutorials";//$_POST["sitecomment"]; 

$conn = mysql_connect("localhost","root","password"); 

if(!$conn){ 
    die("Could not connect: ".mysql_error()); 
} else { 
    mysql_select_db("bookmarks",$conn); 
    $rs = mysql_query("select TypeId from bookmarktypes where TypeName = '$stype'"); 
    $row = mysql_fetch_array($rs); 
    if($row > 0){ 
    //Data found, continue to add... 
    } else { 
    //No data... insert a valid one 
    $rs = mysql_query("insert into bookmarktypes (TypeName) values ('$stype')"); 
    if (!$rs){ 
     die('Error: ' . mysql_error()); 
    } else { 
    //echo "inserted new type data..."; 
    } 
    //echo "</html>"; 
    } 
    } 
    mysql_close($conn); 
    //Refresh page once 
?> 

在mysql關閉命令後有刷新頁面的註釋。

回答

8

刷新它

header('Location: url here'); 
exit; 

插入之後順便說一句,讀了一些關於SQL注入

而且 - mysql_close()是毫無意義的存在。

+0

他應該使用PDO來代替。學習注射也很好,但最好使用PDO參數化查詢。另外,他不應該輸出任何他在重定向時會出錯的東西。 – Alfred 2010-12-06 03:29:42

+1

你應該用`header`添加常見警告:確保在使用之前不輸出任何內容。 +1的SQL注入備忘錄。 – 2010-12-06 03:30:26

+0

謝謝。當函數運行時,我會到達sql注入部分。 – thotheolh 2010-12-06 03:47:58

2
if(check=1) 
{  
echo "\"<meta http-equiv=\"refresh\" content=\"2;url=http://yourwebsite.com/\">\"\n"; 
} 
0

如果您需要打印您剛纔輸入的數據試試這個

header('Location: YourShowDataPage.php?id='.$_POST['id_dataEntered']) 

英里道歉,如果錯了,我是個初學者