Use ID to display data - php

I have two tables; Appointment table and Doctor table. I want to echo the information that is in the Doctor table (Name and Room) into the Appointment table using the Doctor_id. My code so far looks like this
Appointment.php
<!DOCTYPE html>
<?php
session_start();
?>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="my.js">
</script>
<!-- User-generated css -->
<style>
</style>
<!-- User-generated js -->
<script>
try {
$(function() {
});
} catch (error) {
console.error("Your javascript has an error: " + error);
}
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-theme="a" data-role="header">
<a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
Back
</a>
<a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
Home
</a>
<h3>
Book appointment
</h3>
</div>
<div data-role="content">
<h3>
Select date/time:
</h3>
<br />
<?php
{
mysql_connect("localhost" , "" , "") or die (mysql_error());
mysql_select_db("") or die(mysql_error());
$pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
//executes query on the database
$result = mysql_query ($query) or die ("didn't query");
//this selects the results as rows
$num = mysql_num_rows ($result);
//if there is only 1 result returned than the data is ok
if ($num == 1) {}
{
$row=mysql_fetch_array($result);
$_SESSION['Appointment_date'] = $row['Appointment_date'];
$_SESSION['Appointment_time'] = $row['Appointment_time'];
}
}
?>
<strong>Dates available</strong>
<select id="Availability" name="Availability">
<option value="0">--Select date--</option>
<option value="1"><?php echo $_SESSION['Appointment_date'];?></option>
</select>
<br />
<br />
<strong>Times available</strong>
<select id="Availability" name="Availability">
<option value="0">--Select time--</option>
<option value="2"><?php echo $_SESSION['Appointment_time'];?></option>>
</select>
<br />
<br />
<label for="textarea1">
Message GP
</label>
<textarea name="" id="textarea1" placeholder="">
</textarea>
</div>
</div>
</body>
</html>
Thanks!

You need a query like this one:
selec * from Appointment ap
inner join Doctor doc
on (ap.Doctor_id=doc.Doctor_id)
where ap.Patient_id=$pid
Then, this line:
$query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
Should be replaced with this one:
$query = "selec * from Appointment ap
inner join Doctor doc
on (ap.Doctor_id=doc.Doctor_id)
where ap.Patient_id=$pid";
Saludos ;)

Related

How do I make a value not reset each page reload?

I am trying to show people their profiles from my database using PHP.
But every time a button is clicked I want the person_id ($which_person) to go up by 1 so the next person's profile is shown. I don't know how to do that because every time I click a button the page reloads so the variable "which_person" gets reset to 1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets-putinhouse/css/style1.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Festive&display=swap" rel="stylesheet">
<title>Put people in a house</title>
</head>
<body>
<?php
include "db/connection.php";
$conn = create_connection();
$which_person = 1; //This resets to "1" every time the page reloads
$getSql = "select * from Person where person_id = 1;";
$data_labels = mysqli_query($conn, $getSql)->fetch_all(MYSQLI_ASSOC);
?>
<div class="pagesize">
<h1>Put people in a house</h1>
<img src="assets-putinhouse/images/create_account.png" alt="profilepic" class="image_person">
<?php
foreach($data_labels as $labels)
{
?>
<li class="labels" data-id="<?php echo $labels['person_id'];?>">
<?php echo $labels["firstname"] ?>
<br>
<br>
<?php echo $labels["secondname"] ?>
<br>
<br>
<?php echo $labels["gender"] ?>
<br>
<br>
<?php echo $labels["descriptie"] ?>
</li>
<?php
}
?>
<br>
<form method="GET">
<input type="submit" class="slytherin_button" value="Slytherin" name="slytherin_button_name">
<br>
<input type="button" class="gryffindor_button" value="Gryffindor" name="gryffindor_button_name">
<br>
<input type="button" class="hufflepuff_button" value="Hufflepuff" name="hufflepuff_button_name">
<br>
<input type="button" class="ravenclaw_button" value="Ravenclaw" name="ravenclaw_button_name">
<br>
<input type="button" class="nextperson_button" value="Go to next person">
<p class="text_firstname">Firstname: </p>
<p class="text_name">Name: </p>
<p class="text_gender">Gender: </p>
<p class="text_info">Info: </p>
<?php
if(isset($_GET['slytherin_button_name'])) {
onFunc($conn, $which_person);
}
if(isset($_GET['gryffindor_button_name'])) {
offFunc();
}
function onFunc($conn, $wich_person){
$sqlUpdate = "UPDATE Person SET slytherin = slytherin + 1 WHERE person_id like '$which_person';"; //this does the value "slytherin" of which_person go up by 1;
mysqli_query($conn, $sqlUpdate);
$wich_person = $wich_person + 1; //which person goes up by 1 so the next person will be shown but gets reset at the start
}
function offFunc(){
echo "Button is clicked";
}
?>
</div>
<script src="assets-putinhouse/js/script.js"></script>
</body>
</html>
´´´
(Untested) Instead of:
$wich_person = 1;
It seems you could do something like:
if (isset($_GET["id"]) {
$wich_person = (int)$_GET["id"] + 1;
}
else {
$wich_person = 1;
}
and then add the following to your form:
<form action="?<?php echo $wich_person ?>" method="GET">
...
</form>
And also, unless I'm mistaken it seems you've forgot to add </form> to your code.
In any event, this code should add the current ID to your URL every time you press submit, where then it gets increased by 1.
Edit: This is intended as a quick solution based on the code structure you provided, but upon reflection not sure if it's exactly what you're looking for. More robust solutions could be had, such as cookies or sessionStorage.

Create session from drop down list items

I have created a drop-down list that shows the members which have been added from the user. The script for this is this one:
<?php include('server.php'); ?>
<?php
$connect = mysqli_connect("localhost", "root", "", "geofence");
?>
<html>
<head>
<title>Add members</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br /><br />
<h2 align="center">Please, choose a family member</h2>
<br /><br />
<div class="form-group" align="center">
<form name="dropdown" action="" method="post">
<select name="choose" id="choose" width="150" style="width: 150px">
<option value="" selected disabled hidden>Choose Member</option>
<?php
$res=mysqli_query($connect,"select *
from member
where user_id=".$_SESSION['user_id']."");
while($row=mysqli_fetch_array($res)) {
?>
<option><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</form>
<input type="button" name="ok" id="ok" class="btn btn-info" value="OK" />
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#ok').click(function(){
$(location).attr('href', 'http://localhost/Houston/index.php')
});
});
</script>
Now, what I'm trying to do is to create a session ($_SESSION['member_id']) which refers to the name that is selected from the list. I have tried several things so far but with no success. An example is this:
if (isset($_POST['choose'])) {
if (count($errors)==0) {
$query="SELECT * FROM member where name=".$_POST['choose']."";
$result=mysqli_query($db,$query);
if (mysqli_num_rows($result)==1) {
$row=mysqli_fetch_assoc( $result);
$member_id=$row['member_id'];
$_SESSION['member_id'] = $row['member_id'];
}
}
}
I need the $_SESSION['member_id'] so I can use it on another file and populate my database. What is missing?
The problem is in the option tag, you have to specify de value of each option in the select tag , like this:
<option value="<?php echo $row["member_id"];?>"> <?php echo $row["name"]; ?> </option>
So, next you just have to declare the $_SESSION for your option value
if (isset($_POST['choose'])) {
$_SESSION['member_id']=$_POST["choose"];
}

