Jump to content

Having to Press submit button twice to update database! please Help!


eits

Recommended Posts

Hi,

 

Can anybody tell me why in the following code once I enter something in the field hourstoday I have to press the submit button twice to actually update the database? I want it to update when I click submit once!!

 

<?php require_once('Connections/timedb.php'); ?>
<?php $hourstoday = $_POST['hourstoday']; ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE timemanagement SET clientname=%s, hoursworked=%s, hoursleft=%s, hoursallocated=%s WHERE clientid=%s",
                       GetSQLValueString($_POST['clientname'], "text"),
                       GetSQLValueString($_POST['hoursworked'], "text"),
                       GetSQLValueString($_POST['hoursleft'], "text"),
                       GetSQLValueString($_POST['hoursallocated'], "text"),
                       GetSQLValueString($_POST['clientid'], "int"));

  mysql_select_db($database_timedb, $timedb);
  $Result1 = mysql_query($updateSQL, $timedb) or die(mysql_error());
}

mysql_select_db($database_timedb, $timedb);
$query_conn = "SELECT * FROM timemanagement";
$conn = mysql_query($query_conn, $timedb) or die(mysql_error());
$row_conn = mysql_fetch_assoc($conn);
$totalRows_conn = mysql_num_rows($conn);
?>

<?php
function hourstodecimal ($timeinhours) {
  $timeparts = explode(':', $timeinhours);
  return ($timeparts[0] + ($timeparts[1]/60));
}

function decimaltohours ($timeindecimal) {
  $hours = floor($timeindecimal);
  $minutes = str_pad( (($timeindecimal - $hours)*60), 2, '0');
  return $hours.':'.$minutes;
}

//$time_in_hrs = "5:15";
//echo $time_in_hrs . ' in decimal format is ' . hourstodecimal($time_in_hrs);
//Output: 5:15 in decimal format is 5.25

echo "<br><br>";

//$time_in_dec = "3.75";
//echo $time_in_dec . ' in hours:min format is ' . decimaltohours($time_in_dec);
//$timesplit = explode(".", $time_in_dec);
//$hoursplit = $timesplit[0];
//$minutesplit = $timesplit[1];

?>

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" id="box">
  <tr>
    <td height="17" valign="top"><div align="center" class="headstyle">
      <p>Customer Time Logging</p>
      <p class="contstyle">This page allows you to log your time each time you visit a customer. This will then be removed from their monthly allowance and tell you the remaining time left for their support contract. </p>
      <p class="contstyle"><?php echo $totalworked; ?></p>
    
      <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
        <table width="263" align="center">
          <tr valign="baseline">
            <td width="99" align="right" valign="middle" nowrap class="contstyle"><div align="left">Clientname:</div></td>
            <td width="106" valign="middle" class="contstyle"><label>
              <div align="left">
                <select name="select">
                  <?php
do {  
?>
                  <option value="<?php echo $row_conn['clientname']?>"><?php echo $row_conn['clientname']?></option>
                  <?php
} while ($row_conn = mysql_fetch_assoc($conn));
  $rows = mysql_num_rows($conn);
  if($rows > 0) {
      mysql_data_seek($conn, 0);
  $row_conn = mysql_fetch_assoc($conn);
  }
?>
                </select>
                </div>
            </label></td>
          </tr>

          <tr valign="baseline">
            <td align="right" valign="middle" nowrap class="contstyle"><div align="left">Hours Worked:</div></td>
            <td valign="middle" class="contstyle"><div align="left">
                  <input type="text" name="hourstoday" value="0" size="6"> 
              HH:MM </div></td>
          </tr>
          <tr valign="baseline">
            <td align="right" valign="middle" nowrap class="contstyle"><div align="left">Remaining Hours:</div></td>
            <td valign="middle" class="contstyle"><div align="left">
                  <input type="text" name="hoursallocated" value="<?php echo $row_conn['hoursallocated']; ?>" size="6"> 
              HH:MM </div></td>
          </tr>
          <tr valign="baseline">
            <td colspan="2" align="right" valign="middle" nowrap class="contstyle"><div align="center">
              <input type="submit" value="Update record">
            </div></td>
            </tr>
        </table>
        <p>
          <input type="hidden" name="MM_update" value="form1">
          <input type="hidden" name="clientid" value="<?php echo $row_conn['clientid']; ?>">
          <input type="hidden" name="hoursleft" value="<?php echo $row_conn['hoursleft']; ?>">
          <input type="hidden" name="hoursworked" value="<?php echo $row_conn['hoursworked'] + $hourstoday; ?>">
        </p>
        <p>HOURS WORKED <?php echo $row_conn['hoursworked']; ?><br />
          HOURS LEFT         <?php echo $row_conn['hoursleft']; ?></p>
        </form>
      <p> </p>
    </div></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($conn);
?>

I don't see a problem. Why do you say you have to do it twice? Try adding a print right after this line:

$Result1 = mysql_query($updateSQL, $timedb) or die(mysql_error());

print 'Query Ran: '.$updateSQL.' Result: '.$Result1;

 

What does it do?

Also, can I look at the live version if you don't mind? IE, a link?

Just wondering why do you have it like...

 

<?php require_once('Connections/timedb.php'); ?>
<?php $hourstoday = $_POST['hourstoday']; ?>
<?php
etc...

 

That sems a bit silly to me, why not have it like...

 

<?php
require_once('Connections/timedb.php');
$hourstoday = $_POST['hourstoday'];
etc...

 

Just saying.  ;)

Hi - The query bit I entered shows that it submits to the DB but only after I press submit twice. I would love to show you a live version but unfortunately it isn't on my web server yet.

 

In respect of my coding - I will change that bit! probably very poor - I am new to PHP so  ::).

 

I am thinking that

  <input type="hidden" name="hoursworked" value="<?php echo $row_conn['hoursworked'] + $hourstoday; ?>">

 

is only getting the value from $hourstoday after the page has been submitted  :-\

When I print that query I get the following on the first submit:

Query Ran: UPDATE timemanagement SET clientname=NULL, hoursworked='7.75', hoursleft=NULL, hoursallocated='10' WHERE clientid=1 Result: 1

 

and after pressing enter again:

Query Ran: UPDATE timemanagement SET clientname=NULL, hoursworked='8.75', hoursleft=NULL, hoursallocated='10' WHERE clientid=1 Result: 1

 

This is assuming the DB was already on 7.75hrs and I just entered 1hr into the textbox hourstoday

I get:

 

Array ()

 

then when I click again I get:

 

Array ( [select] => [hourstoday] => 00:00 [txtremhours] => 2:45 [MM_update] => form1 [clientid] => 1 [clientname] => Business 1 [hoursleft] => [hoursallocated] => 10 [hoursworked] => 7.25 ) Query Ran: UPDATE timemanagement SET clientname='Business 1', hoursworked='7.25', hoursleft=NULL, hoursallocated='10' WHERE clientid=1 Result: 1

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.