doucheng7534
doucheng7534
2018-04-08 17:07

如何使用jQ以多用途形式回显db值?

已采纳

I have a form that appears in a popup when the user presses Upload. It uses the upload.php file.
The form contains title, a photo file input, a description and a category. The form also uploads the submitter's id to my db.
I want the same form to popup when the user presses Edit in one of his own uploaded photos, but with the fields populated with data from the relevant row, probably using a lookup.php file.

I looked up how to do it, but results assume the form does not perform any other function other than being populated with existing data. Any ideas how I can perform what I want?

I do have the code, but first I'd like to understand the concept.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doukuipai8544 doukuipai8544 3年前

    At simplest, your "upload" script needs to optionally accept an upload id. A typical way of handling this would be either in a get param or a hidden form element. Get is more appropriate and simpler for this use case in my opinion. So when you click edit, the edit button should already be prepopulated with:

    upload.php?id=38
    

    In your upload script, at the top, you will check to see if

    if (isset($_GET['id'])) {
       // Query database for picture
       // If found check if this user owns or is permitted to edit this photo
    
    }
    

    At this point, if you've successfully found the photo, you can pre-populate the form fields you want to allow them to change. Again, the only concern is tampering. In this case, I would probably use a hidden input with the upload id, but check that the user owns that upload before you allow them to change it.

    The portion that saves the script simply needs to be altered so that if you submit the form with an existing upload id, your database SQL peforms an UPDATE rather than a INSERT.

    点赞 评论 复制链接分享

为你推荐