如何防止插入空文本框(具有数组的文本框)

时间:2017-05-03 11:35:54

标签: php sql

我想只插入输入的文本框值,当我点击提交按钮时,文本框也是空的也插入。如何防止空文本框不被插入。请让我通过这个,我也想要插入多个文本框值单细胞。

 <html>
    <head>
    <title>

    </title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <form method="post" action="updatevalues.php">
    <div> 
       <div class="firstdiv">
        <table >
        <thead>
        <th> Monthly</th>
        <th>Enter values</th>
        </thead>
        <tr>
        <td>F1:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F2:</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F3</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F4</td>
        <td><input type="text" name="monthly[]"></td>
        </tr>
        <tr>
        <td>F5:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F6:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F7:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F8:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F9:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F10:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F11:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F12:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F13:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F14:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        <tr>
        <td>F15:</td>
        <td><input type="text" name="monthly[]"> </td>
        </tr>
        </table>
       </div>
       <div class="seconddiv">
       <table class="table">
       <thead>
        <th>Quaterly</th>
        <th>Enter Values </th>
       </thead>
       <tbody>
        <tr>
        <td>
        Q1:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q2:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q3:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        <tr>
        <td>
        Q4:
        </td>
        <td>
        <input type="text" name="quaterly[]">
        </td>   
        </tr>
        </tbody>
       </table>
       </div>
       <div class="thirddiv">
       <table class="table" >
       <thead>
       <th>
       Annualy
       </th>
       <th>Enter values
       </th>
       </thead>
        <tbody>
            <tr>
            <td>A1</td>
            <td><input type="text" name="annualy[]">
            </tr>
        </tbody>

       </table>
       </div>
       <center><input type="submit" name="submit"></center>
    </div>
    </form>
    </body>
    </html>

updatevalues.php

<?php
$server="localhost";
$user="root";
$pswd="";
$dbname="desig";
$conn=mysql_connect($server,$user,$pswd);
mysql_select_db($dbname,$conn);

if(isset($_POST['monthly'])||isset($_POST['quaterly'])||isset($_POST['annualy']))
{
    $monthly=array();
    $textboxmonthly=trim($_POST['monthly']);
    //for($i=0;$i<sizeof($textboxmonthly);$i++)
    //{
        //$get=array();
        //$get[$i]=$textboxmonthly;
        if(!$textboxmonthly==null)
        {
           $monthly=implode(",",$textboxmonthly);

        }
    //}


    $quaterly=array();
    $textboxquaterly=trim($_POST['quaterly']);
    //for($j=0;$j<sizeof($textboxquaterly);$j++)
    //{
        //$getquaterly=array();
        //$getquaterly[$j]=$textboxquaterly;
        if(!$textboxquaterly==null)
        {
         $quaterly=implode(",",$textboxquaterly);

        }
    //}


    $annualy=array();
    $textboxannualy=trim($_POST['annualy']) ;
    //for($k=0;$k<sizeof($textboxannualy);$k++)
    //{//$getannualy=array();
        //$getannualy[$k]=$textboxannualy;
        if(!$textboxannualy==null)
        {
            $annualy=implode(",",$textboxannualy);
        }
    //}


    echo $monthly;
    echo $quaterly;
    echo $annualy;
    $insert="insert into workupdate(Empname,Designation,Monthly,Quaterly,Annualy)values('user','juniordeveloper','$monthly','$quaterly','$annualy')";
    $get=mysql_query($insert,$conn);

           echo "success";

  }   

 else 
 {
        echo "failed";

        }   


?>

1 个答案:

答案 0 :(得分:0)

使用: -

isset($_POST['monthly'][0])?trim($_POST['monthly'][0]):null;