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

2024-08-22php开发问题
13

本文介绍了当用户未在文本框中输入任何内容时,表格会更新空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

我的php页面有一个index.php,它是主菜单,如果你点击列表中的员工姓名,会出现一个弹出窗口.该弹出窗口用于更新.

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

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

<上一页>陈美沙

解决方案

您将 $_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 = "更新 gpl_employees_list 集";$where = " WHERE emp_id = $emp";$字段 = "";$更新 = "";如果($名称){$updates .= " `emp_name` = $name,";}如果($pos){$updates .= " `emp_pos` = $pos,";}如果($dep){$updates .= " `emp_dep` = $dep,";}如果($标签){$updates .= " `emp_tag` = $tag,";}$updates = substr($updates, 0, -1);//去掉结尾的逗号.$qry = $qry_start .$更新.$哪里;

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

解决方案

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;

这篇关于当用户未在文本框中输入任何内容时,表格会更新空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17