0

我的必填字段有错误,但即使已填写,它也会显示我的错误消息,也许我需要一个 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 栏中,当我不包含标题行时,它在页面上(应该在哪里)帮助!

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

4

1 回答 1

1

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

一个 。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.");

乙。多个相互矛盾的 If 语句

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

C.{ 未正确关闭...

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 
    }

}

谢谢

于 2012-04-14T07:52:56.887 回答