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 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变
  • ¥100 如何帮我写一个java小游戏