Reza86 0 Newbie Poster

Hi everyone, have visited the site a few times as a lurker and found answers to most questions.

At the moment I'm stuck with a piece of code, I can't see why it won't work.

The page below has the variable $Repair_No posted to it from another page, the code to select the details from the database work fine and information is displayed, the problem i'm having is with the $storesupdate query. For some reason it will not write to the database. A second issue is that currently when the page does write to the database it creates a new record and doesn't update the record I want.

Many thanks for any help!

Heres a copy of the entire code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php
// Include important information
require_once('common/important.inc');

//Include the header
require_once($fullpath."common/header.inc");


//Check that user is logged in
if (isset($userid))
{
  $firstname = strtok($username,' ');
?>

        <h1>Customer Repair Form</h1>

<?php
//Selects the highest repair number from the Repairs table and increase it by one, this number is then used as the repair
//number in the new repairs forms.
$maxq = "select max(Repair_No) from Repairs" ;
$maxqresult = mysql_query($maxq, $link_id);
while($query_data = mysql_fetch_row($maxqresult))
{
$Maxrecord = $query_data[0];
}
$Maxrecord++;
?>

<?php
//changes the status depending on what fields have been filled in on the form.
if($Repair_No == "")
{
$Status = 'New Repair Order';
}
elseif($Repair_No != "")
{
$Status = 'Awaiting Stores Booking In';
}
elseif($Recieved_by != "" && $Quantity_Recieved != "" && $Date_Recieved != "")
{
$Status = 'Booked in and Awaiting Inspection';
}
?>
        
<?php
if($Repair_No != "")
{
$selectquery = "SELECT * FROM Repairs WHERE Repair_No ='$Repair_No'";

$selectresult = mysql_query($selectquery, $link_id);
$query_data = mysql_fetch_assoc ($selectresult);

$Repair_No = $query_data['Repair_No'];
$Customer_Name = $query_data['Customer_Name'];
$Customer_Location = $query_data['Customer_Location'];
$Part_Description = $query_data['Part_Description'];
$Raised_By = $query_data['Raised_By'];
$Contact = $query_data['Contact'];
$Part_No = $query_data['Part_No'];
$Date_Entered = $query_data['Date_Entered'];
$Quantity = $query_data['Quantity'];
$Quantity_Recieved = $query_data['Quantity_Recieved'];
$Recieved_By = $query_data['Recieved_By'];
$Date_Recieved = $query_data['Date_Recieved'];
}
?>

<?php
//Once data has been inserted into the form and the update button clicked this will run and insert it into the database.
if(isset($insert)){

$insertquery = "INSERT INTO Repairs SET Repair_No = '$Maxrecord', Customer_Name = '$Customer_Name', Customer_Location = '$Customer_Location',
Raised_By = '$Raised_By', Contact = '$Contact', Part_No = '$Part_No' , Date_Entered = '$Date_Entered',
Part_Description = '$Part_Description' , Quantity = '$Quantity'";

$CreateResult = mysql_query($insertquery, $link_id);
$numrows = mysql_fetch_row($CreateResult);
 //echo $insertquery;
header ("location: {$_SERVER["PHP_SELF"]}");
}
?>
        
<?php
//If there is no repair number then select the customer name and location from Custom2.
if($Repair_No == ""){

$CustomerDetails = "SELECT Customer_Name, Customer_Location FROM Custom2 WHERE Customer_Reference = '$CustomerReference'";

$Customerresult = mysql_query($CustomerDetails , $link_id);
$query_data = mysql_fetch_assoc ($Customerresult);

$Customer_Name = $query_data['Customer_Name'];
$Customer_Location  = $query_data['Customer_Location'];
//echo $CustomerDetails;
header ("location: {$_SERVER["PHP_SELF"]}");
}
?>

<?php
if (isset($insert))
{
{

if ($Quantity_Recieved != "" && $Recieved_By != "" && $Date_Recieved != "")

$storesupdate = "UPDATE Repairs SET Repair_No = '$Repair_No', Quantity_Recieved = '$Quantity_Recieved', Recieved_By = '$Recieved_By',
Date_Recieved = '$Date_Recieved' WHERE Repair_No = '$Repair_No'";

$CreateResult = mysql_query($storesupdate, $link_id);
$numrows = mysql_fetch_row($CreateResult);

echo $storesupdate;

header ("location: {$_SERVER["PHP_SELF"]}");
}
}
?>


        
<h5>Repair No. <?php echo "$Repair_No";?></h5>
<h5>Sales &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status: <?php echo $Status; ?></h5>
<form name = "insert" method = "post" action = "repairadmin.php">
<table border = "0">


<td>Customer:</td>
<td><input type = "text" size = "25" name = "Customer_Name" value = "<?=$Customer_Name;?>"></td>
<td>Branch:</td>
<td><input type = "text" size = "17" name = "Customer_Location" value = "<?=$Customer_Location;?>"></td>
<td>Contact:</td>
<td><select name = Contact >
<option selected value = "<?=$Contact?>"><?=$Contact?>
<?
$query = "select Firstname, Surname from Contact2 where CustomerReference = '$CustomerReference'";
$result = mysql_query($query, $link_id);
while($query_data = mysql_fetch_row($result))
{
$name = $query_data[0]." ".$query_data[1];
?>
<option value = "<?=$name?>"><?=$name?>
<?
}
?>
</select></td>
</table>

<table>
<td>Part Number:</td>
<td><input type = "text"  name = "Part_No" value = "<?=$Part_No;?>"></td>
<td>Part Description:</td>
<td><textarea name="Part_Description" cols="51" rows="2"><?php echo $Part_Description;?></textarea></td>
</table>

<table>
<td>Quantity to return:</td>
<td><input type = "text" size ="5" name = "Quantity" value = "<?=$Quantity;?>"></td>

<td>Raised By:</td>
<td><Select name ='Raised_By' value = "<?= $Raised_By?>" SIZE="1"/<?=$hide;?>>
<option selected value = "<?=$Raised_By;?>"><?=$Raised_By;?> </option>
<option value="Claudia Lange">Claudia Lange</option>
<option value="Andy Mawson">Andy Mawson</option>
<option value="David Terry">David Terry</option>
</select>
</td>

<td>Date Entered:</td>
<?php
if($Date_Entered == "" or $Date_Entered == "00-00-0000")
{
$Date_Entered = date("d-m-Y");
}
?>
<td><input onclick="ds_sh(this);" value="<?php echo (isset($Date_Entered)) ? $Date_Entered : ""; ?>
" name="Date_Entered" readonly="readonly" style="cursor: text" size = "10"<?=$hide;?> /></td>
</table>

<h5>Stores/Goods In</h5>  
<table>
<td>Quantity Received:</td>
<td><input type ="text" size = "5" name = "Quantity_Recieved" value = "<?=$Quantity_Recieved;?>"></td>

<td>Recieved By:</td>
<td><Select name ='Recieved_By' value = "<?= $Recieved_By?>" SIZE="1"/<?=$hide;?>>
<option selected value = "<?=$Recieved_By;?>"><?=$Recieved_By;?> </option>
<option value="Starbuck">Starbuck</option>
<option value="Apollo">Apollo</option>
<option value="Cat">Cat</option>
</select>
</td>

<td>Date Recieved:</td>
<?php
if($Date_Recieved == "" or $Date_Entered == "00-00-0000")
{
$Date_Entered = date("d-m-Y");
}
?>
<td><input onclick="ds_sh(this);" value="<?php echo (isset($Date_Entered)) ? $Date_Entered : ""; ?>
" name="Date_Recieved" readonly="readonly" style="cursor: text" size = "10"<?=$hide;?> /></td>
</table>


<!--Insert Data into Repairs table-->
<table>
<td><input type = "submit" name = "insert" value = "Update"></td>
</table>
</form>

<!--Footer-->
<?php
}
else
{
include_once($fullpath."common/not_logged_in.inc");
}
require_once($fullpath."common/footer.inc");
?>
    </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.