PHP:未定义显示在空白页面上

时间:2014-01-04 15:39:16

标签: php mysql undefined

我正在尝试打印/回显来自先前文件的值,但不显示错误,所有显示的内容都是空白页面上的“未定义”。我已经研究并尝试了几种方法,但没有任何效果。请帮忙。

<?PHP
$user_name = "root";
$password = "";
$database = "leadership_program";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

 if (isset($_POST['survey_id'])) {
    $survey_id = $_POST['survey_id'];
   echo $survey_id;
}

if (isset($_POST['marks'])) {
    foreach ($_POST['marks'] as $value) {
        echo"$value";
    }        
}

if (isset($_POST['id'])) {
    $id = $_POST['id'];
    echo $id;
}

// $SQL2 = "UPDATE answer_table SET marks='$value' WHERE survey_id= '$survey_id' AND student_id= '$id'";

//$result2 = mysql_query($SQL2);

//mysql_close($db_handle);


 } else {

    print "Database NOT Found ";
    mysql_close($db_handle);
   // header("Location: surveyView.php");
}
?>

这是displayresult.php

<form action="student_mark_save.php" method="POST"> //<?php...more codes here

            if ($strucrow["qns$i"] === 'radio' || $strucrow["qns$i"] === 'checkbox') {

                foreach ($arr as $b) {

                    echo "<br/>";

                    if (strpos($b, '%#%') !== false) {
                        $c = substr($b, 3, -2);
                        //echo $c;
                        $d = str_replace("$arr[0] :-", ':', $c);
                        echo $d, "<br/>";
                        echo "<br/>";

                        echo "<tr> Marks : <input type=\"text\" name=\"marks[]\"></tr><br />";

                        //echo $b;
                    } else {
                        echo $b;
                        // echo "is not with comment qns";
                    }
                }
            } else if ($strucrow["qns$i"] === 'comment') {

                foreach ($arr as $b) {

                    echo $b;

                    echo "<tr> Marks : <input type=\"text\" name=\"marks[]\"></tr><br />";
                    // echo "is not with comment qns";
                }

            } else {

            }
            echo "<p/>";
        }

        $marksquery = sprintf(
                "SELECT marks FROM answer_table WHERE survey_id = '%d' AND student_id = '$studentid' ", mysql_real_escape_string($survey_id)
        );
        $marksQuer = mysql_query($marksquery) or die(mysql_error());

        $marksrow = mysql_fetch_assoc($marksQuer);

         echo "<td><input type=\"hidden\" value= \"$survey_id\" name=\"survey_id\"></td><br />" ;
         echo "<td><input type=\"hidden\" value= \"$studentid\" name=\"id\"></td><br />" ;

         echo "<p><input type=\"submit\" value=\"Update\"></p>";
        ?> 


        </form>

2 个答案:

答案 0 :(得分:0)

对于未定义的平均值,全局$_POST[]中不存在某些内容 检查尝试在您的第一个$_POST语句中打印if

if ($db_found) {

     print_r($_POST);
     exit();      

 if (isset($_POST['survey_id'])) {
    $survey_id = $_POST['survey_id'];
   echo $survey_id;
}

现在只看看你在$_POST中有什么,它可能对你有所帮助......谢谢

答案 1 :(得分:0)

很可能没有设置一些Post数据。试试vardump($_POST); die('happy');

最好处理丢失的Post数据,而不是忽略它,所以if ($db_found) {可能应该

<?php
if (   $db_found 
    && isset($_POST['survey_id']) 
    && isset($_POST['marks'])
    && isset($_POST['id'])
   )
{
    $survey_id = $_POST['survey_id']; //maybe ensure int
    $id = $_POST['id']; //maybe ensure int
    $values = array(); //this is important $value will not be available outside the for loop or contain only the last value
    foreach ((array)$_POST['marks'] as $value) { //ensure $_POST['marks'] is an array
       $values[] = $value;  //maybe mysql escape each $value here
    }

    $SQL2 = sprintf(
        "UPDATE answer_table SET marks='%s' WHERE survey_id=%d AND student_id=%d",
        impolde(',' $values), //assuming comma separated list here
        $survey_id, 
        $id
    );

    $result2 = mysql_query($SQL2);
    mysql_close($db_handle);

} else { ...

还请考虑转义从POST中读取的值。这样做可以很容易地将SQL注入到脚本中!