Hey all,
I am about to paste a huge slab of code, but only because I cannot figure out where the issue is… as it basically does what it should do…
So this is a page that can add or edit records from a main view page. It does exactly what it should do, the form is empty and there are a couple of drop down boxes for people to use when they are creating. When you edit the record the form then opens with all the fields just as text boxes showing the previously entered fields.
There was a request to put a drop down box on the edit page some someone can edit the project or craft of a user.
The issue is I maintain this page and make basic mods and the like but I didn’t build it. What I am finding is that when I try and create a dropdown box on the form when it is in edit mode, there is never any data being pulled back from the back end lookup tables to populate the drop down.
Somewhere in this mass of code I am dropping the connection to the database when in edit mode and I cannot find how to populate a drop down box.
An example is here;
[php]
<?php
$resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup");
if($resultNames) {
?>
Craft: |
Please Select
<?php
while ($MCraft = $resultNames->fetch_assoc()) {
echo "".$MCraft['txtCraftGroup']."";
}
}else{
$row_cnt = $resultNames->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>
|
Craft: |
|
<?php
}
?>
[/php]
So what is happening there is when the form is opened as a result of pressing edit for a record the $resultnames is NEVER populated.
Part two of this mess is having a drop down box with the previously entered field… but at the moment I would love to just see a drop down box with populated options on this for when edit is pressed.
Help me Obi-… oh sorry wrong train of thought…
[php]
<?php
// connect to the database
include_once "connect-db.php";
?>
<?php
/*
Allows the user to both create new records and edit existing records.
*/
function renderForm($id = '', $MName = '', $MProject = '', $MCraft= '',$MEarliest = '',$MBy = '', $MFrom ='', $MTo ='', $MNotes ='', $error = ''){
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable.
GLOBAL $conn;
?>
Add / Edit Moves Request
<?php
if ($id != ‘’) { echo “Edit Existing Move”; } else { echo “Request New Move”; } ?>
<?php if ($id != '') { echo "Edit Existing Move"; } else { echo "Request New Move"; } ?>
<?php echo "When entering in a 'Name' type last name first and it will auto complete"; ?>
<?php
echo “
Return to Moves List
”;
?>
<?php if ($error != '') {
echo "
" . $error
. "
";
} ?>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php } ?>
<table width="800" border="1" cellspacing="5" cellpadding="5" style="border-collapse: collapse; background-color: #F6F4DD; border: 2px solid lightblue; ">
<tr>
<td><label for="personname">Name: </label></td>
<td><input id="personname" type="text" name="personname" value="<?php echo $MName; ?>"/><br/></td>
</div>
</tr>
<?php
$resultNames = $conn->query("SELECT txtProjectName FROM tblProject Order by txtProjectName");
if($resultNames) {
?>
Project: |
Please Select
<?php
while ($MCraft = $resultNames->fetch_assoc()) {
echo "";
echo $MCraft['txtProjectName'];
echo "";
}
}else{
?>
|
Project: |
|
<?php
}
?>
<?php
$resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup");
if($resultNames) {
?>
Craft: |
Please Select
<?php
while ($MCraft = $resultNames->fetch_assoc()) {
echo "".$MCraft['txtCraftGroup']."";
}
}else{
$row_cnt = $resultNames->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>
|
Craft: |
|
<?php
}
?>
Earliest Move: |
|
Move By: |
|
Move from: |
|
Move To: |
|
Move Notes: |
|
|
<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record.
$int = settype($id, "integer");
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$MName = htmlentities($_POST['personname'], ENT_QUOTES);
$MProject = htmlentities($_POST['project'], ENT_QUOTES);
$MCraft = htmlentities($_POST['craft'], ENT_QUOTES);
$MEarliest = htmlentities($_POST['earliestmove'], ENT_QUOTES);
$MBy = htmlentities($_POST['moveby'], ENT_QUOTES);
$MFrom = htmlentities($_POST['movedfrom'], ENT_QUOTES);
$MTo = htmlentities($_POST['moveto'], ENT_QUOTES);
$MNotes = htmlentities($_POST['notes'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($MName == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
//renderForm($MName, $MFrom, $MTo, $MNotes, $error, $id);
renderForm($MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes);
}
else
{
// if everything is fine, update the record in the database
//if ($stmt = $conn->prepare("UPDATE tblmovesheets SET Name = ?, EarliestMove = ?, MoveBy = ?, MoveFrom = ?, MoveTo = ?, notes = ?
if ($stmt = $conn->prepare("UPDATE tblMoveSheets SET Name = ?, Status = ?, Craft = ?, EarliestMove = ?, MoveBy = ?, MoveFrom = ?, MoveTo = ?, notes = ?
WHERE MoveID = ?"))
{
$body = "A move has been updated for ".$_POST['personname']."";
$body .= " moving to ".$_POST['moveto']."";
$subject = 'Move Updated';
$to = '
[email protected]';
//$subject = 'Ticket:50619 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
$headers = 'From:
[email protected]';
mail($to, $subject, $body, $headers);
$stmt->bind_param("ssssssssi", $MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: moves-view.php");
}
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
//if (is_numeric($_GET['MoveID']) && $_GET['MoveID'] > 0)
if ($_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
//echo $newval;
$int = settype($id, "integer");
// get the recod from the database
if($stmt = $conn->prepare("SELECT MoveID, Name, Status, Craft, EarliestMove, MoveBy, MoveFrom, MoveTo, notes FROM tblMoveSheets WHERE MoveID=?"))
{
$stmt->bind_param("i",$id);
$stmt->execute();
$stmt->bind_result($id, $MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes);
$stmt->fetch();
// show the form
renderForm($id, $MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: moves-view.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$MName = htmlentities($_POST['personname'], ENT_QUOTES);
$MProject = htmlentities($_POST['project'], ENT_QUOTES);
$MCraft = htmlentities($_POST['craft'], ENT_QUOTES);
$MEarliest = htmlentities($_POST['earliestmove'], ENT_QUOTES);
$MBy = htmlentities($_POST['moveby'], ENT_QUOTES);
$MFrom = htmlentities($_POST['movedfrom'], ENT_QUOTES);
$MTo = htmlentities($_POST['moveto'], ENT_QUOTES);
$MNotes = htmlentities($_POST['notes'], ENT_QUOTES);
echo "BLAHHH";
echo $_POST['craft'];
// check that firstname and lastname are both not empty
if ($MName == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes);
}
else
{
// insert the new record into the database
if ($stmt = $conn->prepare("INSERT INTO tblMoveSheets (Name, Status, Craft, EarliestMove, MoveBy, MoveFrom, MoveTo, ConvertedName, notes) VALUES ('$MName', '$MProject', '$MCraft', '$MEarliest', '$MBy', '$MFrom', '$MTo', '$MName', '$MNotes')"))
{
$body = "A move has been submitted for ".$_POST['personname']."";
$body .= " moving to ".$_POST['moveto']."";
if ($_POST['craft'] == "R and D") {
$subject = 'Ticket:50978 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
} else {
$subject = 'Ticket:50619 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
}
$to = '
[email protected]';
//$subject = 'Ticket:50619 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
$headers = 'From:
[email protected]';
mail($to, $subject, $body, $headers);
$stmt->bind_param("ssssssss", $MName, $MProject, $MCraft, $MEarliest, $MBy, $MFrom, $MTo, $MNotes);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: moves-view.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$conn->close();
?>
[/php]