2011-07-15 81 views
0

我是PHP.I新手我得到了這段代碼。如何重定向PHP代碼中的鏈接

<? 
$connection=mysql_connect("","",""); 
if(!$connection) 
{ 
    die("Database Connection Failed: " . mysql_error()); 
} 
//Select a database to use 
$db=mysql_select_db('vss',$connection); 
if(!$db) 
{ 
    die("Database Selection Failed: " . mysql_error()); 
} 

$class=$_POST['class']; 
$section=$_POST['section']; 
$roll=$_POST['roll']; 
mysql_query("INSERT INTO student_class SET 
      class='$class', 
      section='$section', 
      roll='$roll'"); 
print"Data Saved";//If data saved,I want to redirect link to a another page 
mysql_close(); 
?> 

=========================================== =======================================

如果數據保存,我想重定向鏈接到另一個頁面。這可能嗎?

在此先感謝。

回答

0

試試這個:

$qry = mysql_query("INSERT INTO student_class(class, section, roll) VALUES('$class', '$section','$roll')", $connection); 
if($qry){ 
    header("Location: nextPage.php"); 
    exit(); 
}else{ 
    echo "Inserting data fail. <hr />".mysql_error(); 
} 
+0

Thanks.great idea :-) – Adn

0

如果你有

print "Data saved"; 

發送Location頭告訴瀏覽器去一個不同的URL

header("Location: http://www.example.com/"); 

當你進行更改,escape the user input這樣當Little Bobby Tables寄存器,它不會破壞你的數據庫。

0
header('Location: http://www.php.net'); 
+0

請確保沒有HTML內容輸出(例如:回聲「你好」)使頭()調用,或頭(前)將無法正常工作。 – Sparky

0

header("Location: new-url"); 

請注意,這隻會工作,如果你到了頭之前你還沒有發出的輸出。