始终显示必填字段的错误消息

时间:2012-04-14 07:28:49

标签: php

我有一个必填字段的错误,但即使它已被填写,它显示我的错误信息,也许我需要一个else语句但不完全确定。

session_start();
// this sets variables in the session
$id = $_SESSION['user_id'];
foreach($_POST as $key => $value)
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$err = array();
$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0'") or 
die  (mysql_error());

if(empty($_SESSION['$user_id']))  // user not logged in; redirect to somewhere else
if (!empty($_POST['doThesis']) && $_POST['doThesis'] == 'Save')
{
    $result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0'") or 
die (mysql_error());


 list($id) = mysql_fetch_row($result);

 if(empty($abstract))
 {
 $err[] = "ERROR - Enter Abstract";
 //header("Location: thesis.php?msg=$err[0]");
 }
 /// Automatically collects the hostname or domain  like example.com)
 $host  = $_SERVER['HTTP_HOST'];
 $host_upper = strtoupper($host);
 $path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

if(empty($err)) {
        for($i = 0; $i < count($_POST["other"]); $i++)
        {
            $thesis_Name = mysql_real_escape_string($_POST['thesis_Name'][$i]);
            $abstract = mysql_real_escape_string($_POST['abstract'][$i]);


$sql_insert = "INSERT into `thesis`
     (`user_id`,`thesis_Name`,`abstract`  )
VALUES
     ('$id','$thesis_Name','$abstract' ";

 mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
        }

 header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
}

} 显然,当数据在框中时,第二个标题就是它应该去的地方。如果我包含第一个标题,则错误消息位于URL栏中,当我不包含标题行时,它在页面上(应该在哪里)帮助!!

我的意思是当我收到错误时,这是​​我设置的错误,“输入摘要”会一直显示,即使输入了某些内容。

1 个答案:

答案 0 :(得分:1)

请更新您的问题以及错误以及您想要实现的目标......因为在查看您的代码之后......很多事情都是错误的

A。在mysql链接上使用foreach

// this sets variables in the session
$id = $_SESSION['user_id'];
foreach($_POST as $key => $value)
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");

B.多个If语句相互矛盾

if(empty($_SESSION['$user_id']))  // user not logged in; redirect to somewhere else
if (!empty($_POST['doThesis']) && $_POST['doThesis'] == 'Save')
{

℃。 {未正确关闭......

d。未使用查询的多个实例

$result = mysql_query ( "SELECT `id` FROM users WHERE `banned` = '0'" ) or die ( mysql_error () );

ERROR - Enter Abstract应该是您的问题列表,因为您的原因是因为您没有declared $abstract ...

编辑1我认为你需要什么

session_start ();
// this sets variables in the session
$id = $_SESSION ['user_id'];
$link = mysql_connect ( DB_HOST, DB_USER, DB_PASS ) or die ( "Couldn't make connection." ); // Database
$errors = array (); // Link

/**
 * Check if user is logged in
 */
if (empty ( $_SESSION ['$user_id'] )) {
    header ( "Location: logout.php?action=expired" ); // Change to what you want
    exit ();
}

if (! empty ( $_POST ['doThesis'] ) && $_POST ['doThesis'] == 'Save') {
    $host = $_SERVER ['HTTP_HOST'];
    $host_upper = strtoupper ( $host );
    $path = rtrim ( dirname ( $_SERVER ['PHP_SELF'] ), '/\\' );

    for($i = 0; $i < count ( $_POST ["other"] ); $i ++) {
        $thesis_Name = mysql_real_escape_string ( $_POST ['thesis_Name'] [$i] );
        $abstract = mysql_real_escape_string ( $_POST ['abstract'] [$i] );

        if (empty ( $abstract )) {

            $errors[] =" Adbstract not set for $thesis_Name" ;
            continue;
        }

        $sql_insert = "INSERT into `thesis`
            (`user_id`,`thesis_Name`,`abstract`  )
            VALUES
            ('$id','$thesis_Name','$abstract' ";

        mysql_query ( $sql_insert, $link ) or die ( "Insertion Failed:" . mysql_error () );
    }


    if(count($errors) == 0)
    {
        header ( "location: myaccount.php?id=" . $_SESSION ['user_id'] . "" );
    }
    else
    {
        print_r($errors); // Change it how you want to display your error 
    }

}

谢谢