Can't populate records when editing a page

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]

As a coincidence I am redoing my own website and I think you are going about this in the wrong way to be honest with you. First a few general quick observations on your scripts.

First, tables should never be mixed with form HTML elements. I know there are some college professors out there that teach their students this way, but in my opinion they should be flogged. ;D

Second try to keep your php, HTML, and javascript separated as much as possible. I usually try to keep most of my php at the top, the HTML right in the middle and javascript/jquery at the bottom just before the body and html tags.

Thirdly and last, I like to design my page (HTML/CSS) first then adding my php code after I have gotten the design the way I want it. Lastly I add javascript last after I have my PHP working, for once you get your php working adding javascript into the mix usually is a piece of cake. The final result should be a logical and neat format code/HTML with nice CSS that can be look at after some time has passed and still make sense.

With that said here’s an example of what I doing for my website. I’m adding a form where administrators and eventually users can add images and text to a page dynamically and that is usually called a CMS (Content Management System).

Here’s what I have so far for my form ->
members_page
[php]<?php
require_once ‘…/private/initialize.php’;
protected_page();
require_once ‘…/private/includes/header.inc.php’;
?>

<?php //echo "
" . print_r($_SESSION['user'], 1) . "
"; ?>
            <legend>Page, Column and Image</legend>
            <div id="mainselection">
                <select name="page_name">
                    <option value="index.php" selected>Home Page</option>
                    <option value="about.php">About Page</option>
                    <option value="blog.php">Member Blog Page</option>
                </select>
            </div>
            <div class="maxl">
                <label class="radio inline"> 
                    <input type="radio" name="column_pos" value="left" checked>
                    <span>Column Left</span> 
                </label>
                <label class="radio inline"> 
                    <input type="radio" name="column_pos" value="right">
                    <span>Column Right</span> 
                </label>
            </div>
            <div class="maxl">
                <label class="radio inline">
                    <input type="radio" name="insert_image" value="yes" checked>
                    <span>Include Image</span>
                </label>    

                <label class="radio inline">
                    <input type="radio" name="insert_image" value="no">
                    <span>No Image</span>
                </label>
            </div>
        </fieldset>
        <fieldset id="mainEntry">
            <legend>Data Entry Form</legend>
            <input type="hidden" name="user_id" value="<?php echo $_SESSION['user']->id; ?>">
            <input type="hidden" name="action" value="enter">
            <input id="imgBtn" type="file" name="file">
            <label class="inputLabel" for="heading">Heading</label>
            <input id="heading" type="text" name="heading" value="" tabindex="1" autofocus>
            <label class="textareaLabel" for="content">Content</label>
            <textarea id="content" name="content" tabindex="2"></textarea>
            <input type="submit" name="submit" value="enter">
        </fieldset>
    </form>
</div>
[/php]

Notice I have a drop down menu in my form that I will eventually add php to it, so it will be more dynamic. However, I first want to get the HTML/CSS done before I move on to the php script portion.

I just started a Github Repository in case you want to check back time to time to check on my progress. Here’s the link ->
https://github.com/Strider64/Slice-of-Pi

An here’s my website -> https://www.pepster.com/ and it’s also in my signature.
HTH John

Thanks for the reply. I have a copy of the page I posted where I am trying to split it all out into more understandable sections, but I break it every time.

Any pages I have done myself are much clearer in the way the sections are defined.

This isn’t my handywork but I think with me adding things to it I have made it even more messy and now these changes requested have me completely lost.

Sponsor our Newsletter | Privacy Policy | Terms of Service