use SQL data selected from html dropdown list - php

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.

Related

get id from buttons with same name in php

So I have a website that runs a cycle on a database as long as there is a record in it.
It also puts two buttons in each row within the table.
Each button has the same name.
If I press one of the buttons, how can I get the ID of that button (which points to a Youtube link from the database) to be included in a SQL query.
Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ruben MediaShare</title>
</head>
<body>
<form method='POST'>
<?php
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "Welcome to the member's area, " . $_SESSION['modname'] . "!";
} else {
echo "Please log in first to see this page.";
}
$link = mysqli_connect("localhost", "asd", "asd", "mediareq");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM requests";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>link</th>";
echo "<th>Twitch Név</th>";
echo "<th>Üzenet</th>";
echo "<th>Engedélyezés</th>";
echo "<th>Elutasítás</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
$link = $row['link'];
echo "<td>" . "<iframe width='560' height='315' src='$link' frameborder='0' allowfullscreen></iframe>" . "</td>";
echo "<td>" . $row['ttvname'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='enable'></input>" . "</td>";
echo "<td>" . "<input type='submit' id='$link' value='' name='eject'></input>" . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
if (isset($_POST['enable'])) {
$ID = $_POST[$link];
echo $ID;
}
} else{
echo "Még nem kaptál videót, várjál már egy kicsit vagy csak tolj egy F5öt Pog Pog Pog";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</form>
</body>
</html>
The website looks like this: https://imgur.com/a/MWVj7L5
If it can't be done that way, what can I do in this case to keep the loop but since it has to be dynamic, and somehow get it to pick the link from each button that it stores in the ID (or some other way)

How to get the name of what coin was tossed to show instead of a number

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>";

php script for searching a string which contains a space in the string

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%' ";

List & Delete records in mysql using php error

I have written a PHP code. I am not able to retrieve the results from database and delete as well. On Submit it just gives a blank page without throwing any error. I am new to this so please, reply even if u think its a silly question. Refer to the code and suggest me some changes which will make my code work.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT">
</li>
</ul>
</form>
</body>
</html>
//test.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '**');
define("DB_PASSWORD", '**');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from 'add' WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['desig'] . "</td>";
echo "<td><a href='test5.php?del=$row[empid]'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
//test5.php
<?php
define('DB_NAME', 'test');
define('DB_USER', '');
define("DB_PASSWORD", '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);
$value1 = $_POST['del'];
mysql_query("DELETE FROM add WHERE empid = '$value1'")
?>
<?php
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
New Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="result.php" method="post">
<ul>
<li>
Employee ID:</br>
<input type="text" name="eid">
</li>
<li>
<input type="submit" value="SUBMIT" name="ok">
</li>
</ul>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("fdd");
if(isset($_POST['ok'])){
$value1 = $_POST['eid'];
$res = mysql_query("SELECT * from `jobs` WHERE id = '".$value1."'");
echo "<table border='1'>
<tr><th>Name</th>
<th>EmployeeID</th><th>Address</th></tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['job_date'] . "</td>";
echo "<td>" . $row['client_code'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
output
Before querying the database you have to make a connection first. Follow the following code snippet example to first create a connection with mysql and then query the database.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
try this code, Don't use id directly in mysql query, before using your id directly to mysql query pass this from mysql_real_escape_string, it will clear id from sql injection. see SQL injection, REF,
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<title>EDIT SCREEN</title>
<form action="test4.php" method="post">
<ul>
<li>Employee ID:</br><input type="text" name="eid"></li>
<li><input type="submit" value="SUBMIT"></li>
</ul>
</form>
</body>
</html>
********* test4.php file ***********
<?php
$value1 = mysql_real_escape_string($_POST['eid']);
if($value1)
{
$Connection = mysql_connect('localhost','root','') or die(mysql_error());
$ConnectionDB = mysql_select_db($Connection) or die(mysql_error());
$res = mysql_query("SELECT * from `add` WHERE empid = '".$value1."'") or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Name</th>
<th>EmployeeID</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['empid'] . "</td>";
echo "<td>" . $row['add'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
NOTE: When your code work then remove this "or die(mysql_error())" with ";" it is only used in production mode.

ISNULL from Mysql not showing Results on webpage via PHP

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
...

Categories