2015-10-04 141 views
-1

正在制作产品登记表。我试图在运行查询之前检查空白表单或相同的产品代码。问题是,当我运行该页面。甚至当我填写表格的inputNamaProduk我离开了其他空白,我得到的$message空白inputNamaProdukif else not in php [嵌套]

if(strlen($_POST['inputNamaProduk'])>=0) 
    { 
     $form = true; 
     $message = '<p>Sila Isi Nama Produk.</p>'; 

    } 
else 
    { 
     if(strlen($_POST['inputSpesifikasi'])>=0) 
      { 
       $form = true; 
       $message = '<p>Sila Isi Spesifikasi Produk.</p>'; 
      } 

     else 

      { 
       if ($dn==0) 
       $query = mysql_query("INSERT INTO `produk2` 
       (product_code,product_name,product_desc,product_type,price,product_img,product_img_name) 
       VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')"); 

       else 
        { 
         $form = true; 
         $message = '<p>Sila Pilih Kod Produk Lain.</p>'; 
        } 
       } 
    } 

回答

1

为什么你有一个> = 0这基本上意味着,如果strlen的($ _ POST [“inputNamaProduk”] )> 0(如果你在这个领域输入的东西,那么它会告诉你的消息)Sila Isi Nama产品。

试试这个

if(strlen($_POST['inputNamaProduk']) < 1) 
    { 
     $form = true; 
     $message = '<p>Sila Isi Nama Produk.</p>'; 

    } 
else 
    { 
     if(strlen($_POST['inputSpesifikasi'])>=0) 
      { 
       $form = true; 
       $message = '<p>Sila Isi Spesifikasi Produk.</p>'; 
      } 

     else 

      { 
       if ($dn==0) 
       $query = mysql_query("INSERT INTO `produk2` 
       (product_code,product_name,product_desc,product_type,price,product_img,product_img_name) 
       VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')"); 

       else 
        { 
         $form = true; 
         $message = '<p>Sila Pilih Kod Produk Lain.</p>'; 
        } 
       } 
    } 
0

strlen($_POST['inputNamaProduk']) >= 0总是正确的。

因为这意味着>不止和<不到。

它应该像下面,

if (strlen($_POST['inputNamaProduk']) < 1) { 
    $form = true; 
} 

您还可以使用empty检查输入字段是否为空或低于不喜欢。

if (empty($_POST['inputNamaProduk'])) { 
    $form = true; 
    $message = '<p>Sila Isi Nama Produk.</p>'; 
} 
else { 
    if (empty($_POST['inputSpesifikasi'])) { 
     $form = true; 
     $message = '<p>Sila Isi Spesifikasi Produk.</p>'; 
    } 
    else { 
     if ($dn == 0) $query = mysql_query("INSERT INTO `produk2` 
        VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')"); 
     else { 
      $form = true; 
      $message = '<p>Sila Pilih Kod Produk Lain.</p>'; 
     } 
    } 
}