开发者

table updates empty spaces when user do not enter anything to the textbox

The situation is my program's purpose is to update employee details. The user may choose which of the information (name,position,department and tag(which tells if he/she is still employed to the company)) he/she wants to update. He or she may or may not fill up all the details as he/she wants to. In other words,he/she may leave it blank if he/she is not going to change that certain information.

MY PROBLEM IS the updating. it is now updating. But then for example,if the user wants to change only the position information of that employee,he/she will leave the name,department blank.(tag is by default set to yes so there is always a value). when the user clicks the submit button. it updates the table BUT it updates the empty text box to spaces. meaning, in the example. In the database, the name and department will contain "spaces".

MY GOAL is that,if the user leaves a text box empty,the original value should remain. it will only change the inputed values.


here is the code i used in my empinfo.php (NOTE: this one is a pop up,the emp, came from the index,but there is no problem about that :D )

<body>
<center>
<ta开发者_运维百科ble class="main" border="0" cellpadding="0" cellspacing="0"> 
<form action="empinfo.php" method="POST">
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />  

<fieldset>
            <div class='container'>
                <label for='ename' >Employee name:</label><br/>
                <input type='text' name='ename' id='ename' maxlength="50" /><br/><br/>
             </div>

             <div class='container'>
                <label for='pos' >Position:</label><br/>
                <input type='text' name='pos' id='pos' maxlength="50" /><br/><br/>
             </div>

            <div class='container'>
                <label for='dep' >Department/Division:</label><br/>
                <input type='text' name='dep' id='dep' maxlength="100" /><br/><br/>
            </div>

            <div class='container'>
                <label for='tag' >Employee Tag:</label><br/>
                <select name="tag" id="tag">
                    <option value="Y">Yes</option>
                    <option value="N">No</option>
                </select> <br/><br/>
            </div>

            <div class='container'>
                <input type='submit' name='Submit' value='Submit' />
            </div>
</fieldset>

</form>
</div>
<?php   
$con=mysql_connect('localhost','root','mariel') or die(mysql_error());
mysql_select_db('intranet',$con);
    if(isset($_POST['submitted']))
    {

    $sql = "SELECT * FROM gpl_employees_list where emp_id='".$_POST['eid']."'";
    $result = mysql_query($sql) or die (mysql_error());
    if(!$result || mysql_num_rows($result) <= 0)
                            {                       
                                return false;
                            }

    $qry = "UPDATE gpl_employees_list SET emp_nme = '".$_POST['ename']."', emp_pos = '".$_POST['pos']."', emp_dep = '".$_POST['dep']."', emp_tag = '".$_POST['tag']."' WHERE emp_id = '".$_POST['eid']."' ";  

    mysql_query($qry) or die (mysql_error()); 
?><script>window.close();</script><?php 
        }       
    ?>
</table>
<?php mysql_close($con); ?>
</center>
</body>


$col['emp_name'] = (trim($_POST['ename']))?trim($_POST['ename']):false;
$col['emp_pos']  = (trim($_POST['pos']))?trim($_POST['pos']):false;
$col['emp_dep']  = (trim($_POST['dep']))?trim($_POST['dep']):false;
$col['emp_tag']  = (trim($_POST['tag']))?trim($_POST['tag']):false;
// add a val in $col[] with key=column name for each corresponding $_POST val

$queryString ="UPDATE `gpl_employees_list` SET ";
foreach($col as $key => $val){
  $queryString .= ($val)?"`".$key."`='".$val."',":"";
}
$queryString = substr($queryString ,0 ,strlen($queryString) - 1 )." WHERE `emp_id` = '".$_POST['eid']."'"; 
mysql_query($queryString);


Given the situation, how does your script know whether or not the empty data is intentional (I've decided to delete the contents of the field) v.s. not (as you're trying to achieve)? The easiest option is to skip all the fields that come back blank, however you probably want to design your app a little bit better. It would be preferable to have all those values already set in form fields on the page.

Aside from your question, take a look at securing your script a bit. It's not a good idea to simply use $_POST and $_GET values willy nilly. There are lots of good resources out there for that.


Just make some checks. For example use this:

MoreSQL='';
if($_POST['ename']!='') {
    $MoreSQL.='emp_nme = "'.$_POST['ename'].'"';
}

if($_POST['emp_pos']!='') {
    if($MoreSQL!='') $MoreSQL.=', '; // add comma if $MoreSQL is not empty any more
    $MoreSQL.='emp_pos = "'.$_POST['emp_pos'].'"';
}

if($_POST['emp_dep']!='') {
    if($MoreSQL!='') $MoreSQL.=', '; // add comma if $MoreSQL is not empty any more
    $MoreSQL.='emp_dep= "'.$_POST['emp_dep'].'"';
}

if($_POST['tag']!='') {
    if($MoreSQL!='') $MoreSQL.=', '; // add comma if $MoreSQL is not empty any more
    $MoreSQL.='emp_tag= "'.$_POST['emp_tag'].'"';
}

if($_POST['eid']!='') {
    if($MoreSQL!='') $MoreSQL.=', '; // add comma if $MoreSQL is not empty any more
    $MoreSQL.='emp_id = "'.$_POST['eid'].'"';
}

if($MoreSQL!='') {
    $qry='UPDATE gpl_employees_list SET '.$MoreSQL.' WHERE emp_id = '.($_POST['eid']+0).'';
    mysql_query($qry) or die (mysql_error()); 
}else{
    echo 'nothing to update';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