I am trying to retrieve Mysql data, populate a form, user amend/delete fields, UPDATE and return data to database. It is the last stage that does not happen. I press UPDATE RECORD and screen returns to ID input. Where have I gone wrong, please ?
<?php //
require_once 'dbconnect.php';
if (isset($_POST['ID'])) $id =$_POST['ID'];
else $id = "No ID entered";
print'<form method="post" action="updateL2.php"/>
<font color="#3F7cef"size="4"/>Enter Ship ID</font>
<input type="text" name="ID" size="15" maxlength ="6"/>
<input type="submit" name="submit" value="ENTER ID"/>
</form> ';
if($id <= 0)
{
echo "<p><font color='red' size='6px'>You must first enter an ID</font></p>";
exit;
}
else
{
echo "<font color='#3F7cef'size='4'/>You searched for:</font>";
echo"ID:$id";
}
$query = "SELECT * FROM launch WHERE id = '$id'";
$result = mysql_query($query);
if (!$result)
echo "SELECT failed: $query<br />" . mysql_error() . "<br /><br />";
$rows=mysql_num_rows($result);
if(mysql_num_rows($result)==0)
{
echo "<p><font color='red' size='6px'>No data found for your request. Try a different query</font></p>";
exit;
}
for ($j=0; $j < $rows ; ++$j)
{
$row =mysql_fetch_array($result);
}
if (isset($_POST['ID']) &&
isset($_POST['Name']) &&
isset($_POST['Tons']) &&
isset($_POST['Built']) &&
isset($_POST['Builder']) &&
isset($_POST['Number']) &&
isset($_POST['Launched'])&&
isset($_POST['Modified']))
{
$id = get_post('ID');
$name = get_post('Name');
$tons = get_post('Tons');
$built = get_post('Built');
$builder = get_post('Builder');
$number = get_post('Number');
$launched= get_post('Launched');
$modified= get_post('Modified');
$update = "UPDATE launch SET name = '$name',tons = '$tons',built = '$built',builder = '$builder',number = '$number',launched = '$launched',modified = '$modified' WHERE
id ='$id'";//if '$id' type ID in form, otherwise enter number in INSERT query.
$result=mysql_query($update);
if (!mysql_affected_rows())
echo "INSERT failed: $update<br />" . mysql_error() . "<br /><br />";
}
echo"<br /> ";
echo"<font color='green' size='4'>The CURRENT RECORD shows</font>";
print' <form method="post" action="updateL2.php"/>
<font color="#3F7cef"size="4"/></font>
<pre>
ID: <input type="text" name="id" size="25" maxlength ="4" value="'.htmlentities($row['ID']).'"/>
Name : <input type="text" name="name" size="25" maxlength ="25" value="'.htmlentities($row['Name']).'"/>
Tonnage: <input type="text" name="tonnage" size="25" maxlength ="25" value="'.htmlentities($row['Tons']).'"/>
Built: <input type="text" name="built" size="25" maxlength ="25" value="'.htmlentities($row['Built']).'"/>
Builder: <input type="text" name="builder" size="25" maxlength ="25" value="'.htmlentities($row['Builder']).'"/>
Yard No: <input type="text" name="number" size="25" maxlength ="25" value="'.htmlentities($row['Number']).'"/>
Launched: <input type="text" name="launched" size="25" maxlength ="25" value="'.htmlentities($row['Launched']).'"/>
Modified: <input type="text" name="modified" size="25" maxlength ="25" value="'.htmlentities($row['Modified']).'"/>
<input type="submit" value="UPDATE RECORD" />
</form>
</pre>';
echo"<font color='red' size='4'>To check UPDATE 'Enter Ship ID' again</font><br/>";
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>