Need help pls... cant fiugre out the solution....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tiernan

    Need help pls... cant fiugre out the solution....

    Hey everybody.
    I'm verry new to PHP and MYSQL and have been working on a form that
    when it is submitted stores the information into a mysql database.
    The main problem is that i'm trying to finish up my script for almost
    a week now and I still cant get it right.
    here is the code

    <?php

    include ("ES_include s/appconst.inc.ph p") ;
    // Ceck of begin datum juist is
    if (!checkdate ($_POST['maand_van'],$_POST['dag_van'],$_POST['jaar_van'])){
    echo "De begin datum die u hebt opgegeven is ontjuist <b>"
    ..$_POST['dag_van']. "/" .$_POST['maand_van']. "/" .$_POST['jaar_van'].
    "</b>.<br>";
    }
    else
    {
    //check of begin datum reeds in het database staat
    $datum= $_POST['jaar_van']."-".$_POST['maand_van']."-".$_POST['dag_van'];
    $db = mysql_connect(" $dbHost", "$dbUser","$dbP wd");
    mysql_select_db ($dbName,$db);
    $sql_check = "SELECT * FROM verlof WHERE
    gebruikersID='$ _POST['gebruikersID']' AND datum='$datum'" ;
    $check = mysql_query($sq l_check,$db);

    if (mysql_num_rows ($check) != 0){
    echo "Deze datums staan reeds in het databse op uw naam.\n";
    }
    else
    {
    $sql = "INSERT INTO verlof (gebruikersID, datum, voormiddag,
    namiddag, opm_aanvrager) VALUES
    ('$_POST['gebruikersID']','$datum','$_P OST['voormiddag']','$_POST['namiddag']','$_POST['opm_aanvrager']')";
    $result = mysql_query($sq l);
    echo "Uw verlof aanvraag is aangevraagd.\n" ;
    }
    }
    echo "<br><input type=button value=\"Terug\"
    onClick=\"histo ry.go(-1)\"><br>";

    $datum_van= $_POST['dag_van']."/".$_POST['maand_van']."/".$_POST['jaar_van'];
    $datum_tot= $_POST['dag_tot']."/".$_POST['maand_tot']."/".$_POST['jaar_tot'];

    $day1="Monday";
    $day2="Tuesday" ;
    $day3="Wednesda y";
    $day4="Thursday ";
    $day5="Friday";

    $mydate=$datum_ van;
    $newsplit = explode("/", $mydate);
    $dat1=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

    $mydate=$datum_ tot;
    $newsplit = explode("/", $mydate);
    $dat2=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

    $datc=$dat1;
    while ($datc<=$dat2){
    $datw=date("d/m/Y",$datc);
    $jour=date ("l", $datc);
    if ($day1==$jour || $day2==$jour || $day3==$jour || $day4==$jour
    || $day5==$jour){ print (date("Y/m/d",$datc)."<br> ");
    } $datc=$datc + 86400;
    }


    ?>

    Now what does it have to do..
    it should get all the weekdays (from mon. till vri.) between
    those datum_van and datum_tot and this is the script i use for it
    $datum_van= $_POST['dag_van']."/".$_POST['maand_van']."/".$_POST['jaar_van'];
    $datum_tot= $_POST['dag_tot']."/".$_POST['maand_tot']."/".$_POST['jaar_tot'];

    $day1="Monday";
    $day2="Tuesday" ;
    $day3="Wednesda y";
    $day4="Thursday ";
    $day5="Friday";

    $mydate=$datum_ van;
    $newsplit = explode("/", $mydate);
    $dat1=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

    $mydate=$datum_ tot;
    $newsplit = explode("/", $mydate);
    $dat2=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

    $datc=$dat1;
    while ($datc<=$dat2){
    $datw=date("d/m/Y",$datc);
    $jour=date ("l", $datc);
    if ($day1==$jour || $day2==$jour || $day3==$jour || $day4==$jour
    || $day5==$jour){ print (date("Y/m/d",$datc)."<br> ");
    } $datc=$datc + 86400;
    }

    now here is the second problem.
    in my database i have one table called datum
    now for every weekday between these two dates the script must create a
    record
    so that when i submit my form with the following dates
    13/01/2003
    18/01/2003
    and I have selected "namiddag" or "voormiddag " in my form
    I would find in my datase the following records
    _______________ _______________ _______________ _______________ _______
    | gebruikersID | datum | voormiddag | namiddag |opm_aanvrager|
    -------------------------------------------------------------------
    | Username |13/01/2003 | 1 |or| 1 | some text |
    | Username |14/01/2003 | 0 |0 | some text |
    | Username |15/01/2003 | 0 |0 | some text |
    | Username |16/01/2003 | 0 |0 | some text |
    | Username |17/01/2003 | 0 |0 | some text |
    -------------------------------------------------------------------
    the 18th will be left out because it is a saterday.
    I hope some of the experts here can help me out because I'm totaly
    lost and realy need this script asap many thanks in advanced
    Tiernan.

    ps. sorry for my english :)
  • Tom Thackrey

    #2
    Re: Need help pls... cant fiugre out the solution....


    On 19-Aug-2003, michaeljanssens @bluestripes.be (Tiernan) wrote:
    [color=blue]
    > I'm verry new to PHP and MYSQL and have been working on a form that
    > when it is submitted stores the information into a mysql database.
    > The main problem is that i'm trying to finish up my script for almost
    > a week now and I still cant get it right.
    > here is the code
    >
    > <?php
    >
    > include ("ES_include s/appconst.inc.ph p") ;
    > // Ceck of begin datum juist is
    > if (!checkdate
    > ($_POST['maand_van'],$_POST['dag_van'],$_POST['jaar_van'])){
    > echo "De begin datum die u hebt opgegeven is ontjuist <b>"
    > .$_POST['dag_van']. "/" .$_POST['maand_van']. "/" .$_POST['jaar_van'].
    > "</b>.<br>";
    > }
    > else
    > {
    > //check of begin datum reeds in het database staat
    > $datum= $_POST['jaar_van']."-".$_POST['maand_van']."-".$_POST['dag_van'];
    > $db = mysql_connect(" $dbHost", "$dbUser","$dbP wd");[/color]

    Every database call should have error checking. Add ' or die('connect failed
    because '.mysql_error() ) ' to the above (before the ;). Like:
    $db = mysql_connect(" $dbHost", "$dbUser","$dbP wd") or die('connect failed
    because '.mysql_error() );
    [color=blue]
    > mysql_select_db ($dbName,$db);[/color]

    add error checking
    [color=blue]
    > $sql_check = "SELECT * FROM verlof WHERE
    > gebruikersID='$ _POST['gebruikersID']' AND datum='$datum'" ;[/color]

    When referencing an array IN A STRING you do NOT enclose the array index in
    quotes. Change '$_POST['gebruikersID']' to '$_POST[gebruikersID]' in the
    line above.
    [color=blue]
    > $check = mysql_query($sq l_check,$db);[/color]

    add error checking
    [color=blue]
    >
    > if (mysql_num_rows ($check) != 0){
    > echo "Deze datums staan reeds in het databse op uw naam.\n";
    > }
    > else
    > {
    > $sql = "INSERT INTO verlof (gebruikersID, datum, voormiddag,
    > namiddag, opm_aanvrager) VALUES
    > ('$_POST['gebruikersID']','$datum','$_P OST['voormiddag']','$_POST['namiddag']','$_POST['opm_aanvrager']')";[/color]

    remove the quotes around the array indexes
    [color=blue]
    > $result = mysql_query($sq l);[/color]

    add error checking
    [color=blue]
    > echo "Uw verlof aanvraag is aangevraagd.\n" ;
    > }
    > }
    > echo "<br><input type=button value=\"Terug\"
    > onClick=\"histo ry.go(-1)\"><br>";
    >
    > $datum_van=
    > $_POST['dag_van']."/".$_POST['maand_van']."/".$_POST['jaar_van'];
    > $datum_tot=
    > $_POST['dag_tot']."/".$_POST['maand_tot']."/".$_POST['jaar_tot'];
    >
    > $day1="Monday";
    > $day2="Tuesday" ;
    > $day3="Wednesda y";
    > $day4="Thursday ";
    > $day5="Friday";
    >
    > $mydate=$datum_ van;
    > $newsplit = explode("/", $mydate);
    > $dat1=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);
    >
    > $mydate=$datum_ tot;
    > $newsplit = explode("/", $mydate);
    > $dat2=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);
    >
    > $datc=$dat1;
    > while ($datc<=$dat2){
    > $datw=date("d/m/Y",$datc);
    > $jour=date ("l", $datc);
    > if ($day1==$jour || $day2==$jour || $day3==$jour || $day4==$jour
    > || $day5==$jour){ print (date("Y/m/d",$datc)."<br> ");
    > } $datc=$datc + 86400;
    > }[/color]

    The above while loop doesn't seem to have any syntax errors but it is very
    hard to read, please use proper indentation and avoid putting more than one
    statement on a line. It should look like this:
    while ($datc<=$dat2) {
    $datw=date("d/m/Y",$datc);
    $jour=date ("l", $datc);
    if ($day1==$jour || $day2==$jour || $day3==$jour || $day4==$jour
    || $day5==$jour) {
    print (date("Y/m/d",$datc)."<br> ");
    }
    $datc=$datc + 86400;
    }
    [color=blue]
    >
    >
    > ?>[/color]

    Fix the syntax errors first, then work on the logic errors. While you are at
    it, fix the indentation and put every statement on a separate line.

    If you post here again, please ask a specific question. Tell us specifically
    what error message you are getting and on what line or if the output is
    wrong what is wrong with it, give an example.


    --
    Tom Thackrey

    Comment

    • Tiernan

      #3
      Re: Need help pls... cant fiugre out the solution....

      Thx Tom Thackrey, with your information you got me back on track...
      i have added the error checks like your said... had them there before
      but left them out till the end... so now I have edited my script and
      came up with the next result.

      <?php
      include ("ES_include s/appconst.inc.ph p") ;

      // check of begin datum juist is

      if (!checkdate ($_POST['maand_van'],$_POST['dag_van'],$_POST['jaar_van'])){
      echo "De begin datum van uw aanvraag is geen geldige datum <b>"
      ..$_POST['dag_van']. "/" .$_POST['maand_van']. "/"
      ..$_POST['jaar_van']."</b>.<br>";
      }
      elseif (!checkdate ($_POST['maand_tot'],$_POST['dag_tot'],$_POST['jaar_tot'])){
      echo "De eind datum van uw aanvraag is geen geldige datum <b>"
      ..$_POST['dag_tot']. "/" .$_POST['maand_tot']. "/" .$_POST['jaar_tot'].
      "</b>.<br>";
      }
      else
      {

      //check of begin datum reeds in het database staat

      $datum= $_POST['jaar_van']."-".$_POST['maand_van']."-".$_POST['dag_van'];

      $db = mysql_connect(" $dbHost", "$dbUser","$dbP wd")
      or die('Connecting to database failed.');

      mysql_select_db ($dbName,$db)
      or die('Unable to select database.');

      $sql_check = "SELECT * FROM verlof WHERE
      gebruikersID='$ _POST[gebruikersID]' AND datum='$datum'" ;
      $check = mysql_query($sq l_check,$db)
      or die('Unable to query database.');

      if (mysql_num_rows ($check) != 0){
      echo "Deze datums staan reeds in het databse op uw naam.\n";
      }
      else
      {

      //Filter datums tussen begi nen eind datum
      $datum_van= $_POST['dag_van']."/".$_POST['maand_van']."/".$_POST['jaar_van'];
      $datum_tot= $_POST['dag_tot']."/".$_POST['maand_tot']."/".$_POST['jaar_tot'];
      $day1="Monday";
      $day2="Tuesday" ;
      $day3="Wednesda y";
      $day4="Thursday ";
      $day5="Friday";

      $mydate=$datum_ van;
      $newsplit = explode("/", $mydate);
      $dat1=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

      $mydate=$datum_ tot;
      $newsplit = explode("/", $mydate);
      $dat2=mktime(0, 0,0,$newsplit[1],$newsplit[0],$newsplit[2]);

      $datc=$dat1;
      while ($datc<=$dat2){
      $datw=date("d/m/Y",$datc);
      $jour=date ("l", $datc);
      if ($day1==$jour || $day2==$jour || $day3==$jour || $day4==$jour ||
      $day5==$jour){
      print (date("Y/m/d",$datc)."<br> ");
      }

      $sql = "INSERT INTO verlof (gebruikersID, datum, voormiddag,
      namiddag, opm_aanvrager) VALUES
      ('".$_POST['gebruikersID']."','".date( "Y-m-d",$datc)."','" .$_POST['voormiddag']."','".$_POS T['namiddag']."','".$_POS T['opm_aanvrager']."')";
      $result = mysql_query($sq l)
      or die('Unable to query database');
      echo "Uw verlof aanvraag is aangevraagd.\n" ;
      $datc=$datc + 86400;
      }
      }}

      echo "<br><input type=button value=\"Terug\"
      onClick=\"histo ry.go(-1)\"><br>";
      ?>

      Now by editing this script I finaly got the solution on how to create
      a record with the "gebruikers ID" and all the rest that was needed for
      every date between the starting date and the end date... so that
      problem seems to be solved.

      But the thing it doesn't do is leave out the dates that are in a
      weekend (like saterday and sunday) those to days should be left out...

      So i hope any of you can help me solve this problem because I'm
      getting verry confused and angry on my computer (:p)

      Anyway Tom Thackrey thanks again for your information.

      Comment

      Working...