使用php创建一个isset if函数来避免和更新如果MYSQL更新的输入为空

发布于 2024-07-12 01:46:57 字数 1773 浏览 7 评论 0原文

您好,我是 php 新手,我已经为内容管理系统创建了一个更新页面。 我有一个文件上传,在这种情况下是一张图片。 我有其他包含文本的输入,我可以让它们填充我的表单,这很好并且效果很好,因为用户可以看到已经输入的内容。 但是照片的文件名不能有值,因此如果用户没有再次从目录中选择图片,它将更新减去图片。 我认为我需要的是一个isset函数,它表示如果文件(图片)输入留空,则不要更新此字段并使用数据库中已有的任何内容,这样如果创建时留空它仍然会是,如果用户这次改变了它,它也会改变; 或者,如果他们想保持不变,则不会将他们的照片留空。 希望这是有道理的。

这是我当前的表单编码:

 <p>
              Photo:
            </p>
            <input type="hidden" name="MAX_FILE_SIZE" value="350000">
            <input type="file" name="photo"/>

下面是我的更新的 php 代码(如果按下更新按钮):

   $con = mysql_connect("localhost","******","********");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("*******", $con);

  // run this only, once the user has hit the "Update" button

  if (isset($_POST['update'])) {

    // assign form inputs

    $name = $_POST['nameMember'];

    $position = $_POST['bandMember'];

    $pic = $_POST['photo'];

    $about = $_POST['aboutMember'];

    $bands = $_POST['otherBands'];

      // add member to database

    $result = mysql_query("UPDATE dbProfile SET nameMember='".$name."',bandMember='".$position."',photo='".$pic."',aboutMember='".$about."',otherBands='".$bands."' WHERE id='".$id."'");

        mysql_close($con);


      Header("Location: listMember.php"); 

      exit; 

    }


  else { // read member data from database

    $result = mysql_query ("SELECT * FROM dbProfile WHERE id='".$id."'");

    while($row = mysql_fetch_array($result))


{


   $name = $row['nameMember'];

    $position = $row['bandMember'];

    $pic = $row['photo'];

    $about = $row['aboutMember'];

    $bands = $row['otherBands'];

  }
  }
  mysql_close($con);
?>

如果您能提供帮助,我将非常高兴和感激。

Hi I am newish to php and I have created an update page for Content Management System. I have a file upload in this case a picture. I have other inputs that contain text and I can get them to populate my form and thats fine and works great because the user can see what has already been entered. But the file name for the photo can not have a value so if the user doesn't pick the picture from the directory again it will update minus the picture. What I think I need is a isset function that says if the file (picture) input is left blank don't update this field and use whatever what already in the database for it, that way if it was left blank when created it will still be, and if the user has changed it this time it will change; or if they want to leave it the same it won't leave their picture blank. Hope that makes sence.

Here is my coding currently for the Form:

 <p>
              Photo:
            </p>
            <input type="hidden" name="MAX_FILE_SIZE" value="350000">
            <input type="file" name="photo"/>

Below is my php code for my update if the update button is pressed:

   $con = mysql_connect("localhost","******","********");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("*******", $con);

  // run this only, once the user has hit the "Update" button

  if (isset($_POST['update'])) {

    // assign form inputs

    $name = $_POST['nameMember'];

    $position = $_POST['bandMember'];

    $pic = $_POST['photo'];

    $about = $_POST['aboutMember'];

    $bands = $_POST['otherBands'];

      // add member to database

    $result = mysql_query("UPDATE dbProfile SET nameMember='".$name."',bandMember='".$position."',photo='".$pic."',aboutMember='".$about."',otherBands='".$bands."' WHERE id='".$id."'");

        mysql_close($con);


      Header("Location: listMember.php"); 

      exit; 

    }


  else { // read member data from database

    $result = mysql_query ("SELECT * FROM dbProfile WHERE id='".$id."'");

    while($row = mysql_fetch_array($result))


{


   $name = $row['nameMember'];

    $position = $row['bandMember'];

    $pic = $row['photo'];

    $about = $row['aboutMember'];

    $bands = $row['otherBands'];

  }
  }
  mysql_close($con);
?>

If you could help I would be very please and greatful.

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

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

发布评论

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

评论(2

孤凫 2024-07-19 01:46:57

You have to use the $_FILES variable for uploaded files. For further information, see Handling file uploads in the PHP manual.

污味仙女 2024-07-19 01:46:57

尝试:

if(is_uploaded_file($_FILES['photo']['tmp_name'])) 

从手册中:

如果 filename 命名的文件是通过 HTTP POST 上传的,则返回 TRUE。 这有助于确保恶意用户不会试图欺骗脚本来处理它不应该工作的文件,例如 /etc/passwd。

Try:

if(is_uploaded_file($_FILES['photo']['tmp_name'])) 

From the manual:

Returns TRUE if the file named by filename was uploaded via HTTP POST. This is useful to help ensure that a malicious user hasn't tried to trick the script into working on files upon which it should not be working--for instance, /etc/passwd.

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