Creating a new employee php check plz

Discussion in 'PHP' started by lounar, Nov 13, 2007.

  1. #1
    the problem is that everytime i try to make a new employee i get in phpmyadmin the date as 0000-00-00 and the salary or depid as thhe year
    here is the code plz post any ideas
    
    <?php
    
    
    
    /*********************************************************************************
    
     *																				 *
    
     *  This PHP file is  the file that is  used when the administrator selects the  *
    
     *	create a new member option. When this file is called a new form appears		 *
    
     *	from where the administrator can fill all the required information regarding *
    
     *	the particular user that needs to be created. There are several validaions	 *
    
     *	for all the fields that exist and if once pressing the create button, all	 *
    
     *	these validations are met successfully a sql query will be performed that	 *
    
     *	will insert the new user into the database.									 *
    
     *																				 *
    
     *********************************************************************************/
    
    
    
    session_start();
    
    
    
    if ($_POST['verifySubmit'] == "yes"){
    
    	
    
    	include ('inc/db_open.php');
    	
    
    	
    
    	$sql = "SELECT * FROM employees";
    
    	$result = @mysql_query($sql) or die('Query failed '.mysql_error());
    
    	$row=mysql_fetch_array($result);	
    
    		
    
    	
    
    	$emplastname = $_POST['emplastname'];
    	$empfirstname = $_POST['empfirstname'];
    	
    	$empdepid = $_POST['empdepid'];
    	$empsalary = $_POST['empsalary'];
    	$empphonenumber = $_POST['empphonenumber'];
    	
    	
        $day = $_POST['day'];
    
    	$month = $_POST['month'];
    
    	$year = $_POST['year'];
    
    	$emphiredate = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['date'];
    
    	
    
    	
    	
    
    	?>
    <?php
    	
    	
    
    	
    
    	
    
    	
    
    		// Insert the user to the Database.
    
    		$request = "INSERT INTO employees VALUES(NULL,'$emplastname','$empfirstname','$empsalary','$empphonenumber','$emphiredate','$empdepid')";
    
    		$insertResult = mysql_query($request) or die ('Query failed'.mysql_error());
    
    		
    
    		
    
    
    }
    
    
    		
    
    	
    
    
    
    include ('inc/validation.php');
    
    
    
    
    
    ?>
    
    
    <script type="text/javascript" language="javascript">
    
    
    function validate(){
    
    	var error_msg = 0;
    
    	if(document.getElementById('emplastname').value == ""){
    	
    	document.getElementById('h1').style.visibility='visible';
    
    	error_msg = 1;
    
    	}
    
    	
    	if(error_msg == 0){
    
    	document.createEmployee.submit();
    
    	}
    
    }
    
    
    </script>
    
    
    
    
    
    <table border="0" width="800" align="center">
    
      <tr>
    
        <td width="1%" align="left" valign="top">
    
    
    
    	</td>
    
        <td align="center" valign="top">
    
    		<center>
    
    		<FIELDSET>
    
    			<LEGEND>Create Employee</LEGEND>
    
    			
    
    			<form name="createEmployee" id="createEmployee" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
    
    			
    
    				<table width="70%" align="center" cellspacing="2" cellpadding="4">
    
    				  <tr>
    
    					<td class="eventLeftSide">Lastname:</td>
    
    					<td class="eventRightSide"><input type="text" id="emplastname" name="emplastname" /></td>
    
    					<td id='h1' style="visibility:hidden"><font color="#FF0000">Required</font></td>
    
    				  </tr>
    
    				  <tr>
    
    					<td class="eventLeftSide">Firstname:</td>
    
    					<td class="eventRightSide"><input type="text" id="empfirstname" name="empfirstname" /></td>
    
    					<td id='h2' style="visibility:hidden"><font color="#FF0000">Required</font></td>
    
    				  </tr>
    
    				 	 <tr>
    
    				  <td class="eventLeftSide">
    
    				  	Date of Hire:
    
    				  </td>
    				  <td class="eventRightSide">
    
    				  <select name='day' id="day">
    
    					<option selected='selected' value=''>-Day-</option>	
    
    					<?php			
    
    					for($counter = 1; $counter <= 31; $counter++){
    
    						if($counter >=1 && $counter <=9){
    
    							echo "<option value=0" . $counter . ">0" . $counter . "</option>";
    
    						}
    
    						else{
    
    							echo "<option value=" . $counter . ">" . $counter . "</option>";
    
    						}
    
    					}
    
    					?>
    
    				</select>
    				  <select name='month' id="month">					
    
    					<option selected="selected" value="">--Month--</option>
    
    					<option value=01>January</option>
    
    					<option value=02>February</option>
    
    					<option value=03>March</option>
    
    					<option value=04>April</option>
    
    					<option value=05>May</option>
    
    					<option value=06>June</option>
    
    					<option value=07>July</option>
    
    					<option value=08>August</option>
    
    					<option value=09>September</option>
    
    					<option value=10>October</option>
    
    					<option value=11>November</option>
    
    					<option value=12>December</option>
    
    				</select>
    				  <select name='year' id="year">
    
    					<option selected='selected' value=''>--Year--</option>
    
    					<?php
    
    					for($counter = 1920; $counter <= 2007; $counter++){
    
    										echo "<option value=" . $counter . ">" . $counter . "</option>";
    
    					}
    
    					?>
    
    				</select>
    
    				  </td>
    
    				 	<td align="center" colspan="3" id='h3' style="visibility:hidden"><font color="#FF0000">Incorrect Date</font></td>
    
    				  </tr>
    
                      <tr>
    
    					<td class="eventLeftSide">Salary:</td>
    
    					<td class="eventRightSide"><input type="text" id="empsalary" name="empsalary" /></td>
    
    					<td id='h4' style="visibility:hidden"><font color="#FF0000">Required</font></td>
    
    				  </tr>
    				  <tr>
    
    					<td class="eventLeftSide">Phonenumber:</td>
    
    					<td class="eventRightSide"><input type="text" id="empphonenumber" name="empphonenumber" /></td>
    
    					<td id='h5' style="visibility:hidden"><font color="#FF0000">Required</font></td>
    
    				  </tr>
    				  <tr>
    
    					<td class="eventLeftSide">Department:</td>
    
    					<td class="eventRightSide">
    
    						<input type="radio" id="radio1" name="empdepid" value = "1" />Reception <br />
                            <input type="radio" id="radio2" name="empdepid" value = "2" />Cusine <br />
    						<input type="radio" id="radio3" name="empdepid" value = "3" />Cleaning <br />
    						<input type="radio" id="radio4" name="empdepid" value = "4" />Logistics <br />
    						<input type="radio" id="radio5" name="empdepid" value = "5" />Security <br />
    						<input type="radio" id="radio6" name="empdepid" value = "6" />Room Service <br />
    					
    						<input type="radio" id="radio7" name="empdepid" value = "7" />Other Undefined
    
    					</td>
    
    					<td id='h3' style="visibility:hidden"><font color="#FF0000">Required</font></td>
    
    				  </tr>
    
    				  
    				  <tr>
    
    				  <td colspan="2" class="eventHeadingText">
    
    				  <input type="hidden" name="verifySubmit" value="yes">
    
    				  <input type="button" name="submitButton" value='Create' onClick="validate()"/>
    
    				  <input type="button" value="Back" onClick="history.back()">
    
    				  </td>
    
    				  </tr>
    
    				</table>
    
    
    
    			</form>
    
    		</FIELDSET>
    
    		</center>
    
    	</td>
    
      </tr>
    
    </table>
    
    </body>
    
    </html>
    
    
    
    PHP:

    and here is the sql table

    CREATE TABLE `employees` (
    `empid` int(11) NOT NULL auto_increment,
    `emplastname` char(25) character set utf8 NOT NULL,
    `empfirstname` char(25) character set utf8 NOT NULL,
    `emphiredate` date NOT NULL,
    `empdepid` int(11) default NULL,
    `empsalary` float(6,2) NOT NULL,
    `empphonenumber` text character set latin1 collate latin1_general_ci,
    PRIMARY KEY (`empid`),
    KEY `idepid` (`empdepid`)
    )
     
    lounar, Nov 13, 2007 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think that the problem is this line:
    $request = "INSERT INTO employees VALUES(NULL,'$emplastname','$empfirstname','$empsalary','$empphonenumber','$emphiredate','$empdepid')";

    From your CREATE TABLE it looks like the columns are out of order.. explaining why information winds up in the wrong place. One fix might be to replace that line with something like:

    $request = "INSERT INTO employees VALUES(NULL,'$emplastname','$empfirstname','$emphiredate','$empdepid','$empsalary','$empphonenumber')";

    Or maybe better yet do something like below so that the column order you created the table with doesn't matter:

    $request = "INSERT INTO employees (emplastname,empfirstname,emphiredate,empdepid,empsalary,empphonenumber) VALUES('$emplastname','$empfirstname','$emphiredate','$empdepid','$empsalary','$empphonenumber')";

    Hope this helps!
     
    drunnells, Nov 13, 2007 IP
  3. lounar

    lounar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thnx for the help the issue with salary and depid is ok but the date is still appears in phpmyadmin as 0000-00-00!
    i keep on searching and w8ing for ideas!
     
    lounar, Nov 14, 2007 IP
  4. lounar

    lounar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    basically if anyone wants to help pl focus on this part of code which the reason i dont get the right date :/

    $year = $_POST['year'];


    $month = $_POST['month'];


    $day = $_POST['date'];
    $emphiredate = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['date'];


     <td class="eventRightSide">
    
    				  <select name='day' id="day">
    
    					<option selected='selected' value=''>-Day-</option>	
    
    					<?php			
    
    					for($counter = 1; $counter <= 31; $counter++){
    
    						if($counter >=1 && $counter <=9){
    
    							echo "<option value=0" . $counter . ">0" . $counter . "</option>";
    
    						}
    
    						else{
    
    							echo "<option value=" . $counter . ">" . $counter . "</option>";
    
    						}
    
    					}
    
    					?>
    
    				</select>
    				  <select name='month' id="month">					
    
    					<option selected="selected" value="">--Month--</option>
    
    					<option value=01>January</option>
    
    					<option value=02>February</option>
    
    					<option value=03>March</option>
    
    					<option value=04>April</option>
    
    					<option value=05>May</option>
    
    					<option value=06>June</option>
    
    					<option value=07>July</option>
    
    					<option value=08>August</option>
    
    					<option value=09>September</option>
    
    					<option value=10>October</option>
    
    					<option value=11>November</option>
    
    					<option value=12>December</option>
    
    				</select>
    				  <select name='year' id="year">
    
    					<option selected='selected' value=''>--Year--</option>
    
    					<?php
    
    					for($counter = 1920; $counter <= 2007; $counter++){
    
    										echo "<option value=" . $counter . ">" . $counter . "</option>";
    
    					}
    
    					?>
    
    				</select>
    
    				  </td>
    
    				 	<td align="center" colspan="3" id='h3' style="visibility:hidden"><font color="#FF0000">Incorrect Date</font></td>
    
    				  </tr>
    PHP:
     
    lounar, Nov 14, 2007 IP
  5. lounar

    lounar Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok guys this is it i am a little noob so i had to take a hundred of views in code to see the day and date difference!
    thnx even for viwing my post!

    solved
     
    lounar, Nov 14, 2007 IP