Hello,
I'm a total newbie to the web site and would greatly appreciate any help with the following problem.
I have an 'Edit Item' part of a web site I am working on (free) for a friend allowing him to edit items in his Shop.
It displays the data in the fields ok, but when I submit the page (which uses the UPDATE query), six of the fields in the database go to zero('0') as it is trying to put the text value into a TINYINT field.
Anyone have any idea of inserting the relevant index value from the option into the field in the database.
I've attached my code in case you need a look 🙂
Many Thanks.
<?php # this file allows a user to edit an items details.
// This page edits an item.
$page_title = "Edit an Item";
include ('../includes/HEADERPAGE.html');
require_once ('../../connectorfile.php'); // Connect to the db.
$id = $_GET['id'];
// Checks if the form has been submitted.
if (isset($_POST['submitted'])) {
$itemname = stripslashes(strip_tags(trim($_POST['itemname'])));
$descr = stripslashes(strip_tags(trim($_POST['descr'])));
$rrp = stripslashes(strip_tags(trim($_POST['rrp'])));
$price = stripslashes(strip_tags(trim($_POST['price'])));
$qty = stripslashes(strip_tags(trim($_POST['qty'])));
$uksize = stripslashes(strip_tags(trim($_POST['uksize'])));
$ussize = stripslashes(strip_tags(trim($_POST['ussize'])));
$eusize = stripslashes(strip_tags(trim($_POST['eusize'])));
if (!isset($_POST['type'])) {
die('You must select the type');
}
if (!isset($_POST['label'])) {
die('You must select the label');
}
if (!isset($_POST['stype'])) {
die('You must select the sub-type');
}
if (!isset($_POST['colour'])) {
die('You must select a colour');
}
if (!isset($_POST['condition'])) {
die('You must select a condition');
}
if (!isset($_POST['material'])) {
die('You must select a material');
}
$query = "UPDATE items SET name='$itemname', label='$label', type='$type', stype='$stype', qty='$qty', uksize='$uksize', ussize='$ussize', eusize='$eusize', colour='$colour', condition='$condition', material='$material', description='$descr', rrp='$rrp', price='$price', pandp='$pandp' WHERE id = $id";
$result = @mysql_query ($query); // Run the Query
if (mysql_affected_rows() == 1) { // If it ran OK and one row was affected.
// Print a message
echo '<H2>Edit your Item</H2>
<P>The item has been edited</p><p></p>';
// Finish the page
echo '<H3>Thank you for editing the item.</h3>';
include ('../includes/footerpage.html'); // Include the HTML footer.
exit();
} else { // No changes made
echo '<p><font color="red" size="2">No changes have been made to the item.</font></p>';
}
} // End of the main Submit conditional.
// Always show the form.
// Retrieve the user's information.
$query = "SELECT i.id, i.type, i.label, t.id, t.type, l.id, l.name, i.name, i.description, i.price, i.date, s.stype, i.qty, i.uksize, i.colour, c.id, c.colours, i.condition, i.material, i.rrp, con.id, con.condition, m.id, m.material, i.ussize, i.eusize, i.largeimageone, i.pandp, s.id FROM items as i, type as t, label as l, stype as s, colour as c, condition as con, material as m WHERE i.type = t.id AND i.label = l.id AND i.stype = s.id AND i.colour = c.id AND i.condition = con.id AND i.material = m.id AND i.id = $id";
$result = mysql_query($query) or die("Query failed: $query - " . mysql_error());
if (mysql_num_rows($result) == 1) { // Valid ID, show the form.
// Get the user's information.
$row = mysql_fetch_array($result, MYSQL_NUM);
// Create the form.
echo '<h3>Edit the Item</h3>
<form action="edit_item.php?id=' . $_GET[id] . '" method="post">
<p><b>Type :</b><br />
<select name="type" size="1">
<option></option>
'."\n";
$type = array('footwear','clothing','mens','bargain','ladies','kids','football','misc','wholesale','joblots');
for($i = 0, $j = count($type); $i < $j; $i++){
echo '<option value="'.$type[$i].'"'.(($type[$i] == @$row[4])?' selected="selected"':'').'>'.$type[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>Item Name:</b><br /><input type="text" name="itemname" size="30" maxlength="30" value="' . $row[7] . '" /></p>
<p><b>Label:</b><br />
<select name="label" size="1">
<option></option>
'."\n";
$label = array('adidas','fredperry','schott','reebok','lacoste');
for($i = 0, $j = count($label); $i < $j; $i++){
echo '<option value="'.$label[$i].'"'.(($label[$i] == @$row[6])?' selected="selected"':'').'>'.$label[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>Qty:</b><br /><input type="text" name="qty" size="2" maxlength="2" value="' . $row[12] . '" /></p>
<p><b>Sub-Type:</b><br />
<select name="stype" size="1">
<option></option>
'."\n";
$stype = array('mens','ladies','kids','football','misc');
for($i = 0, $j = count($stype); $i < $j; $i++){
echo '<option value="'.$stype[$i].'"'.(($stype[$i] == @$row[11])?' selected="selected"':'').'>'.$stype[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>UK Size:</b><br /><input type="text" name="uksize" size="40" maxlength="40" value="' . $row[13] . '" /></p>
<p><b>US Size:</b><br /><input type="text" name="ussize" size="40" maxlength="40" value="' . $row[24] . '" /></p>
<p><b>EU Size:</b><br /><input type="text" name="eusize" size="40" maxlength="40" value="' . $row[25] . '" /></p>
<p><b>Colour:</b><br />
<select name="colour" size="1">
<option></option>
'."\n";
$colour = array('White','Black','Red','Blue','Green','Pink','Brown','Navy','Silver','Yellow');
for($i = 0, $j = count($colour); $i < $j; $i++){
echo '<option value="'.$colour[$i].'"'.(($colour[$i] == @$row[16])?' selected="selected"':'').'>'.$colour[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>Condition:</b><br />
<select name="condition" size="1">
<option></option>
'."\n";
$condition = array('New','Old');
for($i = 0, $j = count($condition); $i < $j; $i++){
echo '<option value="'.$condition[$i].'"'.(($condition[$i] == @$row[21])?' selected="selected"':'').'>'.$condition[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>Material:</b><br />
<select name="material" size="1">
<option></option>
'."\n";
$material = array('Leather','Cloth');
for($i = 0, $j = count($material); $i < $j; $i++){
echo '<option value="'.$material[$i].'"'.(($material[$i] == @$row[23])?' selected="selected"':'').'>'.$material[$i].'</option>
'."\n";
}
echo ' </select>
<p><b>Description :</b><br><textarea name="descr" cols=40 rows=5>' . $row[8] . '</textarea></p>
<p><b>RRP :</b> £<input type="text" name="rrp" size="6" maxlength="6" value="' . $row[19] . '" /> </p>
<p><b>Price :</b> £<input type="text" name="price" size="6" maxlength="6" value="' . $row[9] . '" /> </p>
<p><b>P & P :</b> £<input type="text" name="pandp" size="30" maxlength="30" value="' . $row[27] . '" /> </p>
<br />
<input type="submit" name="submit" value="Edit Item" class="submit"/>
<input type="hidden" name="submitted" value="TRUE" />
</form>';
} else { // Not a valid User ID.
echo '<h2>Page Error</h2>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close connection with db.
include ('../includes/footerpage.html'); // Include the HTML footer.
?>
CREATE TABLE `items` (
`id` tinyint(4) NOT NULL auto_increment,
`type` tinyint(4) NOT NULL default '0',
`name` varchar(255) NOT NULL default '',
`label` tinyint(4) NOT NULL default '0',
`qty` tinyint(4) NOT NULL default '0',
`stype` tinyint(4) NOT NULL default '0',
`uksize` tinyint(4) NOT NULL default '0',
`ussize` tinyint(4) NOT NULL default '0',
`eusize` tinyint(4) NOT NULL default '0',
`colour` tinyint(4) NOT NULL default '0',
`condition` tinyint(4) NOT NULL default '0',
`material` tinyint(4) NOT NULL default '0',
`description` text NOT NULL,
`rrp` float NOT NULL default '0',
`price` float NOT NULL default '0',
`pandp` tinyint(4) NOT NULL default '0',
`date` date NOT NULL default '0000-00-00',
`thumb` blob NOT NULL,
`content_type` varchar(20) NOT NULL default '',
`largeimageone` mediumblob NOT NULL,
`largeimagetwo` mediumblob NOT NULL,
`largeimagethree` mediumblob NOT NULL,
PRIMARY KEY (`id`)
)