Hi,
I have a script that updates a
multiple select field. I would like it to do the following:
1. check to see if the submitted form has an empty multiple select field and return an error
2. On loading the update page, the previously selected items should be selected in the multiple select field.
Here is what I have so far but I keep on getting the Undefined Index error:
functions.php
PHP Code:
//----------Function for getting item accessories----------
function getAccessories()
{
$sql = "SELECT accessories FROM tablename ORDER BY accessories";
$res = mysql_query($sql);
echo "<select name='accessories[]' size='4' multiple='multiple' class='input'><option value='-1'>------------------------</option>";
while($row = mysql_fetch_row($res)) {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
echo "</select>";
}
//----------Function for updating item on mysql db----------
function updateItem($accessories,$id)
{
if (empty($accessories)) {
return 1;
} else {
$sql = "UPDATE theitem SET item_accessories = '" . $accessories . "' WHERE item_id = " . $id . "";
$res = mysql_query($sql); // or die(mysql_error());
return 99;
}
}
update.php
PHP Code:
require_once ('library/database.php');
include ('library/functions.php');
include ('library/english.php');
if (isset($_POST['update_item'])){
$_POST = cleanCode($_POST);
$serializedaccessories = serialize($_POST['accessories']);
$res = updateItem($serializedaccessories,$_POST['item_id']);
//if successful
if($res == 99){
$message = ITEM_EDITED;
}
//if errors occured
if($res == 1){
$error = EMPTY_FIELD;
}
}
HTML Code:
<HTML>
<HEAD></HEAD>
<BODY>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" name="fmpost">
<input name="item_id" type="hidden" value="<?= $id; ?>" /> //$id got from a function
<span class="error"><?php if(isset($error)){ echo $error;}?></span>
<span class="message"><?php if(isset($message)){ echo $message;} ?></span>
<div><label for="accessories" class="label">Accessories:</label><br /><span class="smallText">Hold down CTRL key and click to select multiple.</span></div>
<div><? getAccessories(); ?> </div>
<div><input type="submit" name="update_item" value="Update" class="postbttn"/></div>
</BODY>
</HTML>
Please help
