0

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));
    }}
}
?> 
3
Contributors
3
Replies
26
Views
1 Year
Discussion Span
Last Post by Raven_1
2

Since you check the select value against your table name, you could do a more general check and just insert the value into your query.

BTW, your update queries will fail, because of the comma before the where clause.

1

There are countless better ways , but all have to do with what is your definition of better. E.g. you have logic in your template file , your SQL queries are all over the place with no grouping in their class scope or even in separate files , you mix OOP with procedural (and maybe there is also functional in this). You use prepared statements with normal parameterized query with question marks in some cases in others you use the PHP implementation of C++ binding form e.g. :pushup , (although question marks make a lot more sense to me) you can follow any rule you like but decide what rule and stick to it. Of course you can write code like that and be very fine in your own PHP idiom mix , i am not judging it but rather explain you thtat the “better” have to do with what you are doing , who are you as a programmer and who you want to be in the near future.

0

thank you for the responds.. :) am such a newbie about programming but i understand some basic like html.. (exploring) am relaying on the books and some suggestions..
In that statement i just want to control the table name using a combobox/dropdownbox i used $_GET for isset and $_POST for $select

1qtr.php

<?php
    $sql="Select * from 1qtr where id=?";
    $res=$db->prepare($sql);
    $res->execute(array($ppid));
        while($rowadd = $res->fetch(PDO::FETCH_ASSOC)){
        $num=$rowadd['id'];
        $psno=$rowadd['sno'];
        $ppush=$rowadd['pushup'];
        $psit=$rowadd['situp'];
        $prun=$rowadd['run'];
        $prema=$rowadd['rema'];     
    }
    ?>

    <form method="post" name="frmQuarter" action="<?php echo"saveqtr.php";?>">
    <input type="hidden" name="pid" value="<?php echo $ppid; ?>"/>
    <input type="hidden" name="selectqtr" id="selectqtr" value="1qtr"/>
        <center><h3>1st Quarter</h3></center>
        <table>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <table width="auto" height="auto">
        <tr>
            <thead style="font-weight:bold">
            <td>EVENT</td>
            <td></td>
            <td>SCORE</td>
            <td></td>
            <td>RATING</td>
            </thead>
        </tr><tr></tr>

            <tr>
                <td>Push Ups</td><td>:</td>
                <td><input type="text" name="tpushup" required="required" value="<?php echo $ppush; ?>" style="width:50px; text-align:center;"/></td>
                <td>=</td>
                <td><input type="text" name="res1" style="width:50px; text-align:center;" value="<?php echo $pushres;?>"/></td>
            </tr>
            <tr>
                <td>Sit Ups</td><td>:</td>
                <td><input type="text" name="tsitup" required="required" value="<?php echo $psit; ?>" style="width:50px; text-align:center;"/></td>
                <td>=</td>
                <td><input type="text" name="res2" style="width:50px; text-align:center;" value="<?php echo $sitres;?>"/></td>
            </tr>
            <tr>
                <td style="width:75px">Mile Run</td><td>:</td>
                <td><input type="text" name="trun" required="required" value="<?php echo $prun; ?>" style="width:50px; text-align:center;"/></td>
                <td>=</td>
                <td><input type="text" name="res3" style="width:50px; text-align:center;" value="<?php echo $runres;?>"/></td>
            </tr>
            </table>
            <table>
            <tr>
                <td>Remarks</td><td>:</td>
                <td><input type="text" name="rema" value="<?php echo $prema; ?>" style="text-align:center;"/></td>
            </tr>
           <tr><td></td><td></td><td><input type="submit" name="update" value="Edit" id="myButton"/></td></tr>
        </table>
    </form>

instead of combobox i only used a textbox.. but the thing is i made a 4 pages for quarters..

saveqtr.php

<?php

if(isset($_GET['selectqtr']));
    error_reporting(0);
    include ("dbconn.php");
    $select=$_POST['selectqtr'];
    $pushup=$_POST['tpushup'];
    $situp=$_POST['tsitup'];
    $run=$_POST['trun'];
    $id=$_POST['pid'];


    if($id==null){
        $sql="INSERT INTO $select(pushup,situp,run)values(:pushup,:situp,:run)";
            $qry=$db->prepare($sql);
            $qry->execute(array(':pushup'=>$pushup,':situp'=>$situp,':run'=>$run));
    }

    else{
    $sql="UPDATE $select SET  pushup=?, situp=?, run=? where id=?";
        $qry=$db->prepare($sql);
            $qry->execute(array($pushup,$situp,$run,$id));}

    echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
    echo "<script language='javascript' type='text/javascript'>window.open('viewall.php?ppid=$id','_self')</script>";
    ?>
</head>
</html>

then i include them in one page.. ahahah such a trial and error thing..
what i want is i'd like to make a one page for the quarters then the manipulator for the table is a combobox..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.