douan7529 2017-06-27 14:18
浏览 42
已采纳

在php mysql表单中未选中复选框时出错[复制]

The checkbox in my form works to put in a true value (1) when the checked and otherwise, the value for the column is false (0).. however, when I run the form without the checkbox checked it gives me this error -

"Notice: Undefined index: publish in C:\xampp\htdocs\phpoop\create_product.php on line 37"

Here is the Product class for creating the product (only including the publish variable)

class Product{

    // database connection and table name
    private $conn;
    private $table_name = "products";

    // object properties
      public $publish;


    public function __construct($db){
        $this->conn = $db;
    }

    // create product
    function create(){

        //write query
        $query = "INSERT INTO
                    " . $this->table_name . "
                SET
                    publish=:publish";

        $stmt = $this->conn->prepare($query);

        // posted values
        $this->publish=htmlspecialchars(strip_tags($this->publish));

        // to get time-stamp for 'created' field
        $this->timestamp = date('Y-m-d H:i:s');
        $this->timestamp2 = date('Y-m-d H:i:s');

        // bind values 
        $stmt->bindParam(":publish", $this->publish);


        if ($stmt->execute()){
            return true;    

        }else{
            return false;
        }

    }

and this is where the form is located for creating the product

// if the form was submitted 
if ($_POST){

    // set product property values
    $product->publish = $_POST['publish'];

    if(isset($_POST['publish'])){
    $published = $_POST['publish'];
    }
    else{
        $published = 0;
    }

    // create the product
    if($product->create()){
        echo "<div class='alert alert-success'>Product was created.</div>";
    }


}

?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
  <input type="checkbox" name="publish"  value="1" class="form-control" <?php if(isset($_POST['publish'])) echo "checked='checked'";?>/>
  <button type="submit" class="btn btn-primary">Create Product</button>
 </form>
</div>
  • 写回答

1条回答 默认 最新

  • duankanjian4642 2017-06-27 14:22
    关注

    Here you're checking if the value exists before trying to use it:

    if(isset($_POST['publish'])){
        $published = $_POST['publish'];
    }
    else{
        $published = 0;
    }
    

    But here (just before it) you're not:

    $product->publish = $_POST['publish'];
    

    Just include that in the same logic to check if it exists before trying to use it:

    if(isset($_POST['publish'])){
        $published = $_POST['publish'];
        $product->publish = $_POST['publish'];
    }
    else{
        $published = 0;
        $product->publish = 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致