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
Related
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";
Basically I have 4 fields in a form. I want to the user search for books in a library by either title, or by author or both. I also want the user to set the length of the list of items and from the starting point, these are not restrictions though the user does not have to specify.
Here is the code:
require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_GET["title"];
$authors = $_GET["authors"];
$st = $_GET["start"];
$ln = $_GET["length"];
$stmt = $dbh->prepare("SELECT title, authors, description, price FROM books WHERE title = :title LIMIT :length OFFSET :start");
$stmt->execute(array(':title' => $title,':start' => $st,':length' => $ln));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
}
echo "<table>";
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
So far it literally just returns me what I have typed in the input - so nothing much at all! Does anyone know how I can do this?
Adapt your code this way:
echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";
to print all the rows returned by the query. Your code was printing just the last row.
Hello I have a problem in one of my php projects. The problem states that I need to provide the top students for each year of classes as well as the best overall student. However i am having trouble with the if statments; I am unable to post the name of each student where the name should be it displays their score for the year.
pic of database:
http://imagizer.imageshack.us/a/img843/9674/7r2t.png
here is my code:
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = 100;
$met = 0;
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result)) {
$grader = "$_POST[result]";
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row[Final_Grade] = $grader;
if ($grader <= 100 and $grader >= 89) {
$grade = "A";
} elseif ($grader <= 90 and $grader >= 79) {
$grade = "B";
} elseif ($grader <= 80 and $grader >= 69) {
$grade = "C";
} elseif ($grader <= 70 and $grader >= 59) {
$grade = "D";
} else {
$grade = "F";
}
if ('$row[Final]' > $set) {
$year1 = $grade;
$year2011 = $row;
}
if ('$row[Final]' < $set) {
$year2 = $grade;
$year2012 = $row;
}
if ('$row[Final]' < $set) {
$year3 = $grade;
$year2013 = $row;
}
if ('$row[Final]' < $set) {
$year4 = $grade;
$year2014 = $row;
}
if ('$row[Final]' < $set) {
$final = $grade;
$finalscore = $row;
}
if ('$row[Final]' < $set) {
$overall = $grade;
$overall = $row;
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "$year2011[Name]";
echo "</td><td>";
echo "$year2011[year_1]";
echo "</td><td>";
echo "$year2011[year_2]";
echo "</td><td>";
echo "$year2011[year_3]";
echo "</td><td>";
echo "$year2011[year_4]";
echo "</td><td>";
echo "$year2011[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "$year2012[Name]";
echo "</td><td>";
echo "$year2012[year_1]";
echo "</td><td>";
echo "$year2012[year_2]";
echo "</td><td>";
echo "$year2012[year_3]";
echo "</td><td>";
echo "$year2012[year_4]";
echo "</td><td>";
echo "$year2012[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "$year2013[Name]";
echo "</td><td>";
echo "$year2013[year_1]";
echo "</td><td>";
echo "$year2013[year_2]";
echo "</td><td>";
echo "$year2013[year_3]";
echo "</td><td>";
echo "$year2013[year_4]";
echo "</td><td>";
echo "$year2013[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "$finalscore[Name]";
echo "</td><td>";
echo "$finalscore[year_1]";
echo "</td><td>";
echo "$finalscore[year_2]";
echo "</td><td>";
echo "$finalscore[year_3]";
echo "</td><td>";
echo "$finalscore[year_4]";
echo "</td><td>";
echo "$finalscore[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "$overall[Name]";
echo "</td><td>";
echo "$overall[year_1]";
echo "</td><td>";
echo "$overall[year_2]";
echo "</td><td>";
echo "$overall[year_3]";
echo "</td><td>";
echo "$overall[year_4]";
echo "</td><td>";
echo "$overall[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
echo "</table>";
Everything in single quotes in PHP gets interpreted literally as a string. When you do '$row[Final]' PHP interprets that as "dollar sign, open bracket, the word 'Final', close bracket."
In order to have PHP interpret what you're saying as a reference to a variable, you have to reference only your index as a string, leaving the rest outside your quotes.
$row["Final"]; tells PHP, "check the variable $row and see what's stored at it's index represented by the string Final."
PHP: Strings - Manual
It looks like you need to store the highest result for each group you want to display later (ie, highest for each year), rather than just one single highest store in $set.
Not 100% sure on what you want, but to give you an idea something like this:-
<body>
<form>
<?php
$username = "amar";
$password = "amar";
$hostname = "localhost";
$database = "study";
$set = array('2011'=>0, '2012'=>0, '2013'=>0, '2014'=>0, 'final'=>0, 'Final_Grade'=>0);
$mysqli = new mysqli($hostname, $username, $password, $database)
or die("Unable to connect to MySQL");
$query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade from toppers";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border=1 cellpadding=5>";
echo "<tr><td>Student Name</td>";
echo "<td>2011</td>";
echo "<td>2012</td>";
echo "<td>2013</td>";
echo "<td>2014</td>";
echo "<td>Final Exam</td>";
echo "<td>Grade</td></tr>";
while ($row = mysqli_fetch_array($result))
{
$grader = $_POST['result'];
$x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
$grader = $x / 5;
$row['Final_Grade'] = $grader;
switch (true)
{
case ($grader <= 100 and $grader >= 89) :
$grade = "A";
break;
case ($grader <= 90 and $grader >= 79) :
$grade = "B";
break;
case ($grader <= 80 and $grader >= 69) :
$grade = "C";
break;
case ($grader <= 70 and $grader >= 59) :
$grade = "D";
break;
default :
$grade = "F";
break;
}
if ($row['Final'] > $set['2011'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2012'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2013'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['2014'])
{
$year2011 = $row;
$set['2011'] = $row['Final'];
}
if ($row['Final'] < $set['final'])
{
$finalscore = $row;
$set['final'] = $row['Final'];
}
if ($row['Final_Grade'] < $set['Final_Grade'])
{
$overall = $row;
$set['Final_Grade'] = $row['Final_Grade'];
}
echo "<tr><td>";
echo "$row[Name]";
echo "</td><td>";
echo "$row[year_1]";
echo "</td><td>";
echo "$row[year_2]";
echo "</td><td>";
echo "$row[year_3]";
echo "</td><td>";
echo "$row[year_4]";
echo "</td><td>";
echo "$row[Final]";
echo "</td><td>";
echo "$grade";
echo "</td></tr>";
}
echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2011)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2012)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2013)."</td></tr>";
echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
echo "<tr><td>".implode("</td><td>", $year2014)."</td></tr>";
echo "<tr><td colspan=7>Topper overall: </td></tr>";
echo "<tr><td>".implode("</td><td>", $overall)."</td></tr>";
echo "</table>";
Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.
i want it to echo a (-) where a student does not take the subject.thanks in advance
<table border='1'>
<tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
</tr>
<?php
//loop to display the names of students
$i = 1;
foreach ($overallresults as $result) {
echo "<tr>";
echo "<td>";
echo $i++;
echo "</td>";
$admNo = $result->admNo;
$total_marks = $result->total_marks;
$agp = $result->aggregate_points;
$mean_grade = $result->mean_grade;
$agp = $result->aggregate_points;
$result_id = $result->result_id;
$fname = "";
foreach ($students as $student) {
// print_r($student);
$admNo1 = $student->admNo;
if ($admNo == $admNo1) {
// print_r($student);
$fname = $student->firstName;
$mname = $student->middleName;
$lname = $student->lastName;
//}
// }
//echo "<tr>";
echo "<td>";
echo $fname . " " . $mname . " " . $lname;
echo "</td>";
}
}
foreach ($subjectresults as $subresult) {
// print_r($result);
$score = "0";
$admNo3 = $subresult->admNo;
$subCode = $subresult->subCode;
$score = $subresult->score;
if ($admNo == $admNo3) {
if ($subCode == '232') {
$score = $score;
}
if ($subCode == '101') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '102') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '121') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '231') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '232') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '233') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '311') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '312') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '313') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '443') {
echo "<td>";
if (!$score) {
echo 0;
} else {
echo $score;
}
echo "</td>";
}
if ($subCode == '565') {
echo "<td>";
echo $score;
echo "</td>";
}
}
}
?>
<?php
if (isset($term)) {
$term = $term;
}
if (isset($form)) {
$form = $form;
}
if (isset($year)) {
$year = $year;
}
if (isset($examCategory)) {
$examCategory = $examCategory;
}
//if ($admNo == $admNo1) {
// print_r($student);
//}
// }
echo "<td>";
echo $total_marks;
echo "</td>";
echo "<td>";
echo $mean_grade;
echo "</td>";
echo "<td>";
echo $agp;
echo "</td>";
echo "<td>";
echo "</td>";
//}
}
?>
</table>
<?php
}
?>
</div>
the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121
I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;
<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
101=>20,
102=>30,
103=>30,
104=>45
),);
$result2 = array(
'studentId'=>2,
'score'=>array(
101=>34,
102=>54,
103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>
Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.
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>";
}