Jump to content

Archived

This topic is now archived and is closed to further replies.

dhrups

updating database!

Recommended Posts

Hi, first of all thanks to Ray and Ken for helping me with my previous problem.

This problem is abit different. I am trying to update a table in the database. My table consists of the following feilds: category, model_number, description, price and quantity.
Now i am trying to update the quantity for each row in the table. The model number is a primary key and it is varchar as i have to use numbers and letters as e.g 9047 NB etc, and some of the model numbers are just plain numbers e.g 9554.
I have done the following scripts but its not working. i have one file called view_all_models.php where i am viewing whats on the database and i have a hyperlink called Edit next to it which takes me to the edit_model.php page and lets me input the new details.

Could someone please check it and let me know whats wrong? any better solutions?

[b]file1: view_all_models.php[/b]
-------------------------------
<?php

require_once("config.php");

?>

<html>
<head>
<title><?php echo "Machine Details" ?></title>

<head>
<body>

<table border="1" width="100%" height="226">
<tr>
<td width="22%" height="91" bgcolor="#0099FF">&nbsp;
</td>
<td width="78%" height="91" bgcolor="#66CCFF" ALIGN=CENTER>&nbsp;

<H1> View MAchine Details </H1>


<?

$query = "SELECT category, model_number, price, quantity FROM models";

$result = mysql_query ($query);

$num = mysql_num_rows($result);

if ($num > 0){
echo "<p>There are currently $num stored machine details. </p>\n";

echo '<table align = "center" border = "1" cellspacing = "0" cellpadding = "5">
<tr>
<td align="left"><b>Edit</b></td>
<td align="left"><b>Category</b></td>
<td align="left"><b>Model Number</b></td>
<td align="left"><b>Price</b></td>
<td align="left"><b>Quantity</b></td>
</tr>
';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '
<tr>
<td align="left"><a href="edit_model.php?model_number='. $row['model_number']. '">Edit</a></td>
<td align="left">'. $row['category']. '</td>
<td align="left">'. $row['model_number']. '</td>
<td align="left">'. $row['price']. '</td>
<td align="left">'. $row['quantity']. '</td>
</tr>
';
}
echo '</table>';

mysql_free_result($result);


} else {

echo '<p class = "error">There are no stored machines.</p>';
}
mysql_close();


?>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</TABLE>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>


</form>
</td>

</table>

</body>
</html>
---------------------------------------------------------------------------------------------------------------------
[b]file2: edit_model.php[/b]
--------------------------------
<?php
require_once("config.php");
$connection = @mysql_connect($db_host, $db_user, $db_password) or die("oops! error connecting");
mysql_select_db($db, $connection);
?>

<html>
<head>
<title><?php echo "Edit Machine Details" ?></title>

<head>
<body>

<table border="1" width="100%" height="226">
<tr>
<td width="22%" height="91" bgcolor="#0099FF">&nbsp;

<td width="78%" height="91" bgcolor="#66CCFF" ALIGN=CENTER>&nbsp;

<H1> Edit Machine Details </H1>

</td>
</tr>

<?

function escape_data($data){
if(ini_get('magic_quotes_gpc')){
$data = stripslashes($data);
}

if (function_exists('mysql_real_escape_string')){
global $connection;
$data = mysql_real_escape_string(trim($data), $connection);
} else {
$data = mysql_escape_string(trim($data));
}

return $data;

}

if ((isset($_GET['model_number']))){
($id = $_GET['model_number']);
}
elseif ((isset($_POST['model_number']))){
($id = $_POST['model_number']);
}
else {
echo '<h1 model_number="mainhead">PAGE ERROR</h1>
<p class="error">This page has been accessed in errorpuj.</p><p><br/><br/></p>';
exit();
}

if (isset($_POST['submitted'])){

$errors = array();


//check for first name
if(empty($_POST['category'])){
$error[] = 'You forgot to enter the category.';
} else {
$fn = escape_data($_POST['category']);
}


if(empty($_POST['quantity'])){
$error[] = 'You forgot to enter the quantity.';
} else {
$e = escape_data($_POST['quantity']);
}



if(empty($errors)) {


$query = "SELECT model_number FROM models WHERE quantity= '$e' AND model_number != $id";
$result = mysql_query($query);
if(mysql_num_rows($result)==0){

$query = "UPDATE models SET model_number='$id', category='$fn', quantity='$e' WHERE model_number=$id";

$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {

echo '<p> The machine detail has been edited.<p>';

} else {
echo '<h1 model_number="mainhead"> System Error</h1>
<p class = "error" > The machine detail could not be edited due to a system error.</p>';
echo '<p>' . mysql_error() . '<br/><br/> Query: ' . $query . '<p>';

exit();
}

}
else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The quantity had already been registered.</p>';
}

} else {
echo '<h1 model_number="mainhead">Error!</h1>
<p class="error"> The following error(s) occured:<br/>';
foreach ($errors as $msg){
echo " - $msg<br/>\n";
}

echo '</p><p>Please Try Again. </p><p><br/></p>';

}

}





$query = "SELECT category, model_number, quantity FROM models WHERE model_number=$id";
$result = @mysql_query ($query);

if (mysql_num_rows($result) ==1){

$row = mysql_fetch_array ($result, MYSQL_NUM);


echo '<h2> Edit Customer </h2>
<form name="edit_cust_details.php" action = "edit_model.php" method = "post">
<p>Category: <input type="text" name ="category" size="15" maxlength="15" value="' .$row[0] . '"/></p>
<p>Model_number: <input type="text" name ="model_number" size="15" maxlength="15" value="' .$row[1] . '"/></p>
<p>Quantity: <input type="text" name ="quantity" size="15" maxlength="15" value="' .$row[2] . '"/></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="model_number" value="' . $id . '" />
</form>';


} else {
echo '<h1 model_number="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br/><br/></p>';
}

mysql_close();

?>





<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

</form>
</td>
</table>
</body>
</html>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.