Page 1 of 1

How to PHP FORM+Database?

Posted: Sat Apr 03, 2010 11:10 am
by koski
How to connect MYSQL Databse if my form is like this?
This form computes salary(#of hrs work * daily)... the name,number of hrs work and daily rate are in 3 different tables... and is being inputed in 3 different forms...
If i enter a name which is on the database it will automatically fill the number of hours work and corresponding daily rate...
Help me pls
Name:________
Number of Hours work:_______
Daily Rate:________

Your Salary is:_______

Re: How to PHP FORM+Database?

Posted: Sat Apr 03, 2010 1:15 pm
by JakeJ
The best thing to do would be to give your best try and then post code and a more specific question if something isn't working.

Re: How to PHP FORM+Database?

Posted: Sat Apr 03, 2010 10:02 pm
by Christopher
Agreed. Research HTML forms and using PHP's $_POST superglobal. Try some code and see what you can do.

Re: How to PHP FORM+Database?

Posted: Thu Apr 08, 2010 5:30 am
by koski
How to connect this in MYSQL database?!i use the post method what others suggested

Code: Select all

<!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>
<style type="text/css">
<!--
body {
	background-color: #FFF;
}
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000;
}
.style3 {
	font-size: 16px;
	font-weight: bold;
	color: #000;
}
.style4 {
	font-size: 14px;
	font-weight: bold;
}
.style5 {
	font-size: 24px;
	font-weight: bold;
	color: #000;
}
.style6 {font-size: 14px; }
.style8 {font-size: 18px}
.style6 {
	font-weight: bold;
}
-->
</style></head>

<body> 
<form method="post">
  <h6 align="center">&nbsp;</h6>
  <br  /> 
  <div align="left"><span class="style3">Today i<span class="style8">s</span></span> 
    <span class="style8">
   
    <?php
  echo gmdate("M d Y");
  ?>
    </span></div>
  <p align="center" class="style5">Payroll Summary</p>
  
  
  <p align="center" class="style3"><span class="style4">Enter Guards Name
  
  </span>
    <input name="name" type="text" />
    <br>
    <br>
  </p>
<label></label>
  <p align="center"><span class="style4">Enter Guards Daily Rate</span>
    <input name="rate" type="text" />
    <br>
    <br></p>
  <label></label>
  <div align="center" class="style6"><span class="style6">Enter Number of Days Work</span>
    <input name="days" type="text" value="" />
  </div>
  <span class="style4">
  <label></label>
  </span>
  <span class="style6">
  <label></label>
  </span>
  <label>
  </label>
  <p align="center">

  <input name="Submit" type="Submit" value="Compute Salary" />
  </p>
  <div align="center">
    <?php
			$name=$_POST['name'];
			$rate=$_POST['rate'];
			$days = $_POST['days'];

       
  		$compute = $days * $rate;
    echo "<p> <h2>Employee's Name :  $name. </h2></p>";
	    echo "<p><h2> Your Salary :  $compute. </h2> </p>";

?>			
    
  </div>
  <p>&nbsp;</p>
 <p>&nbsp;</p>
 <p>&nbsp;</p>
  <p>&nbsp; </p>
</form>
</body>
</html>

Re: How to PHP FORM+Database?

Posted: Thu Apr 08, 2010 7:25 am
by ell0bo
Alright, your above code looks good, but now you're going to need to look at the mysql connector.

Let me direct you here: http://us2.php.net/manual/en/mysqli.query.php

Now one little change about your code...

Code: Select all

<div align="center">
    <?php
                        $name=$_POST['name'];
                        $rate=$_POST['rate'];
                        $days = $_POST['days'];

       
                $compute = $days * $rate;
    echo "<p> <h2>Employee's Name :  $name. </h2></p>";
            echo "<p><h2> Your Salary :  $compute. </h2> </p>";

?>                     
</div>
I would change to ...

Code: Select all

<div align="center">
    <?php
    if ( !empty($_POST) ){
            $name=$_POST['name'];
            $rate=$_POST['rate'];
                        $days = $_POST['days'];

       
            $compute = $days * $rate;
            echo "<p> <h2>Employee's Name :  $name. </h2></p>";
            echo "<p><h2> Your Salary :  $compute. </h2> </p>";
     }
?>                     
</div>
This way your code will only run when something has been POSTed to your page... aka your form has been submitted.