Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2007
    Posts
    27

    Exclamation Unanswered: Return values with ADODB

    Okay how can i get a return value of 1 if the user exists in the table?

    Code:
    <?php
    require("config.php");
    if($_GET['action'] == 'post')
    {
       if(empty($_POST['Employee_ID']) || empty($_POST['Date']) || empty($_POST['Activity']) || empty($_POST['Miles']))
        {
          //if everything is not filled in than prints out error message
    	  error("blank");
    	  exit;
          }
          if(!empty($_POST['Employee_ID']))
          {
           $query="SELECT Employee_ID FROM analyzer_query WHERE Employee_ID = '{$_POST['Employee_ID']}'";
           $rs = $conn->execute($query);
           $num_columns = $rs->RecordCount();
           echo $num_columns . "<br>";
                        if($num_columns <= 0)
                            {
                              echo error ("user");
                              exit;
                            }
                            else
                            {
                            $conn->execute("INSERT INTO Activity_Log (`Employee_ID`, `Date`, `Activity`, `Miles`) VALUES ('{$_POST['Employee_ID']}', '{$_POST['Date']}', '{$_POST['Activity']}', '{$_POST['Miles']}')");
                            header("Location: success.php?action=success");
                            }
     }
    }
    function error($error)
    {
       //if error is equal to blank
       if($error == 'blank')
          {
          echo "Please fill in all the required fields before submitting";
          }
     //if error is equal to blank
       if($error == 'user')
          {
          echo "Please fill in the correct Employee ID";
          }
    }
    ?>

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Without taking too much notice of your code, and writing "off the cuff", I would say you need to do the following (pseudocode) :
    Code:
    function (
      SELECT information from database where equal information supplied
      If number of rows === 1 (exactly) then you have a valid user
      so return 1
      else
      return 0
    }
    Or alternatively
    Code:
    function {
      $sql = "SELECT count(*) as num FROM user_table WHERE info=info &&   info=info && ...";
      $result = mysql_query($sql);
      $row = mysql_fetch_assoc($result);
      if($row['num'] === 1) {
       return 1;
      }
      return 0;
    }
    Adapt these to what you need.

    p.s. i'm not quite sure what you mean by "return 1". Where are you planning on returning this value to exactly?

  3. #3
    Join Date
    Oct 2007
    Posts
    27
    umm.. i'm not using mysql though i'm using access sorry guess i forgot to mention that.

    adodb returns a value of -1 . i need to know how to get it to return a value of 1 if the user exists and i'm clueless.

  4. #4
    Join Date
    Oct 2007
    Posts
    27
    Well i want it to return a value of 1 if the user exists so it doesn't print out an error message. Basically, if the user exists i want it to return a value of 1 so the page can be redirected to another form. If they don't exist, i want a value returned of 0 so an error message appears instead.

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Apologies, i made my examples MySQL specific due to temporary brain loss.

    Ah i think i see what you're saying now. Adodb returns -1 , which you don't know how to handle?

    Here's a complete rewrite of what i perceive you to be wanting to do.
    Code:
    <?php
    // require config
    require_once("config.php");
    
    // Check empty values.
    foreach($_POST as $key=>$value){
    	if(empty($value)){
    		$empties[] = $key;
    	}
    }
    
    // If we have ANY emtpy values then we want to output errors.
    if(count($empties) > 0){
    	echo "Please fill in all required fields before submitting.<br/>";
    	echo "The following fields were emtpy:<br/>";
    	echo "<ul>";
    	foreach($empties as $empty){
    		echo "<li>".$empty."</li>";
    	}
    	echo "</ul>";
    	
    	// Stop here. NOT good programming practise but put here to demonstrate.
    	exit;
    }
    
    // Because we had NO errors thus far we can execute our query.
    $sqlString ="SELECT Employee_ID
    				FROM analyzer_query
    				WHERE Employee_ID = '%s'";
    
    $query = sprintf($sqlString,$_POST['Employee_ID']);
    
    // Execute our query.
    $rs = $conn->execute($query);
    
    // Grab the number of ROWS , NOT COLUMNS!!!
    $rows = $rs->RecordCount();
    
    // Echo number of ROWS
    echo $rows . "<br>";
    
    // If we got 1 row EXACTLY it's a valid user.
    if($rows === 1)
    {
    	// Add something into the activity log.
    	$conn->execute("INSERT INTO Activity_Log (`Employee_ID`, `Date`, `Activity`, `Miles`) VALUES ('{$_POST['Employee_ID']}', '{$_POST['Date']}', '{$_POST['Activity']}', '{$_POST['Miles']}')");
    	header("Location: success.php?action=success");
    
    } else {
    	echo "Please fill in the correct Employee ID";	
    }
    
    ?>

    The key here, the real thing you are looking for is a VALID record. The only way to find one of those is if your query returns 1 record ONLY. Otherwise you have 0 users or you're returning more than 1 user (so you have a duplicate ID in your database).
    Last edited by aschk; 11-09-07 at 07:37.

  6. #6
    Join Date
    Oct 2007
    Posts
    27
    Well I appreciate your help! Here are the errors i'm getting though

    Code:
    Notice: Undefined variable: empties in F:\InetPub\wwwroot\EmployeeWellnessProgram\testwellness.php on line 13
    
    Notice: Undefined index: Employee_ID in F:\InetPub\wwwroot\EmployeeWellnessProgram\testwellness.php on line 31
    -1
    Please fill in the correct Employee ID
    Here is my form code by the way. forgot to post it earlier

    Code:
    <html>
    <head>
    <title>Wellness Program</title>
    <script type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    function sendFocus() {
    		document.forms[0].Employee_ID.focus();
    		}
    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body onload="sendFocus()">
    <form action="testwellness.php" method="">
     <table align="center" width="75%" cellpadding="5" cellspacing="5">
            <tr>
                <td rowspan="8">
                <!-- IMAGE -->
                <img src="HeadToToeHealthcare.bmp" width="210" height="346"></td>
            </tr>
            <tr> <!-- TITLE ON TOP OF PAGE -->
                <th colspan="2">Human Resources Employee Wellness Program - Enter Mileage Form</th>
            </tr>
         <tr> <!-- Employee ID Field -->
               <td>Employee ID:</td>
               <td><input type="text" name="Employee_ID"></td>
           </tr>
           <tr> <!-- DATE Field -->
               <td>Date:</td>
               <td><input type="text" name="Date" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></td>
           </tr>
           <tr> <!-- Activities -->
               <td>Activity:</td>
               <td>
                   <select name="Activity">
                           <option value="Aerobics">Aerobics</option>
                           <option value="BackpackingHicking">Backpacking/Hicking</option>
                           <option value="Basketball">Basketball</option>
                           <option value="BikeRiding">Bike Riding</option>
                           <option value="Bowling">Bowling</option>
                           <option value="DanceClasses">Dance Classes</option>
                           <option value="ExerciseClass">Exercise Class</option>
                           <option value="Football">Football</option>
                           <option value="Golfing">Golfing</option>
                           <option value="IceSkating">Ice Skating</option>
                           <option value="MountainClimbing">Mountain Climbing</option>
                           <option value="Other">Other</option>
                           <option value="Racquetball">Racquetball</option>
                           <option value="RollerBladingRollerSkating">Roller Blading/Roller Skating</option>
                           <option value="Rowing">Rowing</option>
                           <option value="Shuffleboard">Shuffleboard</option>
                           <option value="Softball">Softball</option>
                           <option value="Swimming">Swimming</option>
                           <option value="TaiChi">Tai Chi</option>
                           <option value="Tennis">Tennis</option>
                           <option value="Volleyball">Volleyball</option>
                           <option value="WalkingRunningJogging">Walking/Running/Jogging</option>
                           <option value="Yoga">Yoga</option>
                   </select>
               </td>
           </tr>
           <tr> <!-- Miles -->
               <td>Miles:</td>
               <td><input type="text" name="Miles" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></td>
           </tr>
           <tr>
                <td colspan="2" align="center">Please review the information you have entered <b>BEFORE</b> click submit. <b>DO NOT</b> click submit until you have reviewed your information.<br>If you incorrectly put in your Employee ID, you will not receieve credit for the miles you enter.</td>
           </tr>
           <tr>
               <td colspan="2" align="center"><input type=submit value=Submit>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=reset value=Reset onClick="sendFocus()"></td>
           </tr>
    </table>
     </form>

  7. #7
    Join Date
    Oct 2007
    Posts
    27
    Okay i don't know what happened but this is what i'm getting now

    Code:
    Notice: Undefined variable: empties in F:\InetPub\wwwroot\EmployeeWellnessProgram\testwellness.php on line 11
    -1
    Please fill in the correct Employee ID
    it still appears to returning a value of -1 which i don't get but i'm still new with this. i surely wish it was with mysql and this headache could've been done and overwith when they gave me this project

  8. #8
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Firstly i'll correct the empties problem for you :
    Before the first foreach loop do
    $empties = array();

    Secondly the error message you are getting is because you aren't getting any rows back from the database. What is $_POST['Employee_ID'] ? Put an echo at the top of the page to see what this is.
    Also do echo $query after the $query variable setting part to see what your SQL statement is, and then run that through MSAccess directly to see what resultset it gives you.

  9. #9
    Join Date
    Oct 2007
    Posts
    27
    the $_POST['Employee_ID'] is what they enter into the form in the employee id text box. i've echoed it out and it's pulling the right information.

    Here is what i get when i submit the form

    Code:
    bcourtneyObject
    -1
    Please fill in the correct Employee ID
    thats with the two echo statements.

    even though i entered the correct employee id, it doesn't add to the database
    Last edited by mstng07; 11-12-07 at 10:20.

  10. #10
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    That's with both echos?
    You should have "72" <- employee id
    And "SELECT blah FROM tableblah WHERE blah blah blah".
    Or at least that's what i imagined you should have.

    What on earth is a bcourtneyObject ?

    Edit : Oh i see, two separate parts there "bcourtney" and Object. I'm guessing that bcourtney is the employee id...

  11. #11
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    And after this line :

    $query = sprintf($sqlString,$_POST['Employee_ID']);

    Are you doing :

    echo $query ?

    Because i find it hard to believe that sprintf should give you an Object

  12. #12
    Join Date
    Oct 2007
    Posts
    27
    Code:
    <?php
    require_once("config.php");
    $empties = array();
    foreach($_POST as $key=>$value){
    	if(empty($value)){
    		$empties[] = $key;
    	}
    }
    echo $_POST['Employee_ID'];
    if(count($empties) > 0){
    	echo "Please fill in all required fields before submitting.<br/>";
    	echo "The following fields were emtpy:<br/>";
    	echo "<ul>";
    	foreach($empties as $empty){
    		echo "<li>".$empty."</li>";
    	}
    	echo "</ul>";
    	exit;
    }
    $sqlString ="SELECT Employee_ID
    				FROM analyzer_query
    				WHERE Employee_ID = '%s'";
    $query = sprintf($sqlString,$_POST['Employee_ID']);
    echo $query;
    $rs = $conn->execute($query);
    echo $rs . "<br>";
    $rows = $rs->RecordCount();
    echo $rows . "<br>";
    if($rows === 1)
    {
    	$conn->execute("INSERT INTO Activity_Log (`Employee_ID`, `Date`, `Activity`, `Miles`) VALUES ('{$_POST['Employee_ID']}', '{$_POST['Date']}', '{$_POST['Activity']}', '{$_POST['Miles']}')");
    	header("Location: success.php?action=success");
    
    } else {
    	echo "Please fill in the correct Employee ID";
    }
    ?>
    Code:
    bcourtneySELECT Employee_ID FROM analyzer_query WHERE Employee_ID = 'bcourtney'Object
    -1
    Please fill in the correct Employee ID
    well here is my config file..maybe it has something to do with it? ?

    Code:
    <?php  
    $conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
    $connStr = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=F:\Wellness Program\Copy of HR Employee Wellness Program.mdb;";
    $conn->open($connStr); //Open the connection to the database
    ?>
    Edit: Yeah bcourtney is my employee id.
    Last edited by mstng07; 11-12-07 at 12:34.

  13. #13
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Ok, now take your SQL query (SELECT Employee_ID FROM analyzer_query WHERE Employee_ID = 'bcourtney') and run that directly through MSAccess (not PHP) and see what resultset it gives you...

  14. #14
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Ok lets get rid of this silly COM object.
    It's awfully bad practise to use this for something which PHP provides you with in odbc.


    config.php
    Code:
    <?php
    $connStr = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=F:\Wellness Program\Copy of HR Employee Wellness Program.mdb;";
    $username = '';
    $password = '';
    $conn = odbc_connect($connStr,$username,$password);
    ?>
    otherpage.php
    Code:
    <?php
    require_once("config.php");
    $empties = array();
    foreach($_POST as $key=>$value){
    	if(empty($value)){
    		$empties[] = $key;
    	}
    }
    echo $_POST['Employee_ID'];
    if(count($empties) > 0){
    	echo "Please fill in all required fields before submitting.<br/>";
    	echo "The following fields were emtpy:<br/>";
    	echo "<ul>";
    	foreach($empties as $empty){
    		echo "<li>".$empty."</li>";
    	}
    	echo "</ul>";
    	exit;
    }
    $sqlString ="SELECT Employee_ID
    				FROM analyzer_query
    				WHERE Employee_ID = '%s'";
    $query = sprintf($sqlString,$_POST['Employee_ID']);
    echo $query;
    
    $rs = odbc_exec($conn,$query);
    
    $rows = odbc_num_rows($rs);
    echo $rows . "<br>";
    if($rows === 1)
    {
    	odbc_exec($conn,"INSERT INTO Activity_Log (`Employee_ID`, `Date`, `Activity`, `Miles`) VALUES ('{$_POST['Employee_ID']}', '{$_POST['Date']}', '{$_POST['Activity']}', '{$_POST['Miles']}')");
    	header("Location: success.php?action=success");
    
    } else {
    	echo "Please fill in the correct Employee ID";
    }
    ?>
    Last edited by aschk; 11-13-07 at 10:52.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •