gpt4 book ai didi

php - 控制应该在使用 php 的 if 语句中退出时转到其他程序

转载 作者:可可西里 更新时间:2023-10-31 23:33:57 26 4
gpt4 key购买 nike

我有 2 个程序 welcome.php 和 form.jsp。我很想通过用户表单使用 odbc 将值插入 SQL 数据库。我使用 form.jsp 创建了一个用户窗体。使用 welcome.php 插入行。 while insertion 不是插入重复值,而是检查 while 循环中的条件。当 if 语句起作用时,由于 exit() 语句,它完全从程序中退出。执行退出后得到空白页。但我需要返回到用户表单,或者控件应该转到 form.jsp,这样我应该再次让用户表单输入详细信息而不给出空白页。我该怎么做?

        $query1="select * from company";
$result1 = odbc_exec($connect, $query1);

while(odbc_fetch_row($result1)){
$compname[$index] = odbc_result($result1, 1);
$empname[$index] = odbc_result($result1, 2);

if($compname[$index]==$_POST['cname'])
{
echo "<script> alert(\"compname Exists\") </script>";
exit();
//exit("<script> alert(\"compname Exists\") </script>");
}
if($empname[$index]==$_POST['ename'])
{
echo "<script> alert(\"empname Exists\") </script>";
exit();
}
}
$query=("INSERT INTO dbo.urllink(cname,ename) VALUES ('$_POST[cname]','$_POST[ename]') ");
$result = odbc_exec($connect, $query);
echo "<script> alert(\"Row Inserted\") </script>";

?>

最佳答案

使用 exit() 停止程序的处理。要将用户从 PHP 转到另一个页面,您可以使用 header() 函数,并使用 'Location: ' 参数:

header('Location: form.jsp');

请注意,您不能输出信息(例如使用echo)然后使用header 功能。所有输出都需要由您将它们发送到的页面完成。

如果你想传递信息,比如提交的字段存在,你可以使用查询字符串参数发送,例如:

header('Location: form.jsp?error=abc');

然后您的 JSP 页面将根据 abc 的含义显示错误。

或者,您可以使用 session 将更复杂的信息传递到另一个页面。您可以在 session 上找到更多信息 here .

关于php - 控制应该在使用 php 的 if 语句中退出时转到其他程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426354/

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