here is my sample code:
addqtr.php
<?php
$quarter=$_POST['quarter'];
$sql="Select * from $quarter where id=?";
$res=$db->prepare($sql);
$res->execute(array($pqid));
while($rowadd = $res->fetch(PDO::FETCH_ASSOC)){
$num=$rowadd['id'];
$pushup=$rowadd['pushup'];
$situp=$rowadd['situp'];
}
?> <form method="post" name="frmQuarter" action="saveqtr.php"> <input type="hidden" name="qid" value="<?php echo $pqid; ?>"/> <table> <tr> <td>Select Quarter</td><td>:</td> <td> <select id="quarter" name="quarter"> <option selected></option> <option value="1qtr">1st</option> <option value="2qtr">2nd</option> <option value="3qtr">3rd</option> <option value="4qtr">4th</option> </select> </td> </tr> <tr> <td>Push Ups</td><td>:</td><td><input type="text" name="pushup" required="required"
value="<?php echo $pushup; ?>"/></td> </tr> <tr> <td>Sit Ups</td><td>:</td><td><input type="text" name="situp" required="required"
value="<?php echo $situp; ?>"/></td> </tr> <tr><td></td><td></td><td><input type="submit" class="myButton" value="Save"/></td></tr> </table>
saveqtr.php
<?php
include ("dbconn.php");
if (isset($_POST['quarter'])){
$select = $_POST['quarter'];
$pushup=$_POST['pushup'];
$situp=$_POST['situp'];
$id=$_POST['qid'];
if($id==null){
if($select=="1qtr"){
$sql="INSERT INTO 1qtr(pushup,situp)values(:pushup,:situp)";
$qry=$db->prepare($sql);
$qry->execute(array(':pushup'=>$pushup,':situp'=>$situp));
}
elseif($select=="2qtr"){
$sql="INSERT INTO 2qtr(pushup,situp)values(:pushup,:situp)";
$qry=$db->prepare($sql);
$qry->execute(array(':pushup'=>$pushup,':situp'=>$situp));
}
elseif($select=="3qtr"){
$sql="INSERT INTO 3qtr(pushup,situp)values(:pushup,:situp)";
$qry=$db->prepare($sql);
$qry->execute(array(':pushup'=>$pushup,':situp'=>$situp));
}
elseif($slect=="quater"){
$sql="INSERT INTO 4qtr(pushup,situp)values(:pushup,:situp)";
$qry=$db->prepare($sql);
$qry->execute(array(':pushup'=>$pushup,':situp'=>$situp));
}}
else{
if($select=="1qtr"){
$sql="UPDATE 1qtr SET pushp=?, situp=?, where id=?";
$qry=$db->prepare($sql);
$qry->execute(array($pushup,$situp,$id));
}
elseif($select=="2qtr"){
$sql="UPDATE 2qtr SET pushp=?, situp=?, where id=?";
$qry=$db->prepare($sql);
$qry->execute(array($pushup,$situp,$id));
}
elseif($select=="3qtr"){
$sql="UPDATE 3qtr SET pushp=?, situp=?, where id=?";
$qry=$db->prepare($sql);
$qry->execute(array($pushup,$situp,$id));
}
elseif($slect=="4qtr"){
$sql="UPDATE 4qtr SET pushp=?, situp=?, where id=?";
$qry=$db->prepare($sql);
$qry->execute(array($pushup,$situp,$id));
}}
}
?>