Inserting form array data into MySQL with PHP -


i new both php , mysql , appreciate one.

what trying achieve: store time sheet mysql table using form below should post each day's data separate row while keeping same employee name each day entered. user has option add additional days form -- max of 7. i've tested without use of arrays , able store data table without problems.

html:

<form id="timesheet" method="post" action="timekeep.php">         <fieldset>         <h1>employee info</h1>         <ul>             <li>                 <label>first name:</label>                 <input name="firstname" type="text">             </li>             <li>                 <label>last name:</label>                 <input name="lastname" type="text">             </li>         </ul>         </fieldset>         <fieldset>         <h1>time info</h1>             <h3>day: 1</h3>             <ul>                 <li>                     <input name="date[]" type="text">                 </li>                 <li>                     <input name="straighthours[]" type="number">                 </li>                 <li>                     <input name="overtimehours[]" type="number">                 </li>                 <li>                     <input name="premiumhours[]" type="number">                 </li>                 <li>                     <input name="perdiem[]" type="number">                 </li>             </ul>             <h3>day: 2</h3>             <ul>                 <li>                     <input name="date[]" type="text">                 </li>                 <li>                     <input name="straighthours[]" type="number">                 </li>                 <li>                     <input name="overtimehours[]" type="number">                 </li>                 <li>                     <input name="premiumhours[]" type="number">                 </li>                 <li>                     <input name="perdiem[]" type="number">                 </li>             </ul>          </fieldset>          <input id="submit" name="submit-time" type="submit" value="submit time">      </form> 

php:

$sql_connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("unable connect database! please try again later.");  mysql_select_db($dbuser, $sql_connection);  $sql = "insert table (             date,             firstname,             lastname,             straighthours,             overtimehours,             premiumhours,             totalhours,             perdiem         )         values (".             prepsql($date) . ", " .             prepsql($firstname) . ", " .             prepsql($lastname) . ", " .             prepsql($straighthours) . ", " .             prepsql($overtimehours) . ", " .             prepsql($premiumhours) . ", " .             prepsql($totalhours) . ", " .             prepsql($perdiem) . "         )";  mysql_query($sql, $sql_connection);  mysql_close($sql_connection);  function prepsql($value) {      if(get_magic_quotes_gpc())     {         $value = stripslashes($value);     }      $value = "'" . mysql_real_escape_string($value) . "'";      return($value); } 

using pdo object make easier, mysql_ legacy anyway:

$db = new pdo($hostname,$username,$password);   $qry = "insert table (             date,             firstname,             lastname,             straighthours,             overtimehours,             premiumhours,             totalhours,             perdiem         )         values (:date, :firstname, :lastname, :straighthours, :overtimehours, :premiumhours, :totalhours, :perdiem)"; // colon variables bound actual variable  $statement = $db->prepare($query); //prevents injection  // binds variables place holder in query $statement->bindvalue(':firstname', $firstname); $statement->bindvalue(':lastname', $lastname); $statement->bindvalue(':straighthours', $straighthours); $statement->bindvalue(':overtimehours', $overtimehours); $statement->bindvalue(':premiumhours', $premiumhours); $statement->bindvalue(':totalhours', $totalhours); $statement->bindvalue(':perdiem', $perdiem);  $statement->execute(); $statement->closecursor(); 

you can further input checking php before passing sql via:

trim(strip_tags(htmlentities($firstname))); 

pdo lot simpler use , understand imo

update:

tutorials on pdo

update #2:

for added functionality arrays per day can do:

<input type="text" name="firstname1">  // fields $workingday1 = array(); $workingday1['firstname'] = $_get['firstname1']; // etc. other fields 

then can access field by:

$workingday1 = $_get['workingday1']; // or post or want pass $firstname = $workingday['firstname']; 

after can prune database like. can have single table values , edit selects display employee or day or w/e. can have table each employee , grab tables , display data how ever like.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -