2011-07-15 80 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"); 

请注意,这只会工作,如果你到了头之前你还没有发出的输出。