如何使用现有的选择查询动态定义索引?

时间:2015-10-27 20:43:34

标签: php mysql

我正在尝试使用IsNull()返回当前变量值,但我收到一个未定义的Index error。问题是查询返回完整的array

如何分配特定的memberID以便IsNull()将数据库中已有的信息返回到查询中,从而阻止查询删除当前的字段数据。

if(!empty($_POST)) {

    switch (true) {
        //Removed other case for brevity

        case isset($_POST['update']):
            if($_POST['name'] != "") {
                $name = trim($_POST['name']);
        } 
            else {
                $name = NULL;
        }

        if($_POST['designation'] != "") {
                $designation = trim($_POST['designation']);
        } 
            else {
                $designation = NULL;
        }

        if($_POST['rank'] != "") {
                $rank = trim($_POST['rank']);
        } 
            else {
                $rank = NULL;
        }

        if($_POST['currentScore'] != "") {
                $currentScore = trim($_POST['currentScore']);
        } 
            else {
                $currentScore = NULL;
        }
            if(!is_null($name) || !is_null($designation) || !is_null($rank) || !is_null($currentScore)) {
            $update = $conn->prepare("UPDATE members SET name = IsNull(?, name), designation = IsNull(?, designation), rank = IsNull(?, rank), currentScore = IsNull(?, currentScore)  WHERE memberID = ? ");//Prepared statements are immune to sql injection, so they are used for security
            $update->bind_param('ssiii', $name, $designation, $rank, $currentScore, $memberID);// Binding the values to use in the prepared statement
                if($update->execute()) {
                        header('location: index.php');
                        die();
                    } 
            }
        break;

//      case isset($_POST['delete']):
//          // Delete statement goes here
//      break;
    }
}
if($results = $conn->query("SELECT *, ((previousScore + currentScore) / 2) AS avgScore FROM members")) {
FROM members")) {
    if($results->num_rows) {
        while($row = $results->fetch_object()) {
            $records[] = $row; //Appending value to array
        }
        $results->free();
    }
}

0 个答案:

没有答案