Troubleshooting "Notice: Undefined index" error - php

could anyone check my code why i get a notice like this?
Notice: Undefined index: id in C:\xampp\htdocs\HRPO\module\reports\jo\view_jo.php on line 76
line 76:
$id=$_GET['id'];
here is the first could of which I get my id:
<?php
echo "<dl>";
echo "<dt width = 200 id=\"label\">"."SSA"."</dt>";
echo "<dd align='right'>";
$result = mysql_query("SELECT ssa.first_name,ssa.SSA_ID
FROM staffing_specialist_asst ssa
left join jo_partner jp on jp.SSA_ID = ssa.SSA_ID
group by first_name") or die(mysql_error());
$dropdown = "<select name=\"SSA_ID\" style=\"position:relative; left:-51px;\">\n";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['SSA_ID']}'>{$row['first_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
echo "</dd>";
echo "</dl>";
?>
and the second code where line 76 is found:
<?php
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$datefrom = $_POST['timestamp'];
$dateto = $_POST['timestamp1'];
//echo $option;
$_SESSION['datefrom'] = $datefrom;
$_SESSION['dateto'] = $dateto;
if(( $datefrom == NULL) || ($dateto == NULL)){
echo "<SCRIPT LANGUAGE='javascript'> confirmationError() ;</SCRIPT>";
exit();
}
$final =("SELECT distinct jp.receivedDate as rDate, ssa.first_name as saFName, ssa.last_name as saLName,job.client_order_number as joNum,
job.job_order_type as joType, job.job_title as joTitle, cl.name as clientName
,ss.first_name as ssFName,ss.last_name as ssLName,jp.acknowledgeDate as aDate, stat.status as stat
FROM staffing_specialist_asst ssa
left join jo_partner jp on ssa.SSA_ID = jp.SSA_ID
left join job_order job on jp.job_order_number = job.job_order_number
left join jo_status stat on job.job_order_number = stat.job_order_number
left join staffing_specialist ss on jp.SS_ID = ss.SS_ID
left join client cl on job.client_ID = cl.client_ID
where jp.receivedDate between '$datefrom1' and '$dateto1'
and ssa.SSA_ID='$id'
group by jp.receivedDate
order by jp.receivedDate asc");
echo $final;
$query = mysql_query($final);
echo "<table>";
while($row = mysql_fetch_array($query))
{
$rDate = $row['rDate'];
$saFName = $row['saFName'];
$saLName = $row['saLName'];
$joNum = $row['joNum'];
$joType = $row['joType'];
$joTitle = $row['joTitle'];
$clientName = $row['clientName'];
$ssFName = $row['ssFName'];
$ssLName = $row['ssLName'];
$aDate = $row['aDate'];
$stat = $row['stat'];
echo "<tr>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$rDate."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$saFName."".$saLName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joNum."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joType."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joTitle."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$clientName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$ssFName."".$ssLName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$aDate."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$stat."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Thanks in advance for all the suggestions and help.

$_GET['id'] expected the get variable 'id' in the url (i.e. www.google.com?id=4 then $_GET['id'] would equal 4).
In order to avoid this you could do this before checking the get value if (! empty($_GET)) {$id = $_GET['id']}
EDIT: The actual error ended up being the assumption of needing to use $_GET instead of the possibility of $_POST for form data.

Looks like the select box is SSA_ID but you are using a $_GET['id'], try changing it to $_GET['SSA_ID'];

Related

Add more then two while loop in one while with multiple tables

I want to try multiple while in one while with multiple tables but it's doesn't work.
I have three table and I want a different-different value from table.
Here is my code.
$table1 = GROUPMASTER;
$table2 = LEDGERMASTER;
$table3 = TRANSECTIONMASTER;
$groupmasterdata = mysql_query("select * from $table1 WHERE groupname = 'Capital' OR groupname = 'Fund and Reserves' OR groupname = 'Long Term liabilities' OR groupname = 'Current Liabilities' ");
echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2 style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
while($recored = mysql_fetch_array($groupmasterdata))
{
$onegroupname = $recored ['groupname'];
echo "<tr>";
echo "<td colspan=3><b>" . strtoupper($onegroupname) . "</b></td></tr>";
$groupnamelist = mysql_query("select groupname from $table1 WHERE under = '". $onegroupname ."'");
while($data = mysql_fetch_array($groupnamelist))
{
$gname = $data ['groupname'];
$getledgername = mysql_query("select ledgername from WHERE groupname = '". $gname ."'");
while($lname = mysql_fetch_array($getledgername))
{
$gettocashbal = mysql_query("select *, SUM(amount) AS totalto from $table3 WHERE voucherto = '". $ledgername ."'");
while($datato = mysql_fetch_array($gettocashbal))
{
$tototal = $datato['totalto'];
}
$getbycashbal = mysql_query("select *, SUM(amount) AS totalby from $table3 WHERE voucherby = '". $ledgername ."' ");
while($databy = mysql_fetch_array($getbycashbal))
{
$bytotal = $databy['totalby'];
}
$ledgertotal = $opbal - $tototal + $bytotal;
echo "<tr>";
echo "<td class='test'>" . $gname . "</td>";
echo "<td>Rs. $ledgertotal</td>";
echo "<td></td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td style='text-align:right'>Total</td>";
echo "<td></td>";
echo "<td>Rs.</td>";
echo "</tr>";
}
echo "</table>";
I want to try multiple while but doesn't work.
How can I solve these issues?
I tried different way to solve this...
//TABLE 1 = GROUPMASTER
$table1 = $_SESSION['table1'];
//TABLE 2 = LEDGERMASTER
$table2 = $_SESSION['table2'];
//TABLE 3 = TRANSECTIONMASTER
$table3 = $_SESSION['table3'];
echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2 style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
echo "<tr>";
$liabilitytotal = 0;
echo "<td colspan=3><b>CAPITAL</b></td></tr>";
$categorytotal = 0;
$groupnamelist = mysql_query("select groupname from $table1 WHERE under = 'Capital'");
while($data = mysql_fetch_array($groupnamelist))
{
$grouptotal = 0;
$gname = $data ['groupname'];
echo "<tr>";
echo "<td class='test'>" . $gname . "</td>";
$ledgername = mysql_query("SELECT ledgername from $table2 WHERE groupname = '". $gname ."' ");
while($datato = mysql_fetch_array($ledgername))
{
$lname = $datato['ledgername'];
$getbytotalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherby = '". $lname ."' ");
$gettototalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherto = '". $lname ."' ");
$getopbal = mysql_result($getopbalquery,0,0);
$getbytotal = mysql_result($getbytotalquery,0,0);
$gettototal = mysql_result($gettototalquery,0,0);
$result = $getopbal - $gettototal + $getbytotal;
if ($result == null)
{
$result = 0;
}
$grouptotal = $grouptotal + $result;
}
$categorytotal = $categorytotal + $grouptotal;
echo "<td style='text-align:right'>$grouptotal</td><td></td></tr>";
}
$liabilitytotal = $liabilitytotal + $categorytotal;
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td style='text-align:right'>$categorytotal</td>";
echo "</tr>";
echo "</table>";```
I tried my best..
And I will try to also solve this query to another way...
Thank you.. I think and I hope It's helpful..

php results 2 table on page instead of one with all data

I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.
This is my results code by id from the search script. and returns table for each person
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo"</thead>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
echo"</table>";
echo "</center>\n";}
You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement
$id = $_GET['id'];
$id=mysql_real_escape_string($id);
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
$row=mysql_num_rows($rst);
if($row>0){
//outside while loop
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th>";
echo"</thead>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
}
//outside while loop
echo"</table>";
echo "</center>\n";
}
Note:- Don't use mysql because it is deprecated instead use mysqli or
PDO
To prevent sql injection check this link How can I prevent SQL-injection in PHP?
You have to get table part outside from while loop.
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo "</thead><tbody>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";
}
echo"</tbody></table>";
echo "</center>\n";

"Array to string conversion" while writing MySQL output into a variable

I'm trying to write the output of a MySQL select into a variable. The problem is that i get the error "Array to String conversion".
$user = mysql_query("select username from user");
echo "<table border='1'>";
if (isset($_POST['winneron'])) {
echo "<tr>";
while ($printuser = mysql_fetch_array($user)) {
echo "<th align='center'>". $printuser['username'] . "</th>";
}
echo "</tr>";
$user = mysql_query("select username from user");
while ($printuser = mysql_fetch_array($user)) {
$games = mysql_query("SELECT s.spielid, date, team1, team2, sieger, wettid, u.userid, w.spielid, team
FROM user u, spiele s, wette w
WHERE u.userid = w.userid
AND u.username = '$printuser'
AND w.spielid = s.spielid"); <-- Error line
while ($printgames = mysql_fetch_array($games)) {
if ( $printgames['sieger'] == $printgames['team1'] ) {
echo "<tr><td align='center'><b>". strtoupper($printgames['sieger']) . "</b></td></tr>";
}
else {
echo "<tr><td align='center'>". strtoupper($printgames['sieger']) . "</td></tr>";
}
}
}
}
AND u.username = '$printuser' replace it with AND u.username = '".$printuser['username']."'
$printuser is a array and you are using it like a string

how to select two columns from two tables in mysql

I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}

Php undefine variable issue

I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c

Categories