session variable won't display

I am trying to send POST data into a SESSION variable
$_SESSION['plan'] = $_POST['plan']
info.php show that sessions are loaded.
browser -> inspect element appears to show sessions initialized but no key => values.
But I cannot get the session value to display.
no errors in apache logs.
main dynamic frame:
cat index.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="index,follow"/>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav class="nav">
<ul>
BLAH
</ul>
</nav>
<div id="content">
<?php
$pages_dir = 'pages';
if (!empty($_GET['p'])) {
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p=$_GET['p'];
if(in_array($p.'.inc.php', $pages)){
include($pages_dir.'/'.$p.'.inc.php');
}else {
echo 'Sorry, page not found.';
}
}else{
include($pages_dir.'/home.inc.php');
}
?>
</div>
</body>
</html>
User form page:
cat pages/payment.inc.php
<?php
//$_SESSION['plan'] = $_POST['plan'];
?>
<div id="content_pay">
<form action="pages/scheckout.php" method="post">
<div>
<input type="radio" id="plan1" name="plan" value="2500"> Beta membership <br><br>
<input type="radio" id="plan2" name="plan" value="3500"> VIP membership <br><br>
<label for="plan"> If you would like to pay another amount, enter the amount here:</label>
<input type="text" id="plan3" name="plan" />
<br>
<label for="invoice_num"> Enter the invoice number here:</label>
<input type="text" name="invoice_num" /> <br>
<input type="submit" value="submit" name="submit">
</div>
</form>
</div>
Basically this page is here just to capture the POST and assign it to SESSION then redirect with SESSION loaded:
cat pages/scheckout.php
<?php
session_start();
$_SESSION['plan'] = $_POST['plan'];
//needed to prevent weird race conditions
session_write_close();
header("location: ../index.php?p=scheckout");
die();
echo "<br>";
echo $_SESSION['plan'];
?>
Where is goes to:
cat pages/scheckout.inc.php
<?php
require_once('pages/sconfig.php');
?>
<div id="content_pay">
<form action="pages/scharge.php" method="post">
<div>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
myElem.setAttribute('data-amount', <?php $_SESSION['plan']; ?>);
myElem.setAttribute('data-description', <?php $_SESSION['plan']; ?>); >
</script>
</div>
</form>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<?php
echo $_SESSION['plan'];
?>
How do I get SESSIONS loaded from POST to display?
You have to start the session only once, before the html output. A session stays open until you use session_destroy or unset the $_SESSION variable, or close the browser. I suggest you, to delete all session_starts except the one in the index.php.
you use SESSION['plan'], but should use echo SESSION['plan']
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
myElem.setAttribute('data-amount', <?php echo($_SESSION['plan']); ?>);
myElem.setAttribute('data-description', <?php echo($_SESSION['plan']); ?>); >
</script>

