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

本小妞迷上赌 提交于 2019-12-30 07:49:08

问题


i am doing a project where one may update the name, position, department and tag of the employee.

But as i do my project, it wont update, i know there is something wrong with my code. would you guys mind checking it.

my php page has an index.php which is the main menu, if you click the employee name in the list, a pop up window will appear. that pop up is for updating.

my php code (it now updating) but errors found:

<?php
$con=mysql_connect('localhost','root','pss') or die(mysql_error());
mysql_select_db('intra',$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 
        }       
    ?>

*NOTE : this is now updating, but if a user leaves one of the textboxes empty, it updates the table with empty spaces as well and that is my problem now. how do i avoid that? i mean if a user leaves one textbox empty,the data with empty values must still contain its old value,but how to do that with this code? thanks for those who will help

MisaChan

回答1:


You use $_POST for 'name/pos/dep/tag' and $_GET for 'emp' so you're probably not getting the values. Change the GETs to POST - that should do it. Since you're updating, I'd recommend using POST over GET. GET is more appropriate for searching.

Also, you can put all your update queries into one update query. Like so.

$name = $_POST['name'];
$pos = $_POST['pos'];
$dep = $_POST['dep'];
$tag = $_POST['tag'];
$emp = $_POST['emp'];

$qry_start = "UPDATE gpl_employees_list SET ";
$where = " WHERE emp_id = $emp";
$fields = "";
$updates = "";

if($name){
  $updates .= " `emp_name` = $name,";
}
if($pos){
  $updates .= " `emp_pos` = $pos,";
}
if($dep){
  $updates .= " `emp_dep` = $dep,";
}
if($tag){
  $updates .= " `emp_tag` = $tag,";
}
$updates = substr($updates, 0, -1); //To get rid of the trailing comma.
$qry = $qry_start . $updates . $where;



回答2:


this is what i used to keep it working :) i hope this could be a source for others as well :)

$col['emp_nme'] = (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){
if($val){
    $queryString .="`".$key."`='".$val."',";
}
                            }
$queryString = substr($queryString ,0 ,strlen($queryString) - 1 )." WHERE emp_id = '".$_POST['eid']."'"; 
mysql_query($queryString);



回答3:


After making changes to an SQL database, remember to commit those changes, otherwise they'll be ignored.



来源:https://stackoverflow.com/questions/6182253/table-updates-empty-spaces-when-user-do-not-enter-anything-to-the-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!