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";
Related
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
$_SESSION['delete'] = $row['movieid'];
echo "<td>";
echo"</tr>";
}
echo "</table>";
?>
if (isset($_POST['deletem'])) {
$delete=$_SESSION['delete'];
$query6 = "DELETE FROM movieinfo WHERE movieid=$delete";
mysqli_query($db, $query6);
}
i need to delete the particular row with the id selected so i am storing in session the id and deleting it but when i press on the deleting button it deletes the last row only its because session is storing all the id's because of loop how should i fix it?
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
**echo "</td>";
$mid = isset($_GET['mid']) ? $_GET['mid'] : '';
$del = "DELETE FROM movieinfo WHERE movieid=$mid";
mysqli_query($db, $del);
{
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
}
echo"</tr>";
}
echo "</table>";
?>
I have created sql view. I want to export the view contents to excel sheet upon clicking the button say "Export in Excel". How do I do that?. Looking forward to your help.
<?php
$db_host = '';
$db_user = '';
$db_pwd = '';
$database = '';
$table = '';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM view_abc");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<div class='wrapper'>
<font size=3 color=blue><b>Entry";
echo "</font></b><table class='table'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
/* foreach($row as $cell)
echo "<td>$cell</td>";
*/
echo "<td bgcolor='#d5eaf0'>{$row[0]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[1]}</td>";
if ($row[2]==0)
echo "<td bgcolor='green'>{$row[2]}</td>";
else if ($row[2]>0)
echo "<td bgcolor='red'>{$row[2]}</td>";
if ($row[3]==0)
echo "<td bgcolor='green'>{$row[3]}</td>";
else if ($row[3]>0)
echo "<td bgcolor='red'>{$row[3]}</td>";
if ($row[4]>90)
echo "<td bgcolor='green'>{$row[4]}</td>";
else if ($row[4]<90)
echo "<td bgcolor='red'>{$row[4]}</td>";
if ($row[5]>90)
echo "<td bgcolor='green'>{$row[5]}</td>";
else if ($row[5]<90)
echo "<td bgcolor='red'>{$row[5]}</td>";
if ($row[6]>90)
echo "<td bgcolor='green'>{$row[6]}</td>";
else if ($row[6]<90)
echo "<td bgcolor='red'>{$row[6]}</td>";
if ($row[7]>90)
echo "<td bgcolor='green'>{$row[7]}</td>";
else if ($row[7]<90)
echo "<td bgcolor='red'>{$row[7]}</td>";
if ($row[8]>90)
echo "<td bgcolor='green'>{$row[8]}</td>";
else if ($row[8]<90)
echo "<td bgcolor='red'>{$row[8]}</td>";
if ($row[9]>90)
echo "<td bgcolor='green'>{$row[9]}</td>";
else if ($row[9]<90)
echo "<td bgcolor='red'>{$row[9]}</td>";
if ($row[10]=='YES')
echo "<td bgcolor='green'>{$row[10]}</td>";
else if ($row[10]=='NO')
echo "<td bgcolor='red'>{$row[10]}</td>";
if ($row[11]==0)
echo "<td bgcolor='green'>{$row[11]}</td>";
else if ($row[11]>0)
echo "<td bgcolor='red'>{$row[11]}</td>";
if ($row[12]==0)
echo "<td bgcolor='green'>{$row[12]}</td>";
else if ($row[12]>0)
echo "<td bgcolor='red'>{$row[12]}</td>";
if ($row[13]<28)
echo "<td bgcolor='green'>{$row[13]}</td>";
else if ($row[13]>28)
echo "<td bgcolor='red'>{$row[13]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[14]}</td>";
echo "</tr></div>\n";
}
mysql_free_result($result);
?>
use the below query
$res = mysql_query("SELECT * INTO OUTFILE '$name.csv' FROM users ");
here $name is the name of the file you want to create.
otherwise you can use the present date by using date function instead of a static name.
The file will be stored in xampp/mysql/data/your database name ( assuming you are using xampp).
I have this following code in a switch statement since the query can vary based on screen-name(1 word), full name (2 words...maybe 3), or the email address which "explode"'s to 3 words (I put it back together later, no biggy). However, cannot locate the problem in the following code:
$query = "SELECT * FROM personal WHERE FirstName='$searchName' OR LastName='$searchName' OR Email='$searchName' OR displayName='$searchName'";
$result = mysql_query($query) or die(mysql_error());
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
$myBuddy = $row['FirstName'].
" ".$row['LastName'];
$picName = $row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/'.$picName.
'/profile.jpg')) {
$picLine = "<img src = '../members/".$picName.
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>".$picLine.
"</td>";
echo "<td style='text-align:center; color:red'>".$myBuddy.
"</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
Problem is, I have several people in my MySql database with the same last name (Jones), however in the code above only returns the first occurance of someone with the last name Jones. Trying to return them all, not just one. I know I am just overlooking something small & stupid - been doing php/mysql for 2 years now - never had to do a search page before. Any help is greatly appreciated.
while ($row = mysql_fetch_assoc($result)) {
//...
//use $row
}
instead of
$row = mysql_fetch_assoc($result);
You have too loop the results, like in the following example:
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result) )
{
while($row = mysql_fetch_assoc($result)) {
$myBuddy = $row['FirstName'] . " " . $row['LastName'];
$picName=$row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/' . $picName . '/profile.jpg'))
{
$picLine = "<img src = '../members/" . $picName .
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>" . $picLine . "</td>";
echo "<td style='text-align:center; color:red'>" . $myBuddy . "</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
}
Above, I have used a while loop.
while($row = mysql_fetch_assoc($result)) {
// code
}
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>";
}
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