当用户未在文本框中输入任何内容时,表会更新空白空间

发布于 2024-11-10 10:22:49 字数 1182 浏览 2 评论 0原文

我正在做一个项目,可以更新员工的姓名、职位、部门和标签。

但是当我做我的项目时,它不会更新,我知道我的代码有问题。你们介意检查一下吗?

我的php页面有一个index.php,它是主菜单,如果您单击列表中的员工姓名,将会出现一个弹出窗口。那个弹出窗口是为了更新。

我的 php 代码(现在正在更新)但发现错误:

<?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 
        }       
    ?>

*注意:这现在正在更新,但是如果用户将其中一个文本框留空,它也会用空格更新表格,这就是我现在的问题。我该如何避免这种情况?我的意思是,如果用户将一个文本框留空,则具有空值的数据必须仍包含其旧值,但如何使用此代码执行此操作?感谢那些愿意提供帮助的人

MisaChan

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2024-11-17 10:22:49

您使用 $_POST 表示“name/pos/dep/tag”,使用 $_GET 表示“emp”,因此您可能无法获取这些值。
将 GET 更改为 POST - 这样就可以了。
由于您正在更新,我建议使用 POST 而不是 GET。
GET 更适合搜索。

此外,您可以将所有更新查询放入一个更新查询中。
就像这样。

$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;

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;
深爱不及久伴 2024-11-17 10:22:49

这就是我用来保持它正常工作的方法:)我希望这也可以成为其他人的来源:)

$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);

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);
离旧人 2024-11-17 10:22:49

对 SQL 数据库进行更改后,请记住提交这些更改,否则它们将被忽略。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文