MySQL & PHP help needed.

Discussion in 'Software' started by pari22, Nov 23, 2009.

  1. pari22

    pari22 Corporal

    Hello,
    Pretty new to mysql & PHP. Got everything working (db engine & php installed - some tables are setup and php pages are working).
    Now, I want to get into little bit technical side of this so I can fully use the benefits of db & php. Basically, I want to assign a field to be automatically calculated and value to be updated with that result. For example:
    Field1 = 10
    Field2 = 20
    Field3 = field1 + field2
    I've tried it in php and was able to show the result but I can not figure out how to actually send the calculated value to the db server.
    Could anyone help me with this? Isn't there a better way to do this on MySQL side, instead of php?
     
  2. JJJIrish05

    JJJIrish05 Sergeant

    how are you calculating field3?
     
  3. pari22

    pari22 Corporal

    In a php page that shows the db's result set, I included:
    <?php echo (field1+filed2) ?>

    This is showing me the calculation, but I do not know what/where/how to send this as actual field value.
    To be clear, I would like this to be done on the page where I enter the new values (called "addpage"). I enter filed1 & filed2. filed3 is hidden at this phoint. click on "Update" button, then the calculated value is sent to the db.
    Can this be done?
     
  4. JJJIrish05

    JJJIrish05 Sergeant

    Yes, it can be done, it can be done without the use of a db as well if you don't need to store the value for later use. If you post your code I can tell what to add without having to explain everything depending on what you've got so far.
     
  5. pari22

    pari22 Corporal

    Thanks!!
    I will get back to you with the php codes. I need to go take care of something now...
     
  6. SWario

    SWario Sergeant

    I agree with JJJIrish. Please post your code and link us to the site so we can see what you are trying to do.
     
  7. pari22

    pari22 Corporal

    Guys,
    Sorry about late post... and thank you for your support!!
    Anyway, I found the problem and solved it for adding new records page.
    What I've done is: set the field3 = field1+field2 -- done that.
    What I've forgot to do is: make the field3 hidden, so it doesn't even show on the "add new record page".
    Eventually, I'd like to learn how to show the result as you type the field1 & field2... like those online order forms. But that's later.
    Here is the code. Very simple form since I'm only testing and learning. :)
    However, I am having problems with calculating time. This is driving me crazy! Simple numbers are being calculated without any problems but my new fields (timeIN & timeOUT) are giving me headaches. If you guys have any idea how to calculate time difference, could you give me an example??

    <form id="form1" name="form1" method="post" action="insert.php">
    <p>Field1 :
    <!-- name of this text field is "field1" -->
    <input name="field1" type="text" id="field1" />
    <br />
    Field2 :
    <!-- name of this text field is "field2" -->
    <input name="field2" type="text" id="field2" />
    <br />

    <!-- name of this text field is "filed3" -->
    <input type="hidden" name="field3" type="text" id="field3" />
    </p>

    <p>
    <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>

    <?php
    mysql_connect("localhost", "user", "password");
    mysql_select_db("test");

    $field1 = $_POST['field1'];
    $field2 = $_POST['field2'];
    $field3 = $field1+$field2;

    mysql_query("insert into test1(field1, field2, field3) values('$field1',

    '$field2', '$field3')");

    mysql_close();

    ?>
     
  8. SWario

    SWario Sergeant

  9. pari22

    pari22 Corporal

    timeIN & timeOUT was NOT included in first test. I was still trying to figure out and learn about calculations as you can see. I will check out the site and get back to you on time functions.
    Thanks~!
     
  10. pari22

    pari22 Corporal

    I was just checking out the site and figuring out the $_SERVER['REQUEST_TIME'] function... and it works but gives me some kind of weird numbers... Is this normal?
    When I do:
    <?php
    echo $_SERVER['REQUEST_TIME'];
    ?>

    It gives me this:
    1259948305
     
  11. JJJIrish05

    JJJIrish05 Sergeant

    Code:
    [COLOR=Black]<form id="form1" name="form1" method="post" action="insert.php">
       <p>Field1 : 
         <!-- name of this text field is "field1" -->
         <input name="field1" type="text" id="field1" />
         <br />
         Field2 : 
         <!-- name of this text field is "field2" -->
         <input name="field2" type="text" id="field2" />
         <br />
       <p>
         <input type="submit" name="Submit" value="Submit" />
       </p>
    </form> 
    
    <?php
      if(array_key_exists('field1', $_POST) && array_key_exists('field2', $_POST)){
        mysql_connect("localhost", "user", "password");
        mysql_select_db("test");
    
        $field1 = $_POST['field1']; 
        $field2 = $_POST['field2']; 
    [/COLOR][COLOR=Black]    $field3 = $field1+$field2;[/COLOR]
    [COLOR=Black] 
        mysql_query("insert into test1(field1, field2, field3) values('$field1', '$field2', '$field3')");
    
        mysql_close();
      } 
    ?>[/COLOR]
    You don't need to have field3 in your form at all since you calculate it later and nothing is stored in it. Also you should make sure the values exist before doing anything with them using array_key_exists. To get field3 to auto calculate you will have to use some javascript with AJAX.

    As for your time issue use the date function instead http://php.net/manual/en/function.date.php
     
  12. pari22

    pari22 Corporal

    JJJIrish,
    Thanks for the info. Like I said, I've hidden the field3 from the form (just there to calculate and send the result to the DB). It's working fine and that test is now closed.
    For the time difference issue, I looked at the site and got the functions working - showing time, date, day...etc. It seems like I can also achieve that with many different methods. What I can not figure out is the difference between times.
    To be clear, let's say I entered current timestamp in to the "timeIN" field. After the day, project, sales, work...etc is over, I enter the "timeOUT" with current timestamp.
    I need to calculate the difference between "timeOUT" & "timeIN" in hours. I know this is pretty simple for you, but it keeps giving me sql error or some weird numbers. Could you possibly give me an example?
     
  13. JJJIrish05

    JJJIrish05 Sergeant

    (time1-time2)/(60*60)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds