...

View Full Version : Problem with inserting data into database



dlep2x2
05-27-2011, 09:50 PM
hi! i need some help with my php code. Here is the part of my php code that doesn't work:


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!

Fumigator
05-27-2011, 10:40 PM
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:



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

Old Pedant
05-27-2011, 10:41 PM
Your code ordering is bass-ackwards!!



$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:



$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.

dlep2x2
05-27-2011, 11:00 PM
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:



$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
05-27-2011, 11:01 PM
Your code ordering is bass-ackwards!!



$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:



$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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum