Andy Hinkle Andy Hinkle - 5 months ago 22
SQL Question

Form Update - Not working

All,

I'm migrating from the obsolete

mysql_query
to
mysqli
and during the process I'm having some issues with the form updating changes. Specifically, it seems as if the
$ud_id
variable to get the
id
is not working.

The queries are written to left outer join link tables
empinfo
and
headcount
in which both of these of the linking primary id.

Here is the edit.php page which works and loads as intended:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<div class="header">
<?php include 'header.php';?>
</div>

<center>

<?php
// db connection
include_once('../connection.php');

// id pull from url
$ID = (int)$_GET['id'];
// get results from database
$query = "SELECT headcount.*, empinfo.*
FROM headcount
LEFT OUTER JOIN empinfo
ON headcount.id = empinfo.id
WHERE headcount.id = '$ID'";

$response = mysqli_query($dbc, $query);

while($row = mysqli_fetch_array($response))

{

echo "<form action='update.php' method='post'>";
echo '<div style="width:40%;padding:0 10pt 0 0;float:middle;">';
if ($row['isActive'] === '1')
echo '<input type="checkbox" name="ud_isActive" value="1" checked>Active<br />';
else
echo '<input type="checkbox" name="ud_isActive" value="0">Active<br />';
echo '<input type="hidden" name="ud_ID" value="<?=$ID;?>">';
echo "ID #: <input type='text' name='ud_hwid' value=". $row['id'] ." disabled readonly><br />";
echo 'Name: <input type="text" name="ud_EmployeeName" value="'. $row["EmployeeName"] .'"><br />';
echo '<br />';
echo '<a href="mailto:' . $row["Email"] .'">Email</a>: <input type="text" name="ud_Email" value="'. $row["Email"] .'"><br>';
echo '<br />';
echo 'DOB: <input type="text" name="ud_DOB" value='. $row["DOB"] .'><br />';
echo 'Age: <input type="text" name="ud_Age" value='. $row["Age"] .'><br />';
if ($row['isUnderage'] === '1')
echo '<input type="checkbox" name="isUnderage" value="1" checked>Underage<br />';
else
echo '<input type="checkbox" name="isUnderage" value="0">Underage<br />';
echo '</div>';
echo '<div style="width:40%;padding:0 10pt 0 0;float:middle;">';
echo '<br />';
echo 'Address: <input type="text" name="ud_StreetAddress" value="'. $row["StreetAddress"] .'"><br />';
echo 'City: <input type="text" name="ud_City" value="'. $row["City"] .'"><br />';
echo 'Zip Code: <input type="text" name="ud_ZipCode" value="'. $row["ZipCode"] .'"><br />';
echo '<br />';
echo 'Home Phone: <input type="text" name="ud_HomePhone" value="'. $row["HomePhone"] .'"><br />';
echo 'Wireless: <input type="text" name="ud_Wireless" value="'. $row["Wireless"] .'"><br />';
echo '<br />';
echo 'Home Department: <input type="text" name="ud_HomeDept" value="'. $row["HomeDept"] .'"><br />';
echo 'Department Manager: <input type="text" name="ud_Manager" value="'. $row["Manager"] .'"><br />';
echo '</div>';
echo '<br />';
echo 'Live Orientation:<input type="date" name="LODate" value=""><br />';
echo '<br />';
echo 'Online Training: <input type="date" name="OTTraining" value=""><br />';



}

?>
<br />

<input type="Submit">
</form> <br />
<a href="javascript:history.back()">Go Back</a>
<br />
</body>
</html>


Here is the update.php -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<style type="text/css">
#wrap {
width:600px;
margin:0 auto;
}
#left_col {
float:left;
width:300px;
}
#right_col {
float:right;
width:300px;
}
</style>

<div class="header">
<?php include 'header.php';?>
</div>

<center>

<?php
$ud_ID = $_REQUEST["ID"];
$ud_EmployeeName = $_POST["ud_EmployeeName"];
$ud_Email = $_POST["ud_Email"];
$ud_DOB = $_POST["ud_DOB"];
$ud_Age = $_POST["ud_Age"];
$ud_StreetAddress = $_POST["ud_StreetAddress"];
$ud_City = $_POST["ud_City"];
$ud_ZipCode = $_POST["ud_ZipCode"];
$ud_HomePhone = $_POST["ud_HomePhone"];
$ud_Wireless = $_POST["ud_Wireless"];
$ud_HomeDept = $_POST["ud_HomeDept"];
$ud_Manager = $_POST["ud_Manager"];

// db connection
include_once('../connection.php');

$query = "UPDATE headcount SET
EmployeeName = '$ud_EmployeeName', Email = '$ud_Email',
DOB = '$ud_DOB', Age = '$ud_Age', StreetAddress = '$ud_StreetAddress',
City = '$ud_City', ZipCode = '$ud_ZipCode', HomePhone = '$ud_HomePhone',
Wireless = '$ud_Wireless', HomeDept = '$ud_HomeDept', Manager = '$ud_Manager'
WHERE ID = '$ud_ID'";

$response = mysqli_query($dbc, $query);

echo $ud_ID;
echo $ud_EmployeeName;

if ($response)
echo '<p>Record Updated<p> <br /> <a href="javascript:history.back()">Go Back</a>';
else
echo "Problem updating record. MySQL Error: " . mysql_error();
?>
</body>
</html>


At the line 52 of update.php you will notice I use echo
$ud_ID
and
$ud_EmployeeName
- This is to show me what the variable is displaying. The
$ud_EmployeeName
appears and the
$ud_ID
does not.

I also have tried using

$ud_ID = $_POST["ID"];


and I no results.

Additionally, on edit.php I have tried:

echo '<input type="hidden" name="ud_ID" value='. $row["id"] .'><br />';


and I receive the error:

Notice: Undefined index: ID in update.php on line 27


When I click the
submit
button, I receive the message that the record has been updated but on refresh, the record has the same data.

Answer

Hi use name property to get value

$ud_ID = $_POST['ud_ID'];
Comments