I have created this form to update a product, but I can't seem to get it to change the database. I have a list of items product that the user choses from. When he click on the link I pass with id variable. Here is the code for my single for update page (my first single page update form, so be gentle):
<?php
$id = $_GET['id'];
if (isset($_POST['submitted'])) {
//Initialize error array
$errors = array();
//check for a order number
if (empty($_POST['part_number'])) {
$errors[] = 'You forgot the part number.';
}
//Check for Total errors
if(empty($errors)) {
//include statement for database connectivity
include 'config.php';
// create a new mysqli object with default database
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die ("Unable to connect");
// create updated command
$sql = "update geist_parts set
part_number = '$_POST[part_number]',
voltage ='$_POST[voltage]',
amperage = '$_POST[amperage]',
active = '$_POST[active]'
where id = '$_POST[pid]'";
//excute query
$result = mysqli_query($connection, $sql) or die ("Error in query: $sql. ".mysqli_error());
// close connection
mysqli_close($connection);
//congrats - Part number changed - redirect to listing page
header("Location: view_parts_list.php");
//if - else $errors
} else {
//PCE intranet page layout
echo '<html>';
echo '<head>';
echo '<title>Intranet</title>';
echo '<link href="../includes/xxx.css" rel="stylesheet" type="text/css">';
echo '</head>';
include '../includes/main_menu.php';
echo '<body BGCOLOR=#FFFFFF topmargin=0 leftmargin=0
marginheight=0 marginwidth=0 rightmargin=0 bottommargin=0>';
echo '<center>';
echo '<table border=0 cellpadding=0 cellspacing=0 width=100% vailn=top>';
include '../includes/left_nav.php';
echo '<div align=center>';
//report the errors
echo '<h1>Error!</h1>The following error(s) occurred: <br /><p>';
foreach ($errors as $msg) {
//print the errors message
echo "- $msg<br />\n";
}
echo '<p><p>Please go back and try again</p></p>';
//end of Intranet tables
echo '</table>';
}
//if - else statement isset
} else {
?>
<script>
function toForm() {
document.part_number.focus();
}
</script>
<html>
<head>
<title>Intranet</title>
<link href="../includes/pce.css" rel="stylesheet" type="text/css">
</head>
<!-- add main menu -->
<?php include '../includes/main_menu.php';?>
<body onload="toForm()" BGCOLOR=#FFFFFF topmargin="0" leftmargin="0"
marginheight=0 marginwidth=0 rightmargin=0 bottommargin=0>
<center>
<table border=0 cellpadding=0 cellspacing=0 width=100% vailn=top>
<!-- server side include leftside nav table -->
<?php include '../includes/left_nav_pce.php'; ?>
<!-- end of left side nav -->
<br>
<form name="parts" method="post" action="view_parts_list.php">
<div align="center">
<p> </p>
<?php
//include statement for database connectivity
include 'config.php';
// create a new mysqli object with default database
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die ("Unable to connect");
// sql to INSERT a new record
$sql = "select id, part_number, voltage, amperage, active
from geist_parts where id = '$id'";
//excute query
$result = mysqli_query($connection, $sql) or die ("Error in query: $sql. ".mysqli_error());
//create list of variables from query results
list($pid, $part_number, $voltage, $amperage, $active) = @mysqli_fetch_row($result);
// close connection
mysqli_close($connection);
?>
<table width="350" border="1" cellpadding="5" style='border-collapse: collapse'>
<tr>
<td colspan="3" bgcolor="#CCCCCC"><div align="center"><strong>
<font size="2" face="Arial, Helvetica, sans-serif">Part Number <?php echo $pid; ?></font></strong></div></td>
</tr>
<tr>
<td>
<table border=0 cellpadding=3 cellspacing=0 width=99%>
<tr>
<td width=100%><font size="2" face="ARIAL, SANS-SERIF">
<p><strong>Part Number:</strong><br>
<input type="text" name="part_number" size=50 maxlength=75 value="<?php echo $part_number; ?>">
</p>
<p><strong>Voltage:</strong><br>
<input type="text" name="voltage" size=50 maxlength=75 value="<?php echo $voltage; ?>">
</p>
<p><strong>Amperage:</strong><br>
<input type="text" name="amps" size=50 maxlength=75 value="<?php echo $amperage; ?>">
</p>
<p><strong>Active:</strong><br>
<?php if($active != "") {
echo ("<input type=checkbox CHECKED name='active'><br>");
} else {
echo ("<input type=checkbox name='active'><br>");
}; ?>
</p>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="pid" value=<?php echo $pid; ?>"
<input type="hidden" name="submitted" value="true" />
</div></td>
</tr>
</table>
<?php
}
?>
I know that it's probably something really simple, but I'm tired of chasing my tail. Thanks for any help that you can provide.
A