简体   繁体   中英

UPDATE using html form (edit button) php/mysql

I'm trying to figure out how to UPDATE a post in my db using html form. I have a page with a form using INSERT that successfully uploads data to my db. I also have another page presenting that data using SELECT. On this page I would like to have an Edit button, sending me to a similar page with the original form but this with the data from the db already inserted into the form, for easy editing and updating. Basically I don't know where to start and I need your help! It's an album database btw.

form page

<form method="post" action="spara2.php" name="std">

    <label>Cover:</label><br>
    <input id="file" type="file" name="file"><br>

    <label>Title:</label><br>
    <input type="text" name="std_title" size="58"/><br>
    <label>Artist:</label><br>
    <input type="text" name="std_artist" size="58"/><br>
    <label for="select"><br>
    Year:</label><br>
    <select name="std_year">

<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select><br>

    <label>Description:</label><br>
    <textarea name="std_beskriv" cols="45" rows="7">
    </textarea><br>
    <label>Tracks:</label><br>
    <textarea name="std_lista" cols="45" rows="7">
    </textarea><br>
    <label>Spotify HTTP Link:</label><br>
    <input type="text" name="std_spotify" size="58"/><br>
    <br>
<label for="reset"></label><input type="reset" value="Reset"/>
<label for="save"></label><input type="submit" name="std_save" value="Save"/>
</form>

My db is called album and the table musik. I want a similar page but with the data from the db inserted into it. My table has:

  1. id
  2. title
  3. artist
  4. year
  5. beskriv
  6. tracks
  7. spotify
  8. date

Since there's a lot of posts in the db how can I select specific entry's so that I can edit one album at a time?

Hope you can understand what i'm after.

Thanks!

You should use $_GET to get a specific album from the DB, and use the attributes as default values in the form. Here's an example.

Album1 Edit
Album2 Edit
Album3 Edit
Album4 Edit
Album5 Edit
Album6 Edit

When you retrieve all the albums from db, you should use the unique id of each album to create a unique path to update a specific album. So the Edit button should have a link of this form: <a href="edit.php?albumID=<?php echo $album[id];?>"> So after writing this, you will have for the first album edit link: edit.php?albumID=1

To create the edit.php page, first you should fetch from the DB the album by specifiying the ID passed by the $_GET['edit'] parameter in the url. So this will look like:

$albumid = mysql_real_escape_string($_GET['edit']);
$sql = "SELECT * FROM musik where albumid = $albumid";

If the record was not found, redirect to a 404 page. Otherwise, echo the attributes in the corresponding inputs. Ex:

<input type="text" name="std_artist" value="<?php echo $album['std_artist'];?>" size="58"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM