Jump to content

Can't populate records when editing a page


stevenls

Recommended Posts

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
  $resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup");
  if($resultNames) {
  
  ?>
  <tr>
    <td>Craft: </td>
    <td><select name="craft">
   <option value="0">Please Select</option>
 <?php
   while ($MCraft = $resultNames->fetch_assoc()) {
       echo "<option value=\"".$MCraft['txtCraftGroup']."\">".$MCraft['txtCraftGroup']."</option>";        
   }
   }else{
 $row_cnt = $resultNames->num_rows;
     printf("Result set has %d rows.\n", $row_cnt);
 ?>
 <td>Craft: </td>
 <td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/></td>
 <?php
       }
     ?>
   </select></td>
   </tr>

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
// 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;
 ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
  <meta charset="utf-8">
  <title>Add / Edit Moves Request</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#personname" ).autocomplete({
      source: 'search.php'
    });
  });
  $( function() {
    $( "#datepickerfrom" ).datepicker(
      {dateFormat: 'dd/mm/yy'}
    );
  } );
  $( function() {
    $( "#datepickerto" ).datepicker(
      {dateFormat: 'dd/mm/yy'}
    );
  } );
  </script>
<title>
<?php 

if ($id != '') { echo "Edit Existing Move"; } else { echo "Request New Move"; } ?>

</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h2><?php if ($id != '') { echo "Edit Existing Move"; } else { echo "Request New Move"; } ?></h2>
<?php echo "When entering in a 'Name' type last name first and it will auto complete"; ?>
<?php

echo "<p><a href='moves-view.php'><b>Return to Moves List</b></a></p>";
 ?>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>

<form action="" method="post">
<div class="ui-widget">

  <?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) {

  ?>
  <tr>
    <td>Project: </td>
    <td><select name="project">
    <option value="0">Please Select</option>
  <?php
    while ($MCraft = $resultNames->fetch_assoc()) {
        echo "<option value=\"{$MCraft['txtProjectName']}\">";
        echo $MCraft['txtProjectName'];
        echo "</option>";
    }

    }else{
  ?>
  <td>Project: </td>
  <td><input type="text" name="project" value="<?php echo $MProject; ?>"/><br/></td>
  <?php
        }
      ?>
    </select></td>
    </tr>


<?php
$resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup");
if($resultNames) {

?>
<tr>
  <td>Craft: </td>
  <td><select name="craft">
  <option value="0">Please Select</option>
<?php
  while ($MCraft = $resultNames->fetch_assoc()) {
      echo "<option value=\"".$MCraft['txtCraftGroup']."\">".$MCraft['txtCraftGroup']."</option>";        
  }
  }else{
$row_cnt = $resultNames->num_rows;
    printf("Result set has %d rows.\n", $row_cnt);
?>
<td>Craft: </td>
<td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/></td>
<?php
      }
    ?>
  </select></td>
  </tr>



  <tr>
    <td>Earliest Move: </td>
  <td><input id="datepickerfrom" type="text" name="earliestmove" value="<?php echo $MEarliest; ?>"/><br/></td>
      </tr>
  <tr>
    <td>Move By: </td>
    <td><input id="datepickerto" type="text" name="moveby" value="<?php echo $MBy; ?>"/><br/></td>
  </tr>
  <tr>
    <td>Move from: </td>
    <td><input type="text" name="movedfrom" value="<?php echo $MFrom; ?>"/><br/></td>
  </tr>
  <tr>
    <td>Move To: </td>
    <td><input type="text" name="moveto" value="<?php echo $MTo; ?>"/><br/></td>
  </tr>
  <tr>
    <td>Move Notes:</td>
    <td><input type="text" size="60" name="notes" value="<?php echo $MNotes; ?>"/><br/></td>
  </tr>
<tr>
   <td><input type="submit" name="submit" value="submit" /></td>
   </tr>
</table>
</div>
</form>
</body>
</html>

<?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    = 'stevenls@al.com.au';
    //$subject = 'Ticket:50619 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
    $headers = 'From: stevenls@al.com.au';
    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    = 'helpdesk@al.com.au';
    //$subject = 'Ticket:50619 Action:TechUpdate Hidden:NO Status: EmailClient:YES';
    $headers = 'From: stevenls@al.com.au';
    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();
?>