filling combobox with data from php database

this is my code
<select>
<?php
$query = "SELECT * FROM 'sections'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}
?>
</select>
nothing happen I don't know why
this my page maybe there is wrong some where
<?php
ob_start();
session_start();
include('../includes/connect.php');
include('../includes/phpCodes.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>لوحة التحكم</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../css/mainstyle.css">
<link rel="stylesheet" type="text/css" href="css/controlstyle.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});
</script>
</head>
<body>
<div class="wrapper">
<?php headerCode(); ?>
<div class="content">
<div id="tabs">
<ul>
<li>اضافة موضوع</li>
<li>حذف موضوع</li>
<li>تعديل موضوع</li>
<li>التحكم بالاقسام</li>
</ul>
<div id="add">
<form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/from-data">
<br>
حدد القسم :
<select>
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}?>
</select><br>
عنوان الموضوع :<input type="text" name="title" class="mem-information"/><br>
الموضوع : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
الصورة :<input type="file" name="image"><br>
<input type="submit" value="إرسال" name="send" class="log" style="color: black">
</form>
</div>
<div id="remove">
<form method="POST" action="includes/remove.php" dir="rtl"><br>
حدد القسم :
<select name ="sectionsName">
<option value="">dd</option>
</select>
<input type="submit" value="حذف" name="send" class="log" style="color: black">
</form>
</div>
<div id="edit">
</div>
<div id="addDep">
</div>
</div>
</div>
</div>
</body>
</html>
sorry for my bad English
my table
The problem I see is you are using single quotes on your table name in your query, you should either use back tick or no back ticks for table name. Please try the following code:
<?php
$query = "SELECT * FROM `sections`";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
}
?>
Also start looking into using mysqli (http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), as mysql is deprecated.
Try this... And we are all Asuming the name of the column is "sectionsd" with the "d".
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["id"]."'>".$row["sectionName"]."</option>";
}?>
</select>
If it's not working it may be due to:
1) You're using a mysql_ function and you're not sending the parameter $link to the function. Like:
mysql_query($query, $connectionlink);
2) The table name is wrong
3) You have an error: Use mysql_query() or die(mysql_error()); to see what's going on
4) DO NOT use single quotes ' around your table name in the query
echo '
<option value='.$row["id"].'>'.$row["sectionName"].'</option>
';

Unable to insert data into database using PHP and J Query Mobile

I can insert data into the database for the several time before this. I'm using web matrix and XAMPP to develop. I have problem while dealing with ports but then after altering something it become okay, but the database does not receive data anymore. My other system using PHP that was been develop is running good but the problem is with the jQuery mobile.
This is the index.html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="my.css" />
<script src="http://code.jquery.com/jquery-1.7.2.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="my.js">
</script>
<!-- User-generated css -->
<style>
</style>
<!-- User-generated js -->
<script>
try {
$(function() {
});
} catch (error) {
console.error("Your javascript has an error: " + error);
}
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-theme="a" data-role="header" data-position="fixed">
<h3>
Student Uitm
</h3>
</div>
<div data-role="content">
<form action="http://localhost/student/save2db.php" method="post">
<label>Nama</label><input type="text" name="Nama">
<label>No Kad Pengenalan</label><input type="text" maxlength="12" name="icno">
<label>Jantina</label><select name="jantina">
<option value="L">Lelaki</option>
<option value="P">Perempuan</option>
</select>
<input name="sbt" type="submit" value="save" data-inline="true">
</form>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
<h3>
Footer
</h3>
</div>
</div>
</body>
</html>
and this is the save2db.php file:
<?php
$con=mysqli_connect("localhost","root"," ","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO student1 (Nama, icno, jantina)
VALUES
('$_POST[Nama]','$_POST[icno]','$_POST[jantina]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
The problems:
you check for the db connection, but you let the script run through even when it's failed
you are not being cautious enough with the received data
you don't log errors
internal problem symptoms leak out to users
Here's a possible alternative to your script taking care of these issues:
<?php
$con = mysqli_connect("localhost","root"," ","student");
// Check connection
if (mysqli_connect_errno()) {
trigger_error("Failed to connect to MySQL: " . mysqli_connect_error(),
E_USER_ERROR);
die("unable to complete request");
} else if (!(isset($_POST['Nama']) &&
isset($_POST['icno']) && isset($_POST['jantina'])) {
trigger_error( "incomplete POST data", E_USER_ERROR);
die("unable to complete request");
} else {
$nama = mysqli_real_escape_string($con, $_POST['Nama']);
$icno = mysqli_real_escape_string($con $_POST['icno']);
$jantina = mysqli_real_escape_string($con,$_POST['jantina']);
$sql = "INSERT INTO student1 (Nama, icno, jantina) VALUES ('$nama','$icno','$jantina')";
if (!mysqli_query($con,$sql)) {
trigger_error("query failed :" . mysqli_error($con), E_USER_ERROR);
die("unable to complete request");
}
echo "1 record added";
mysqli_close($con);
}
With the above modification done, check the php logs after unsuccessful updates to know the possible reasons of the failure.

Categories