I'm working on a server based POS and I have a php page that displays the client current money on a table, I have 2 tables (Mov_ctes and Clientes), it works fine when I add WITH ROLLUP on the mysql query, It displays the Total but without A Name (NULL value), so I used
IFNULL(Clientes.Nombre,'TOTAL')
so It could change the NULL value to TOTAL, I entered the whole command on mysql and worked fine, however if I enter the same query via PHP it doesnt output the "Nombre" column
heres my code and a Mysql screenshot
<!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" />
<style type="text/css">
<!--
#import url("source/style.css");
-->
</style>
</head>
<body>
<?php
session_start();
$log=$_SESSION['sesion'];
$nombr=$_SESSION['username'];
if($log==1)
{
$con=mysqli_connect("localhost","user","pw","My_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Mysql query
$result = mysqli_query($con,"SELECT Clientes.cliente_id,IFNULL(Clientes.Nombre,'TOTAL'), sum(Mov_ctes.Movimiento) FROM Clientes NATURAL LEFT JOIN Mov_ctes GROUP BY Nombre WITH ROLLUP");
echo "<table id='hor-minimalist-b' summary='Employee Pay Sheet'>";
echo "<thead>";
echo "<tr>";
echo "<th scope='col'>ID</th>";
echo "<th scope='col'>Nombre</th>";
echo "<th scope='col'>Saldo</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result))
{echo "<tr>";
echo "<td>" . $row['cliente_id'] . "</td>";
echo "<td>" . $row['Nombre'] . "</td>";
echo "<td>" . $row['sum(Mov_ctes.Movimiento)'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>
You need an alias for the column, otherwise the column name will be IFNULL(Clientes.Nombre,'TOTAL'):
SELECT IFNULL(Clientes.Nombre,'TOTAL') AS Nombre
...
Related
NEWBIE ALERT
Ive been reading for hours but cannot see how to do what I want. I have an SQL DB with a list of medication and dosages etc. Im trying to create an 'edit' page so i can select the medication from a dropdown list(this appears to work), and edit certain parts of it(size, box quantity, doseage etc.
the problem is i cant get the selected medication item(from dropdown) to become a variable or if neccessary, forward to a 2nd PHP page.
Ive left a couple of attempted edits in there but they caused errors (TRIED NOT WORK)
As it is, It is loading editmed.php to load my edit page on submit. (not sure if this is the best way of doing it).
Any help would be great thanks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div class="header">
<h1>Edit Medication Item</h1>
</div>
<div class="topnav">
Home
<a class="active" href="edit.php">Edit Medication</a>
Prescription Collection
Add New Medication
Pillbox refill
Individual refill
</div>
<?php
// server connect
$mysqli = new mysqli("localhost:port", "USERNAME", "PASSWORD", "prescription")
or die ('Cannot connect to db');
// select medication name field from db
echo "<p><b><U><i><p style='color:red'>Select Medication</I></U></p><p style='color:black'></b></p>";
$sqlSelect="select Medication from general";
$result = $mysqli -> query ($sqlSelect);
//$medselect=$result['Medication'];**TRIED NOT WORK**
echo "<form action='editmed.php'>";
echo "<select id=`Medication` name=`medselect`>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Medication'] . "'>" . $row['Medication'] . "</option>";
}
//$medselect=$result['Medication'];**TRIED NOT WORK**
echo "</select>";
echo "<input type='submit' value='submit'>";
// echo "<input type='submit' value='<$medselect>' name='medselect'/>" ; **TRIED NOT WORK**
echo "</form>" ;
echo "</body>";
echo "</html>";
?>
</body>
</html>
editmed.php >
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div class="header">
<h1>Edit Medication Item</h1>
</div>
<div class="topnav">
Home
<a class="active" href="edit.php">Edit Medication</a>
Prescription Collection
Add New Medication
Pillbox refill
Individual refill
</div>
<?php
// server connect
$link = mysqli_connect("localhost:port", "user", "password",
"prescription")
or die ('Cannot connect to db');
// $sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain,
grandtotal, last_collected, Dosage, countdown FROM general where
`Medication` = $medselect";
$sql = "SELECT ID, Medication, dosagedaily, Box_Size, Boxed_remain,
grandtotal, last_collected, Dosage, countdown FROM general where
`Medication` = 'Esomeprazole 40mg'";
// you check sql and link true
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Index</th>";
echo "<th>Medication</th>";
echo "<th>Daily Dosage</th>";
echo "<th>Box Size</th>" ;
echo "<th>Boxed Remaining</th>" ;
echo "<th>Grand Total</th>";
echo "<th>Last presc collection</th>";
echo "<th>Dosage</th>";
echo "<th>Pills Remaining</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Medication'] . "</td>";
echo "<td>" . $row['dosagedaily'] . "</td>";
echo "<td>" . $row['Box_Size'] . "</td>";
echo "<td>" . $row['Boxed_remain'] . "</td>";
echo "<td>" . $row['grandtotal'] . "</td>";
echo "<td>" . $row['last_collected'] . "</td>";
echo "<td>" . $row['Dosage'] . "</td>";
echo "<td>" . $row['countdown'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
i dont know how to assign the variables from the input form, thats where my issue is.If i get a variable assigned then i can create the actual editing part.
I made a flip a coin game. It consists of three rows. I want the flip number what the flip was and the coin. I have all except the name of the flip like (heads,tails) I am only getting the number that I set for the flip. How do I get the flip to say the word 'heads' for 0 or 'tails' for 1 instead of just 0 or 1. I tried to do it in an echo I still received the number.this is the code I have
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="keywords" content="insert, keywords, here">
<meta name="description" content="Insert description here">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Coin</title>
</head>
<body>
<header>
<h1> Coin</h1>
</header>
<nav>
</nav>
<section>
<h2> Coin</h2>
<?php
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "</tr>";
echo "<thead>";
echo "<tbody>";
for($i=0; $i<20; $i++){
//0 is heads
//1 is tails
$result = rand(0, 1);
$count = $i+1;
echo "<tr>";
echo "<td>". $count . "</td>";
echo "<td>" . $result . "</td>";
if($result == 0){
echo "<td><img src=\"heads.jpg\" alt=\"heads\"></td>";
} else {
echo "<td><img src=\"tails.jpg\" alt=\"tails\"></td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</section>
</body>
</html>
You could map the number to the word you want. For example:
$headsTailsMapping = [];
$headsTailsMapping[0] = "heads"
$headsTailsMapping[1] = "tails"
...
echo "<td>" . $headsTailsMapping[$result] . "</td>";
I have a problem in searching a string which consist of space(manish pandey). php script used to search for only (manish) instead of full name (manish pandey).
I am using here get functions:
image, image2
<!doctype html>
<html lang="en-US">
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset="UTF-8">
</head>
<body>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "sample");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM qcdataa1 Where project_id='".$_GET['pid']."'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>sno</th>";
echo "<th>project_id</th>";
echo "<th>project_name</th>";
echo "<th>application</th>";
echo "<th>investigator</th>";
echo "<th>created_by</th>";
echo "<th>creation_date</th>";
echo "<th>last_modified</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" .$row['sno']. "</td>";
echo "<td>" .$row['project_id'] . "</td>";
echo "<td>" .$row['project_name']. "</td>";
echo "<td>" .$row['application']. "</td>";
echo "<td>" .$row['investigator']."</td>";
echo "<td>" .$row['created_by']. "</td>";
echo "<td>" .$row['creation_date']. "</td>";
echo "<td>" .$row['last_modified']. "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</body>
</html>
<!doctype html>
<html lang="en-US">
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset="UTF-8">
</head>
<body>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "sample");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
echo $_GET['iid'];
$sql = "SELECT * FROM qcdataa2 Where first_name='".$_GET['iid']."'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>sno</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>userid</th>";
echo "<th>password</th>";
echo "<th>emailid</th>";
echo "<th>creation_date</th>";
echo "<th>last_modified</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" .$row['sno']. "</td>";
echo "<td>" .$row['first_name'] . "</td>";
echo "<td>" .$row['last_name']. "</td>";
echo "<td>" .$row['userid']. "</td>";
echo "<td>" .$row['password']. "</td>";
echo "<td>" .$row['emailid']. "</td>";
echo "<td>" .$row['creation_date']. "</td>";
echo "<td>" .$row['last_modified']. "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</body>
</html>
Try sending data with urlencode() and decode it with urldecode()
First make your search term safe and then use it in query using LIKE instead of =.
$safe = mysqli_real_escape_string($link, $_GET['iid']);
$safe = trim($safe);
Then query could be,
$sql = "SELECT * FROM qcdataa2 Where first_name LIKE '$safe%' ";
Hey there I am new to HTML and PHP for time I have successfully done what i want to achieve but there is one thing that is going wrong: the case is:
Remember that this is not an assignment but I'm trying to do it for learning!
I have a mytable.php file where i have to show the records into table fetched from the mysql db
then i have an edit button so that i can edit my details
pressing edit button i go to edit.php and there i have to edit the record data in mysqli query where some condition should come from mytable.php
my code is:
\mytable.php//
session_start();
<!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>Q4Part a</title>
</head>
<body>
//and for mysqli_connection string will be
$con=mysqli_connect('localhost','root','','db');
if($con){
$result=mysqli_query($con,"select * from users");
if($result){
echo "<table>";
echo "<tr>";
echo "<td>";
echo "Username";
echo "</td>";
echo "<td>";
echo "Password";
echo "</td>";
/*echo "<td>";
echo "Pass";
echo "</td>";
echo "<td>";
echo "Phone";
echo "</td>";
echo "<td>";
echo "Address";
echo "</td>";
echo "<td>";
echo "Action";
echo "</td>"; */
echo "</tr>";
while($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>";
echo $row['myusername'];
echo "</td>";
echo "<td>";
echo $row['mypassword'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php'>edit</a>";
$_SESSION['username']=$row['myusername'];
//to go to the edit.php
echo "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo mysqli_error($con);
//so that if query won't run!
}
}
else{
echo mysqli_connect_error($con);
//or you can use exit that shows an error message and terminates the current script
//and or die which is a substitue for exit function and also shows the error message and terminates the current script
//so
exit("Connection error!");
//we can also use die(string);
die("Connection error");
}
</body>
</html>
\edit.php//
session_start();
$username=$_SESSION['username'];
<!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>Edit File</title>
</head>
<body>
<?php
$con=mysqli_connect('localhost','root','','db');
if($con){
$result=mysqli_query($con,"select * from users where myusername='".$username."'");
//where $username contains the current user that has been already signed in in the current time!//
if($result){
echo "<form action='save_edit.php' method='post'>";
echo "<table>";
echo "<th colspan='5'>";
echo "edit your details!";
echo "</th>";
while($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>";
echo "<input type='text' name='username' value='".$row['myusername']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='email' value='".$row['mypassword']."'>";
echo "</td>";
echo "<td>";
echo "<button type='submit'>Edit Changes</button>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
}else{
echo mysqli_error($con);
}
}
else{
echo mysqli_connect_error($con);
}
?>
</body>
</html>
all is working fine and good but it only gets the last result always when i click on the link 'edit'
Try using mysqli_fetch_assoc() instead of mysqli_fetch_array(). You're using a hashmap (associative array) so make sure you access it accordingly. Try doing var_dump($row); after your while statement to get an object print out of the array.
Lastly, you should never store passwords in plain text in the database. The password should be stored hashed, so you never have the ability to view the password again. When the user logs in, you check if the pass is valid by rehashing it and comparing for a match. Eg: $pass = md5($_POST['password'])
You should also consider using HTML5. Your doctype makes you use HTML4 and there's not many good reasons to do that anymore. I'd recommend changing to which sets it to HTML5.
$_SESSION['username'] will store the very last username you retrieved from the database/displayed on the page, so that's not going to work.
The standard way to do this is to use a unique identifier for each row in the database, and pass that as a URL parameter to edit.php.
If the username is always unique you can do something like this.
in mytable.php:
//echo "<a href='edit.php'>edit</a>";
echo "<a href='edit.php?username=" . urlencode($row['myusername']) . "'>edit</a>";
//$_SESSION['username']=$row['myusername'];
in edit.php:
//$username=$_SESSION['username'];
$username = $_GET['username'];
HI, All
I have created (copied) a website that can view the information inside my database. But the code i have, displays every single bot as well as members.
I am connecting it to local host and the database is phpbb3.
I need a sting to get rid of the bots which have a group_id of 6.
Here is my code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW.PHP
Displays all data from 'phpbb_users' table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM phpbb_users")
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_id'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['user_email'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Thank you,
In advanced.
Change this:
$result = mysql_query("SELECT * FROM phpbb_users")
To
$result = mysql_query("SELECT * FROM phpbb_users WHERE group_id <> 6")