Link to comment
Share on other sites

apply some general trouble-shooting to the problem -

 

1) do you have php's error_reporting set to E_ALL, display_errors set to ON, and output_buffering set to OFF so that php will help you by reporting and displaying all the errors it detects and doesn't hide any errors or output from your code when the header() redirects are executed?

 

2) do you have error handling for the php mysqli statements? if you set the mysqli error mode to exceptions, combined with the above error_reporting/display_errors settings, php will catch and handle any mysqli errors and display the actual error information. to set the mysqli error mode to exceptions, add the following two lines of code before the point where you are making the database connection - 

// note: the $driver variable name used in the following two lines is unique to these two lines of code and don't have any relationship to any other variable used in your code
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <- w/index checking; w/o index checking -> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
Link to comment
Share on other sites

after reviewing the code, i'm going to dispute your statement that it basically does what it should do. of the four places you are calling the renderForm(....) function, only two have a parameter list that would produce expected results, and i'm not even sure the code is reaching the second of those two calls.

 

forget about your renderForm() function. just put the html document at the end of the file and produce and store the input data that it needs in php variables. the inputs to the html document are the - id, form field values (if you use an array for these, you won't have to write out variables for each field and you can operate on all the data as a set), errors (if you use an array, you can easily validate all the data at once and have a discrete error message for each one), project/status select list data (fetch the data from the query into an array), and the craftgroup select list data (fetch the data from the query into an array.) by just moving the html document to the end and having the php code concentrate on producing the input data for it, a lot of the existing logic will be eliminated.

 

a word about constancy, it counts when programming, especially if someone will have to read and figure out your code. the form field names and database column names should be the same and your project/status data should be named for what it actually is.

 

a word about your database design. the tblMoveSheets table should store the project id and the craft id, not the project name and craft name and if the name/personname data are users for the system this code is part of, you would store the user id, not the name. the two select/option menus for the project and craft choices should use the data's id as the option value and the data's name as the display value, so that they submit the id values to be used by the insert/update query code.

 

there doesn't appear to be any sort of user authenticate/permission system to control what the code does, so anyone can create or edit any data. if this is more than just a classroom assignment, you need to have a user authenticate/permission system.

 

htmlentities() is an output function. it should only be used on the data as it is being output in the html document.

 

you need to have error handling for all the database statements. the connection, query(), prepare(), and execute() statements can fail and provide important trouble-shooting information (bind_... statements throw php errors instead), which you should log on a live server and display when learning, developing, and debugging code. see point #2 in my post above in this thread for how to use exceptions to do this. this will eliminate the discrete error handling logic you have in the code now.

 

as you have identified, the existence of an id is what controls if the code executes an insert or an update query. you should eliminate all the duplication in the two sections of code by only having the things that are different enclosed in conditional logic. only the sql query statement (and if you use the SET version of the INSERT query most the two query statements will be the same) and the binding of input parameters is different between the two queries. the validation of the inputs and the execution of the query are the same and you should organize the logic so that these common parts only exist once in the code.

 

lastly, if you can, switch to use the php PDO extension. this will further simplify the code (there's no need to bind the results and you can skip binding the inputs, by supplying them as a parameter to the execute() call.) the INSERT query isn't even using a prepared query correctly now, needs more work, and is probably throwing errors.

Edited by mac_gyver
Link to comment
Share on other sites

Hey,

 

Yeh it's not great, but it's inherited so it is what I have to work with until I can do a full rebuild of it.  The page works in the way that when someone creates a record the database is updated and when they click edit the same form is displayed and the data can be edited, but the drop down boxes are not available in 'edit mode' anymore... that is what I have been tasked to fix.

 

I can't figure out why the $conn is dropped when it's in edit mode.  I put a check in one of the code blocks and it's right in the sense that at the end of the check it says there are 0 rows.

<?php
$sql = "SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup";
$resultNames = mysqli_query($conn, $sql);
if($resultNames) {

?>
<tr>
  <td>Craft: </td>
  <td><select name="craft">
  <option value="0">Please Select</option>
<?php
  while ($row = $resultNames->fetch_assoc()) {
      echo "<option value=\"".$row['txtCraftGroup']."\">".$row['txtCraftGroup']."</option>";        
  }
  }else{
$row_cnt = $resultNames->num_rows;
    printf("Result set has %d rows.\n", $row_cnt);
?>
<td>Craft: </td>
<td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/></td>
<?php
      }
    ?>
  </select></td>
  </tr>

When you are adding a record, the craft group drop down list is populated, a value can be selected and boom you can save the record.  When you are editing the existing record, the value is displayed as a pulled value in the text field;

<td>Craft: </td>
<td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/></td>

But the users want the option to change the craft from a dropdown list if it was entered incorrectly in the first place.

 

I cannot understand why the underlying recordset is never populated for the craft table when editing a record.

 

I know I am making a mess of this question,  in a nut shell, I need to have a drop down list on this form at the point of add and edit.

 

eergghhh.. hurts my head.

Link to comment
Share on other sites

until you do the two things suggested in reply #2, to get some feedback from php/mysqli as to where and what's occurring, we cannot help you. if the logic you have posted is all the code on the page, nothing is different with $conn between inserting a record and editing it.

Link to comment
Share on other sites

Where I am located is different to the team that supports the server side of things to turn on the error handling (I don't have access to the back end side of things).  I lodged a ticket and have to wait for feedback about what is or isn't enabled, so I went back to toiling with the code.

 

Yeh the code I pasted is the whole slab of php for the edit / add page.

 

I am completely stumped with it.  There is just no way I can get a drop down box running when the page is acting as the edit side of the task.

 

Anyway, I appreciate the time you spent on this mess, thanks for giving it a crack.

 

S.

Link to comment
Share on other sites

Also, I bunged a whole heap of print statements through the code where I could..  every time there is this else clause;

$sql = "SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup";
$resultNames = mysqli_query($conn, $sql);
if($resultNames) {

?>
<tr>
<td>Craft: </td>
<td><select name="craft">
<option value="0">Please Select</option>
<?php
while ($row = $resultNames->fetch_assoc()) {
echo "<option value=\"".$row['txtCraftGroup']."\">".$row['txtCraftGroup']."</option>"; 
}
}else{
$row_cnt = $resultNames->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>

The $conn is dropped, always returns 0 for number of rows.

Link to comment
Share on other sites

Your code makes no sense, and you clearly haven't done anything that has been suggested to you.

 

Either you fix the error handling as you've been told multiple times. Or there's nothing we can do for you. Programming is not voodoo. We cannot magic your problems away.

Link to comment
Share on other sites

I was told by the IT guru's here that the error logs are written to the server, I put in some chunks of code per the previous comments and have to wait for the logs to be emailed to me.

 

Unless I am missing a way for the error tracking to be more dynamic and display on screen while all the code is executing, I have to wait.

 

This is what I put in some row_cnt checks in the code to try and come up with a trail of information on the pages to see where the $conn drops off.  

 

No advice is being ignored, just work within a VERY slow moving wheel for support and swimming in a pool I don't normally swim in, so just trying to make things work within the very small confines I have to work in.

Link to comment
Share on other sites

hah.. have to log a ticket to retrieve the logs..     DOH

 

 

 

 

you should be learning, developing and debugging on a localhost development system. only after code is written and thoroughly tested should it be put onto a live server.

 

Unless I am missing a way for the error tracking to be more dynamic and display on screen while all the code is executing,

 

 

the suggested settings cause all php errors to be reported and displayed, which is why the settings are named error_reporting and display_errors. if you cannot find how to set these in your code, ask, but about 80% of all the php threads in all the programming help forums on the web, and in the signature lines of a number of forum member's, show how you can set these in your code for debugging purposes.

Link to comment
Share on other sites

I have a wamp setup on my laptop for anything new I am working on and code I am trying to create better than this lump that I was presented with - for this case though I am stuck only being able to Tortoise commit and update pages when I change the code.  The page I am working is on the production server but a copy of the production page they want changed, so my tinkering is at least not breaking the production page.. I am trying in this instance to work detached where I can from the prod pages...  it's far from ideal but it's like "oh Steve built that database for us, get him to make the changes we need to this page".. ummm ok!

 

anyhow..  I have the errors displaying now, just commenting for the sake that anyone looking at this understands I am wading through this to find some sort of resolution.

 

So I did a vardump of the $resultnames directly under the if..  so basically when someone clicks 'Add New Record', the drop down box appears on the page, is populated and the dump spits this out;

object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(30) ["type"]=> int(0) }

 

The key being the int(30) as there are 30 rows.. seems to make sense.

 

I put a vardump after the else..   and the else is triggered because when someone clicks edit record the array is always empty..  tied in with error handling I get this;

bool(false)

Trying to get property of non-object in /var/www/vhosts/newstarter.al.com.au/movesEdit.php on line 126 

Result set has 0 rows

 

The Result set has 0 rows is the print of the row_cnt for $resultNames

 

So I loop back to when someone edits the records the array is never populated for the drop down.  I think this was never an issue as the 'edit record' page only ever showed text boxes of what was previously entered.  Now someone wants a drop down box on the edit field, this weird empty array issue has come about.  

 

So at least I know that for some reason somewhere from opening the edit screen the connection is not actually dropped, cause the text boxes are populated..   just anywhere where this is called;

$sql = "SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup";
$resultNames = mysqli_query($conn, $sql);

it returns nothing.

 

Anyway.. thanks for steering me in the general direction it is helping me.

Link to comment
Share on other sites

I had that in the connect-db page..   obviously didn't understand where that went and I wasn't getting any errors on screen.  That is all now sitting at the top of the add / edit page.

 

Adding records, no errors, drop downs appear etc etc all great, data added to the underlying table.

 

Pressing edit;

 

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Commands out of sync; you can't run this command now' in /var/www/vhosts/newstarter.al.com.au/movesEdit.php:86 Stack trace: #0 /var/www/vhosts/newstarter.al.com.au/movesEdit.php(86): mysqli->query('SELECT txtProje...') #1 /var/www/vhosts/newstarter.al.com.au/movesEdit.php(264): renderForm(2767, 'Bilson, Jack', 'Peter Rabbit', 'Compositing', '25/05/2017', '31/05/2017', 's15-01', 'sb07-02', '') #2 {main} thrown in /var/www/vhosts/newstarter.al.com.au/movesEdit.php on line 86

 

Failing at the exact line that works though when the record is being added.  Looking up Commands out of sync now.

Link to comment
Share on other sites

As for logs - why do you need logs to do your own debugging?  Why not do some echo statements where you have concerns and see the info on your screen?  And if you have turned on php error checking you will see those errors right on your screen also.  Logs? Who needs logs?

Link to comment
Share on other sites

yeh I was echoing out row counts at different points, vardump of arrays / variables etc etc.  As previously highlighted by someone, I am running around in circles not being able to get my simple grey matter around the fact works in one instance and not in another.

 

Looking at the command sync stuff I think there is an issue here;

// 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();

As the array isn't filled only when the form is being edited (when the above code is required to pull the data).

 

Is the $stmt -> close() killing the connection to the database?  I am not sure cause without the error handling the form populates all the text boxes fine.. it fails when I try and fill a combo box when I put one on the edit form.  It seems to make sense, just not sure how to make sense of it.  

Link to comment
Share on other sites

Got it..   $stmt->store_result();

 

after execute.

 

So now the page shows the drop down boxes on the add and the edit page.  Obviously they now flatten the previously entered result, but that is a new wave of searching to figure out about the retaining the previous value in a drop down box

 

Thanks all.

Link to comment
Share on other sites

So to end this.. I know there is code issues, things are not done the right way, ID fields are not being leveraged.

 

BUT.. putting bandaids on something I plan to redo when time and resources permits it does what it needs to.

 

Drop down boxes are populated with values already entered when a record is being edited and new records are added as they should be.

 

This is messy but this works now

<?php
$sql = "SELECT txtCraftGroup FROM tblCraftGroup Order by txtCraftGroup";
$resultCraft = mysqli_query($conn, $sql);
?>
<tr>
  <td>Craft: </td>
  <td><select name="craft">
  <option value="0">Please Select</option>
<?php
  while ($row = $resultCraft->fetch_assoc()) {
    echo '<option value="'.$row['txtCraftGroup'].'"';
    if (isset($MCraft) && $MCraft==$row['txtCraftGroup']){ echo ' selected="selected"'; }
    echo '>'.$row['txtCraftGroup'].'</option>';
  }
 ?>
  </select></td>
  </tr>

 

Now to get back to getting Lego Ninja out the door and moving onto animated rabbits :)

Edited by stevenls
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.