Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-27-2011, 08:50 PM   PM User | #1
dlep2x2
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
dlep2x2 is an unknown quantity at this point
Exclamation Problem with inserting data into database

hi! i need some help with my php code. Here is the part of my php code that doesn't work:
PHP Code:
elseif ($_GET["k"]==4)
                {
                        
session_start();
                        if (isset(
$_SESSION["uname"]))
                        {
                                
$anoSulod ="Payroll administration<hr>";
                                
$anoSulod.="<form method='post' action='logged.php?k=41'>";
                                
$anoSulod.="<table border='0'>";
                                
$anoSulod.="    <tr>";
                                
$anoSulod.="            <td><input type='submit' value='Update Payroll Details' name='sButton'></td>";
                                
$anoSulod.="    </tr>";
                                
$anoSulod.="</table>";
                                
$anoSulod.="<table border='1'>";
                                
$anoSulod.="    <tr>";
                                
$anoSulod.="            <td>&nbsp;</td>";
                                
$anoSulod.="            <td>Employee No</td>";
                                
$anoSulod.="            <td>Employee name</td>";
                                
$anoSulod.="            <td>Position</td>";
                                
$anoSulod.="    </tr>";
 
 
                                
$k="select * from emp order by empno";
                                
$q=mysql_query($k);
                                if (
mysql_num_rows($q)>0)
                                {
                                        while (
$akonRows=mysql_fetch_array($q))
                                        {
                                                
$anoSulod.="    <tr>";
                                                
$anoSulod.="            <td><input type='radio' name='empID' value='".$akonRows["empID"]."'></td>";
                                                
$anoSulod.="            <td>".$akonRows["empno"]."</td>";
                                                
$anoSulod.="            <td>".$akonRows["empname"]."</td>";
                                                
$anoSulod.="            <td>".$akonRows["emppos"]."</td>";
                                                
$anoSulod.="    </tr>";
                                        }
                                }
                                else
                                {
                                                
$anoSulod.="    <tr>";
                                                
$anoSulod.="            <td colspan='6' align='center'>No records found</td>";
                                                
$anoSulod.="    </tr>";
                                }
 
 
                                
$anoSulod.="</table>";
                                
$anoSulod.="</form>";
                                
$kadamoTD=2;
                        }
                        else 
$kadamoTD=0;
                }
                elseif (
$_GET["k"]==41)
                {
                        
session_start();
                        if (isset(
$_SESSION["uname"]))
                        {
                                
$empID=$_POST["empID"];
                                
$aBulan=0$aPetsa=0$aTuig=0$tuigYana=date("Y"); $tuigHadto=$tuigYana-5;
                                
$anoBulan=array("January","February","March","April","May","June","July","August","September","October","November","December");
 
 
                                if (
$_POST["sButton"]=="Update Payroll Details")
                                {
                                        
$anoSulod ="Payroll Details<hr>";
                                        
$anoSulod.="<form method='post' action='logged.php?k=42'>";
                                        
$anoSulod.="<input type='hidden' name='empID' value='".$empID."'>";
                                        
$anoSulod.="<table>";
                                        
$anoSulod.="    <tr><td>Name of employee : </td><td>".accessDatabase::hinoEmployee($empID)."</td></tr>";
                                        
$anoSulod.="    <tr>";
                                        
$anoSulod.="            <td>For the month ending : </td>";
                                        
$anoSulod.="            <td>";

        
// Kanan month ini dinhi
                                        
$anoSulod.="                    <select name='bBulan'>";
                                        for (
$bulan=1$bulan<=12$bulan++)
                                        {
                                            if (
$bulan==$aBulan$anoSulod.="       <option value='".$bulan."' selected>".$anoBulan[$bulan-1];
                                            else 
$anoSulod.="       <option value='".$bulan."'>".$anoBulan[$bulan-1];
                                        }
                                        
$anoSulod.="                    </select>&nbsp;";
 
 
        
// Kanan year ini dinhi
                                        
$anoSulod.="                    <select name='bTuig'>";
                                        for (
$tuig=$tuigYana$tuig>=$tuigHadto$tuig--)
                                        {
                                            if (
$tuig==$aTuig$anoSulod.=" <option value='".$tuig."' selected>".$tuig;
                                            else 
$anoSulod.="       <option value='".$tuig."'>".$tuig;
                                        }
                                        
$anoSulod.="                    </select>";
                                        
$anoSulod.="            </td>";
                                        
$anoSulod.="    <tr>";
                                        
$anoSulod.="    <tr><td>Basic Salary : PhP</td><td><input type='text' name='bs'></td></tr>";
                                        
$anoSulod.="    <tr><td>PERA/ACA : PhP</td><td><input type='text' name='pa'></td></tr>";
                                        
$anoSulod.="    <tr><td>PAIC : PhP</td><td><input type='text' name='pc'></td></tr>";
                                        
$anoSulod.="    <tr><td>REL : PhP</td><td><input type='text' name='rl'></td></tr>";
                                        
$anoSulod.="    <tr><td>GSIS Premium/Loans : PhP</td><td><input type='text' name='gs'></td></tr>";
                                        
$anoSulod.="    <tr><td>HDMF Premium/Loans : PhP</td><td><input type='text' name='hd'></td></tr>";
                                        
$anoSulod.="    <tr><td>Coop Loans : PhP</td><td><input type='text' name='cl'></td></tr>";
                                        
$anoSulod.="    <tr><td>Union Dues : PhP</td><td><input type='text' name='ud'></td></tr>";
                                        
$anoSulod.="    <tr><td>Withholding Tax : PhP</td><td><input type='text' name='tx'></td></tr>";
                                        
$anoSulod.="    <tr><td colspan='2'><hr></tr>";
                                        
$anoSulod.="    <tr><td colspan='2'><input type='submit' value='Save'></tr>";
                                        
$anoSulod.="    <tr><td colspan='2'><hr></tr>";
                                        
$anoSulod.="    <tr>";
                                        
$anoSulod.="     <td colspan='2'>";
                                        
$anoSulod.="     <table border='1'>";
                                        
                                        
$anoSulod.="     <caption>Payroll Details</caption>";
                                        
$anoSulod.="     <tr>";
                                        
$anoSulod.="     <td>&nbsp;</td>";
                                        
$anoSulod.="     <td>Pay period</td>";
                                        
$anoSulod.="     <td>Basic Salary</td>";
                                        
$anoSulod.="     <td>PERA/ACA</td>";
                                        
$anoSulod.="     <td>PAIC</td>";
                                        
$anoSulod.="     <td>REL</td>";
                                        
$anoSulod.="     <td>GSIS Premium/Loans</td>";
                                        
$anoSulod.="     <td>HDMF Premium/Loans</td>";
                                        
$anoSulod.="     <td>Coop Loans</td>";
                                        
$anoSulod.="     <td>Union Dues</td>";
                                        
$anoSulod.="     <td>Withholding Tax</td>";
                                        
$anoSulod.="     </tr>";

                                        
$k="select b.payID, a.empname, date_format(b.date,'%M %Y') as date, b.bs, b.pa, b.pc, b.rl, b.gs, b.hd, b.cl, b.ud, b.tx, from emp a, pay b where a.empID=b.empID and a.empID=".$empID;
                                        
$q=mysql_query($k);
                                        if (
mysql_num_rows($q)>0)
                                        {
                                            while (
$akonRows=mysql_fetch_array($q))
                                            {
                                                
$anoSulod.="    <tr>";
                                                
$anoSulod.="     <td><input type='radio' name='payID' value='".$akonRows["payID"]."'></td>";
                                                
$anoSulod.="     <td>".$akonRows["date"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["bs"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["pa"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["pc"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["rl"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["gs"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["hd"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["cl"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["ud"]."</td>";
                                                
$anoSulod.="     <td>".$akonRows["tx"]."</td>";
                                                
$anoSulod.="    </tr>";
                                            }
                                        }
                                        else
                                        {
                                            
$anoSulod.="    <tr>";
                                            
$anoSulod.="     <td colspan='11' align='center'>No records found</td>";
                                            
$anoSulod.="    </tr>";
                                        }


                                        
$anoSulod.="     </table>";

                                        
$anoSulod.="     </td>";
                                        
$anoSulod.="    </tr>";
                                        
$anoSulod.="</table>";
                                        
$anoSulod.="</form>";
                                        
$kadamoTD=2;
                                                    }


                                            }
                }
                elseif (
$_GET["k"]==42)
                {
                        
session_start();
                        if (isset(
$_SESSION["uname"]))
                        {
                                
$k="insert pay set empID=".$empID.", bs=".$bs.", pa=".$pa.", pc=".$pc.", rl=".$rl.", gs=".$gs.", hd=".$hd.", cl=".$cl.", ud=".$ud.", tx=".$tx.", date='".$date."'";
                                
$q=mysql_query($k);
 
                                
$empID=$_POST["empID"];
                                
$bs=$_POST["bs"];
                                
$pa=$_POST["pa"];
                                
$pc=$_POST["pc"];
                                
$rl=$_POST["rl"];
                                
$gs=$_POST["gs"];
                                
$hd=$_POST["hd"];
                                
$cl=$_POST["cl"];
                                
$ud=$_POST["ud"];
                                
$tx=$_POST["tx"];
                                
$bBulan=$_POST["bBulan"];
                                
$bTuig=$_POST["bTuig"];
                                
$date=$bTuig."/01/".$bBulan;
 
 
                                
 
 
                                
$anoSulod="<script>window.location.href='logged.php?k=4';</script>";
                                
$kadamoTD=2;
                        }
                        else 
$kadamoTD=0;
                }
                elseif (
$_GET["k"]==3)
                {
                        
session_start();
                        if (isset(
$_SESSION["uname"]))
                        {
                                
$anoSulod ="Employees registry<hr>";
                                
$anoSulod.="<form method='post' action='logged.php?k=5'>";
                                
$anoSulod.="<table>";
                                
$anoSulod.="    <tr><td>Employee No : </td><td><input type='text' name='empno'></td></tr>";
                                
$anoSulod.="    <tr><td>Employee Name : </td><td><input type='text' name='empname'></td></tr>";
                                
$anoSulod.="    <tr><td>Position : </td><td><input type='radio' name='emppos'></td></tr>";
                                
$anoSulod.="    <tr><td colspan='2'><hr></tr>";
                                
$anoSulod.="    <tr><td colspan='2'><input type='submit' value='Save'></tr>";
                                
$anoSulod.="</table>";
                                
$anoSulod.="</form>";
                                
$kadamoTD=2;
                        }
                        else 
$kadamoTD=0;
                }
                elseif (
$_GET["k"]==4)
                {
                        
session_start();
                        if (isset(
$_SESSION["uname"]))
                        {
                                
$anoSulod="Employees update<p><p>";
                                
$kadamoTD=2;
                        }
                        else 
$kadamoTD=0;
                }
        }
        else
        {
                
$anoSulod="<form method='post' action='logged.php?k=1'>";
                
$anoSulod.="<table class='t' bgcolor='gray'>";
                
$anoSulod.="    <tr><td align='center' colspan='2' bgcolor='lightgray'>Sign-in</tr>";
                
$anoSulod.="    <tr><td>Username : </td><td><input type='text' name='u'></td></tr>";
                
$anoSulod.="    <tr><td>Password : </td><td><input type='password' name='p'></td></tr>";
                
$anoSulod.="    <tr><td colspan='2'><hr></tr>";
                
$anoSulod.="    <tr><td colspan='2' align='right'><input type='submit' value='Login'></tr>";
                
$anoSulod.="</table>";
                
$anoSulod.="</form>";
                
$kadamoTD=1;
        }
 
 
        if (
$kadamoTD==1)
        {
                
$d="            <tr>";
                
$d.="                   <td colspan='2' valign='top'>".$anoSulod."</td>";
                
$d.="           </tr>";
        }
        elseif (
$kadamoTD==2)
        {
                
akonScreen::akonMenu();
                
$d="            <tr>";
                
$d.="                   <td valign='top'>".$anoSulod."</td>";
                
$d.="           </tr>";
        }
        elseif (
$kadamoTD==0)
        {
                
$anoSulod=security::performBackDoor();
                
$d="            <tr>";
                
$d.="                   <td colspan='2' valign='top'>".$anoSulod."</td>";
                
$d.="           </tr>";
        } 
The tables I am using in my database are emp and pay. The emp table has these rows: empID, empno, empname, emppos. While the pay table has the following: payID, empID, date, bs, pa, pc, rl, gs, hd, cl, ud, tx.

Its supposed to allow me to add pay details into the database (pay table) according to the empID selected... But when I try to add some pay details, it wont go into the database.

Please do help! Thanks!
dlep2x2 is offline   Reply With Quote
Old 05-27-2011, 09:40 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
This is why you should be checking for errors after calling mysql_query(). You're shooting blind after being spun around 3 times. Kind of hard to hit the target.

To take the blindfold off, check the return value of mysql_query:

PHP Code:
$result mysql_query($query);
if (!
$result)
{
    die(
"MySQL Error! Query is $query<br />Error is ".mysql_error());

__________________
Fumigator is offline   Reply With Quote
Old 05-27-2011, 09:41 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Your code ordering is bass-ackwards!!

Code:
                                $k="insert pay set empID=".$empID.", bs=".$bs.", pa=".$pa.", pc=".$pc.", rl=".$rl.", gs=".$gs.", hd=".$hd.", cl=".$cl.", ud=".$ud.", tx=".$tx.", date='".$date."'";
                                $q=mysql_query($k);
 
                                $empID=$_POST["empID"];
                                $bs=$_POST["bs"];
                                $pa=$_POST["pa"];
                                $pc=$_POST["pc"];
                                $rl=$_POST["rl"];
                                $gs=$_POST["gs"];
                                $hd=$_POST["hd"];
                                $cl=$_POST["cl"];
                                $ud=$_POST["ud"];
                                $tx=$_POST["tx"];
                                $bBulan=$_POST["bBulan"];
                                $bTuig=$_POST["bTuig"];
                                $date=$bTuig."/01/".$bBulan;
You are getting the POSTed values *AFTER* trying to use them in the query!!!

You must do the query *AFTER* getting the values *FOR* the query:
Code:
 
                                $empID=$_POST["empID"];
                                $bs=$_POST["bs"];
                                $pa=$_POST["pa"];
                                $pc=$_POST["pc"];
                                $rl=$_POST["rl"];
                                $gs=$_POST["gs"];
                                $hd=$_POST["hd"];
                                $cl=$_POST["cl"];
                                $ud=$_POST["ud"];
                                $tx=$_POST["tx"];
                                $bBulan=$_POST["bBulan"];
                                $bTuig=$_POST["bTuig"];
                                $date=$bTuig."/01/".$bBulan; 

                                $k="insert pay set empID=".$empID.", bs=".$bs.", pa=".$pa.", pc=".$pc.", rl=".$rl.", gs=".$gs.", hd=".$hd.", cl=".$cl.", ud=".$ud.", tx=".$tx.", date='".$date."'";
                                $q=mysql_query($k);
There may be other things wrong, but that one is so dead obvious that you must fix it first.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
dlep2x2 (05-27-2011)
Old 05-27-2011, 10:00 PM   PM User | #4
dlep2x2
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
dlep2x2 is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
This is why you should be checking for errors after calling mysql_query(). You're shooting blind after being spun around 3 times. Kind of hard to hit the target.

To take the blindfold off, check the return value of mysql_query:

PHP Code:
$result mysql_query($query);
if (!
$result)
{
    die(
"MySQL Error! Query is $query<br />Error is ".mysql_error());


yah. sorry, i updated my codes already with that one. Thanks anyways.
dlep2x2 is offline   Reply With Quote
Old 05-27-2011, 10:01 PM   PM User | #5
dlep2x2
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
dlep2x2 is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Your code ordering is bass-ackwards!!

Code:
                                $k="insert pay set empID=".$empID.", bs=".$bs.", pa=".$pa.", pc=".$pc.", rl=".$rl.", gs=".$gs.", hd=".$hd.", cl=".$cl.", ud=".$ud.", tx=".$tx.", date='".$date."'";
                                $q=mysql_query($k);
 
                                $empID=$_POST["empID"];
                                $bs=$_POST["bs"];
                                $pa=$_POST["pa"];
                                $pc=$_POST["pc"];
                                $rl=$_POST["rl"];
                                $gs=$_POST["gs"];
                                $hd=$_POST["hd"];
                                $cl=$_POST["cl"];
                                $ud=$_POST["ud"];
                                $tx=$_POST["tx"];
                                $bBulan=$_POST["bBulan"];
                                $bTuig=$_POST["bTuig"];
                                $date=$bTuig."/01/".$bBulan;
You are getting the POSTed values *AFTER* trying to use them in the query!!!

You must do the query *AFTER* getting the values *FOR* the query:
Code:
 
                                $empID=$_POST["empID"];
                                $bs=$_POST["bs"];
                                $pa=$_POST["pa"];
                                $pc=$_POST["pc"];
                                $rl=$_POST["rl"];
                                $gs=$_POST["gs"];
                                $hd=$_POST["hd"];
                                $cl=$_POST["cl"];
                                $ud=$_POST["ud"];
                                $tx=$_POST["tx"];
                                $bBulan=$_POST["bBulan"];
                                $bTuig=$_POST["bTuig"];
                                $date=$bTuig."/01/".$bBulan; 

                                $k="insert pay set empID=".$empID.", bs=".$bs.", pa=".$pa.", pc=".$pc.", rl=".$rl.", gs=".$gs.", hd=".$hd.", cl=".$cl.", ud=".$ud.", tx=".$tx.", date='".$date."'";
                                $q=mysql_query($k);
There may be other things wrong, but that one is so dead obvious that you must fix it first.

It did work! Yah. that was really dumb of me, sorry.
Thanks a lot!
dlep2x2 is offline   Reply With Quote
Reply

Bookmarks

Tags
database, insert, php, sql, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:05 AM.


Advertisement
Log in to turn off these ads.