简体   繁体   中英

Using form to update completed database entries?

I have a form that has drop down boxes in it as well as text fields and a checkbox. What I am trying to do is make it so that when a user goes on this page and the address bar displays /updatephoneform.php?id=1, the form displays the information for id1 (PhoneID in my database) and so it can be edited and updated. How would I modify my form to do this? The variables and form are below:

Variables

$ManufacturerID = $row->ManufacturerID;
$Model = $row->Model;
$OSID = $row->OSID;
$Description = $row->Description;
$ScreenSize = $row->ScreenSize;
$Price = $row->Price;
$StockQuantity = $row->StockQuantity;
$StorageSize = $row->StorageSize;
$thumb= $row->ThumbImg;
$large= $row->LargeImg;

Form

<h1>Update an existing phone</h1>
<form action="updatephone.php" method="post" enctype="multipart/form-data">
<p>Brand: <select name="ManufacturerID">
<option value="1">Apple</option>
<option value="2">LG</option>
<option value="3">BlackBerry</option>
<option value="4">HTC</option>
<option value="5">Nokia</option>
<option value="6">Samsung</option>
<option value="7">Sony</option>
<option value="8">Huawei</option>
<option value="9">Motorola</option>
</select>

<p>Model: <input type="text" name="Model"></p>
<p>Operating System: <select name="OSID">
<option value="1">iOS</option>
<option value="2">Android</option>
<option value="3">BlackBerry</option>
<option value="4">Windows Phone 8</option></p>
</select>

<p>Description: <input type="text" name="Description"></p>
<p>Screen Size (inches): <input type="text" name="ScreenSize"></p>
<p>Price: &pound;<input type="text" name="Price"></p>
<p>Stock Quantity: <input type="text" name="StockQuantity"></p>
<p>Storage Size: <input type="text" name="StorageSize"></p>
<p>Large Image(340x340):<input type="text" name="LargeImg"></p>
<p>Thumbnail(100x100):<input type="text" name="ThumbImg"></p>
<p>Flagged?: <input type="checkbox" name="IsFlagged"></p>
<input type="submit" value"Submit">
</form>

Thank you.

EDIT : I got the form to pull up the values in the form of the phone I wanted to edit, which is good, but I want the 'Large Image(340x340)' and 'Thubmnail(100x100)' text fields in the form to display the filename of the image of the phone (If there is one). The images are uploaded to the server and the filename (ie iphone5.jpg) of both the large image and thumbnail are inserted into fields called LargeImg and ThumbImg. How can I get it to do this?

Also, when I went to edit something and submit the form, I got the error :

Notice: Undefined variable: PhoneID on line 51 Below are lines 32 to 51, with line 51 being the '$sql =' part

$ManufacturerID = $_POST['ManufacturerID'];
$Model = $_POST['Model'];
$OSID = $_POST['OSID'];
$Description = $_POST['Description'];
$ScreenSize = $_POST['ScreenSize'];
$Price = $_POST['Price'];
$StockQuantity = $_POST['StockQuantity'];
$StorageSize = $_POST['StorageSize'];
$thumb= $_POST['ThumbImg'];
$large= $_POST['LargeImg'];
if (!isset($IsFlagged))
{
$IsFlagged= 0;
}
else
{
    $IsFlagged = $_POST['IsFlagged'];
}

$sql = "UPDATE phones SET ManufacturerID='$ManufacturerID', Model='$Model', OSID='$OSID', Description='$Description', ScreenSize='$ScreenSize', Price='$Price', StockQuantity='$StockQuantity', StorageSize='$StorageSize', ThumbImg='$thumb', LargeImg='$large', IsFlagged='$IsFlagged' WHERE PhoneID='$PhoneID' ";

I'm not sure what the problem is. I also need to stop it from throwing up errors if the checkbox is unticked for example or if no image filename is entered into the text boxes.

<option value="1" 
    <? if($varForThisSelect == 'Apple') echo "selected = 'selected'" ?>>
Apple
</option>

You'll want to add that PHP to each option. There's probably a better way to do it (less lines of code), but this is what I've done in the past.

Basically, what you'll do is add a value to the <input> tags, which will cause the input to be prepopulated with whatever is in that value. In your case, you would do the following for <input> tags:

<p>Description: <input type="text" name="Description" 
        value="<?php echo htmlspecialchars($Description);?>"></p>

For the <select> tags, you'll be checking if the value in each option equals your variable, then displaying selected="selected" for each option. For example:

<option value="1" <?php if($ManufacturerID == 1) echo 'selected="selected"';?>>Apple</option>

If you have PHP short tags enabled on your server, you can condense the above line a bit, making it into a ternary operation. Keep in mind, though, that not all servers have short tags enabled.

<option value="1" <?= $ManufacturerID == 1 ? 'selected="selected"' : ''?>>Apple</option>

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