使用isset进行修正?

时间:2014-05-22 06:23:17

标签: php

我是PHP的新手,我的网页出错了。它说:

  

注意:未定义的索引:第103行/home/tz005/public_html/COMP1687/edit.php中的itemid

我可以使用isset来解决这个问题吗?如果是,怎么办?这是我的剧本:

<?php
//include database connection
include 'dbconnect.php';

// if the form was submitted/posted, update the item
if($_POST){

    //write query
    $sql = "UPDATE 
                item_information 
            SET
                itemtitle = ?, 
                itemdescription = ?,
                date = ?,
            WHERE 
                itemid= ?";

    $stmt = $mysqli->prepare($sql);

    $stmt->bind_param(
        'sssi', 
        $_POST['itemtitle'], 
        $_POST['itemdescription'],
        $_POST['date'],
        $_POST['itemid']
    );

    // execute the update statement
    if($stmt->execute()){
        echo "Item was updated.";

        // close the prepared statement
        $stmt->close();
    }else{
        die("Unable to update.");
    }
}

$sql = "SELECT 
            itemid, itemtitle, itemdescription, date
        FROM 
            item_information
        WHERE 
            id = \"" . $mysqli->real_escape_string($_GET['itemid']) . "\"
        LIMIT 
            0,1";

// execute the sql query
$result = $mysqli->query( $sql );

//get the result
if ($result = $mysqli->query( $sql )) {
  if ($row = $result->fetch_assoc()) {

    // $row contains data

  }
}

//disconnect from database
$result->free();
$mysqli->close();
?>

2 个答案:

答案 0 :(得分:1)

变化

$mysqli->real_escape_string($_GET['itemid'])

$mysqli->real_escape_string($_POST['itemid'])

或使用empty()isset()检查存在的值

答案 1 :(得分:0)

是的,您可以使用isset()函数

来完成

为它创建条件

if(isset($_GET['itemid'])){
  //execute your code
}
else{
 //header them back to page or show error that itemid not set or something else whatever suits you
}