in my project i try to display data from database and this is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="prac.css">
<title>PHP Practical</title>
</head>
<body>
<div class="main">
<table class="table">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Contact</th>
<th>Employee Salary</th>
<th>Employee Phone</th>
<th>Employee City</th>
<th>Join Date</th>
<th>Action</th>
</tr>
<?php
require 'config.php';
$query = mysql_query("SELECT * FROM emp");
while ($row = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_contact']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
<td><?php echo $row['emp_phone']; ?></td>
<td><?php echo $row['emp_city']; ?></td>
<td><?php echo $row['join_date']; ?></td>
<td>Update | Delete</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
i checked my config.php file and it is working fine and in database i have 6 records but it is not displaying any. i try also run query to database and it is working fine in database but not here. please help me.
**Use this**
$query = mysql_query("SELECT * FROM emp") or die(mysql_error());
to display mysql error
The mysql_ extension is deprecated. There is no point in using it.
Switch to mysqli or PDO
The issue you are currently facing is not caused because of mysql_ being deprecated but there's no worth in investigating it. It's not used anymore
Related
All the other parts of my application are working but my users view cart is not working I do not know whether if it is about the indexing
I have tried using outer php
<!DOCTYPE html>
<html>
<head>
<title>View table</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs /popper.js/1.14.7/umd/popper.min.js"></script>
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">brand</th>
<th scope="col">flavour/type</th>
<th scope="col">quantity</th>
<th scope="col">Number of units</th>
<th scope="col">Number of packets</th>
<th scope="col">price</th>
<th scope="col">Cost</th>
<th scope="col">Picture</th>
<th scope="col">D</th>
</tr>
</thead>
<tbody>
<?php
require_once("config.php");
error_reporting(0);
$email_address=$_SESSION['email_address'];
$sql="SELECT product_name ,brand,flavour_type,quantity,number_of_units,price ,units,image_path
FROM gokcen.product NATURAL JOIN gokcen.cart
WHERE cart.email_address=:email_addres";
$stmt=$db->prepare($sql);
$stmt->bindParam(':email_address',$email_address);
$stmt->execute();
$result=$stmt->fetchAll();
foreach($result as $product)
{
?> <tr>
<td><?php echo $product['product_name'];?></td>
<td><?php echo $product['brand'];?></td>
<td><?php echo $product['flavour_type'];?></td>
<td><?php echo $product['quantity'];?></td>
<td><?php echo $product['number_of_units'];?></td>
<td><?php echo $product['units'];?></td>
<td><?php echo $product['price'];?></td>
<td><?php echo $product['price']* $prouct['units'];?></td>
<td> <img src="pics/<?php echo $product['image_path'];?>" width="80" height="80"/></td>
<td><a href="deletefromcart.php?item=<?echo product['product_name'];?>"> delete <a></td>
</tr>
<?php }?>
</tbody>
</table>
<a href="#" class="btn btn-primary" >Buy</a>
</body>
</html>
There are no results It does not give any errors it shows the header part only and Im not sure about the indexing.
there are several typos in your code.
When I am right there should be another "s" at the end? cart.email_address=:email_addres
There is a "d" missing at line <?php echo $product['price']* $prouct['units'];?>
You also missed the "$" sign at this point as well as the "php" after the question mark: <?echo product['product_name'];?>
You should set display_errors to On in your php.ini and comment out the error_reporting(0); for debugging purposes :)
Greetings
You have a typo email_addres email_address.
WHERE cart.email_address=:email_addres";
$stmt->bindParam(':email_address',$email_address);
Thank you guys you are the best you rock my coding life It was the typos and also that
I had not put a
session_start()
To initialize the value of $email_address on
$email_address=$_SESSION['email_address'];
All the best!
I have a problem with displaying files added by the logged user.
I do not know how to pass the variable correctly to the sql query.
Can anyone help me with this?
Currently, the code looks like this:
<?php
include_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">your uploads...<label>upload new files...</label></th>
</tr>
<tr>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT * FROM files";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td>view file</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
I am trying to change this record :
$sql="SELECT * FROM files";
to
$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";
but I still do not get the correct result. Can anyone help?
It looks like the issue with that line is in how you are including the $_SESSION variable. You should have quotes around userId like $_SESSION['userId'] or {$_SESSION['userId']}.
More importantly you should avoid entering variables directly into MySQL queries. I would recommend using MySQLi or PDO instead of MySQL, and look into prepared statements (here or here, for example).
This is user class.
<?php
class User {
/*
*public static function get_all_users()
*/
public function get_all_users() {
global $link;
$result_set = $link->query("SELECT * FROM `user`");
return $result_set;
}
}
Above class is user class i made object but cannot access. Display all use page of html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>Admin Panel ::: </title>
</head>
<body>
<h3>Users :</h3>
<table border="1">
<tr>
<th>S.N</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th>E-mail</th>
<th>Contact</th>
<th colspan="2">Option</th>
</tr>
<?php
$user = new User();
$result_set = $user->get_all_users();
/*
*$result_set = User :: get_all_users(); - accessing user class using static keyword.
*/
$i = 1;
while ($row = mysqli_fetch_assoc($result_set)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['Contact']; ?></td>
<td>Update</td>
<td>Delete</td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>
I can not access user class when I create object of user class and user class is inside includes folder and below HTML file is inside admin folder or admin ->viewuser.php and admin->includes->user.php it gives error.
You need to include that User class inside the viewuser.php script !
include_once("includes/user.php");
I am not sure organizing your folders like that is the best way to do but it should work.
I followed a web tutorial on how to build a basic search engine from my database the problem is that when I get the results displayed it shows each result in its own table on my page? I want to merge the results so they all show under one table in html.
<?php
include("dbconnect.php");
if(!isset($_POST['search'])) {
header("Location:www.bacons.me");
}
$search_sql="SELECT * FROM users WHERE username OR FirstName LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query) !=0) {
$search_rs=mysql_fetch_assoc($search_query);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Bacons.me">
<meta name="keywords" content="HTML,CSS,JavaScript">
<meta name="author" content="James Narey">
<meta name=viewport content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="stylesheet" type="text/css" href="main.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<title>Bacons.me</title>
</head>
<body>
<h1 class="logo">BACONS.ME</h1>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Search</li>
<li>Contact</li>
</ul>
<div class="handle">Menu<span class="arrow"> ▾</span></div>
</nav>
<p>Search Results</p>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
<table>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th>
</tr>
<tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
</table>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
} else {
echo "No results found";
}
?>
<html>
<title>results</title>
<head>
</head>
<body>
</body>
</html>
<footer>
<p class="footer">Website created by James Narey 2015.</p>
</footer>
</body>
</html>}
Put your table tag outside your do while loop, otherwise it would create new table at every iteration of the loop.
Your code structure should be like this
<?php if(mysql_num_rows($search_query) !=0) {?>
<table>
<?php do { ?>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th>
</tr>
<tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
?>
</table>
...} while ($search_rs=mysql_fetch_assoc($search_query));
I suspect this is the issue. You're creating a table for each time you fetch the next record. You probably want to do something like this:
<table>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
// fill in table...
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
?>
</table>
Notice the table tags are outside the loop, so you'll only create a new row for each record you fetch
Just place the table tags outside the loop structure:
<table> do { ?> <tr>
<th>Username</th>
<th>First Name</th>
<th>Surname</th>
<th>Shift</th>
<th>Agency</th> </tr> <tr>
<td><?php echo $search_rs['username']; ?></td>
<td><?php echo $search_rs['FirstName']; ?></td>
<td><?php echo $search_rs['LastName']; ?></td>
<td><?php echo $search_rs['Shift']; ?></td>
<td><?php echo $search_rs['Agency']; ?></td> </tr> <br> <?php } while ($search_rs=mysql_fetch_assoc($search_query)); </table>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my member profile page where I want the details of user to come ... but everything is getting printed including user id... but not the user record... I am new to this php and will appreciate any one help. screen shot is http://postimg.com/image/146000/screenshot-145081.jpg
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h2>Welcome, <?=$_SESSION['sess_user'];?>! Logout</h2>
<table width="1105" height="140" border="0" align="center" cellpadding="3" cellspacing="3">
<tr>
<th colspan="12"> </th>
</tr>
<tr>
<th colspan="7"><?php if(isset($_GET["msg"])) echo $_GET["msg"];?></th>
</tr>
<tr>
<td>Name</td>
<td>Company Name</td>
<td>Mobile no.</td>
<td>Email</td>
<td>City</td>
<td>State</td>
<td>Country</td>
<td>Category</td>
<td>Registration</td>
<td>DOJ</td>
<td>Action</td>
</tr>
<?php
require("includes/connections.php");//to make connection with database
$id=$_SESSION['sess_user'];
$sql="select * from stonemember where id='$id'";//select query from table stonemember
$result=mysql_query($sql);//result of select query in $result varialble
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["Name"];?></td>
<td><?php echo $row["CompanyName"];?></td>
<td><?php echo $row["mobileno"];?></td>
<td><?php echo $row["email"];?></td>
<td><?php echo $row["city"];?></td>
<td><?php echo $row["state"];?></td>
<td><?php echo $row["Country"];?></td>
<td><?php echo $row["cat"];?></td>
<td><?php echo $row["registered"];?></td>
<td><?php echo $row["Date"];?></td>
<td><a href="admin/edit.php?id=<?php echo $row["id"];?>"Edit</a></td>
</tr>
<tr>
<?php
}
?>
</tr>
</table>
</body>
</html>
<?php }?>
You are going to need to learn to debug your scripts. Use google, others may have had the same problems.
1. In your php script, before everything, use:
erorr_reporting(E_ALL);
ini_set('display_errors', 'On');
usually, this will show ALL your script errors.
2. Instead of:
$result=mysql_query($sql);
use
$result=mysql_query($sql) or die(mysql_error());
this way, if you get a mysql error, it will stop the script and show the error.
3. Learn to use var_dump and print_r.
Example: after this part
$id=$_SESSION['sess_user'];
put this code:
var_dump($id);
this will show you if there is a value in $id variable and its type