I can't seem to view my transaction history? - php

Please help :(
<?php
include 'dbFunctions.php';
session_start();
$user_id = $_SESSION['user_id'];
?>
<table>
<td>
<b><label for="billinghistory">Billing History:</b></label>
<select name="billing_history"
The following codes can't seem to print my transaction history, I am not sure if I have chosen the correct fields.
<?php
$query3 = "SELECT * from billinghistory";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
while ($row3 = mysqli_fetch_array($result3)) {
$billing_name = $row3['billing_name'];
$billing_name_id = $row3['billing_name_id'];
?>
<option value="<?php echo $billing_name; ?> "><?php echo $billing_name ;?></option>
</select>
</td>
</table>

Try
while ($row3 = mysqli_fetch_array($result3,MYSQLI_ASSOC)) {

Related

Make AJAX on add and remove items to avoid reload

I have 3 php files in doing addition and deletion of medicine details. medicines detail are used to order drugs for each patient who comes for treatment. In order not to be a hassle, how do I prevent my application from needing to be reloaded with AJAX? The 3 files are: services.php , insertDetailMedicines.php and deleteDetail.php .
services.php
$data = mysqli_query($conn, "SELECT MAX(id_service) AS ids FROM tbl_services");
$final_data = mysqli_fetch_array($data);
$id1 = $final_data['ids'];
$id2 = substr($id1,3,3); //MR for Medical Record
$id3 = $id2 + 1;
$id4 = 'MR'.sprintf('%03s' , $id3); // <-- Auto generating unique id
<form method="POST" action="services.php">
<input type="hidden" name="id_medical_record" value="<?php echo $id4 ?>">
<select name="medicineName" id="medicineName" required>
<option value="">- Choose -</option>
<?php
$medicines = mysqli_query($conn, "SELECT * FROM tbl_medicines ORDER BY id_medicines ASC");
$m = mysqli_fetch_array($medicines);
while($m = mysqli_fetch_array($medicines)){ ?>
<option value="<?php echo $m['id_medicine'] ?>">
<?php echo $m['medicine_name'] ?>
</option>
<?php } ?>
</select>
<input type="text" name="qty_medicines" id="qty_medicines" value="1" required />
<button type="submit" name="add" style="cursor: pointer">ADD</button>
</form>
<table> <!--this is to display the drugs that have been added-->
<?php
$show_details = mysqli_query($conn, "SELECT * FROM tbl_detail_medicines LEFT JOIN tbl_medicines USING (id_medicine)");
$num = 1;
if(mysqli_num_rows($show_details) > 0)
{
while ($detail = mysqli_fetch_array($show_details))
{
?>
<tr>
<td>
<?php echo $num++.'.'; ?>
</td>
<td>
<?php echo $detail['medicine_name'] ?>
</td>
<td>
<?php echo $detail['qty'] ?>
</td>
<td>
<a href="deleteDetail.php?delete=<?php echo $detail['id'] ?>">
<b> X </b>
</a>
</td>
</tr>
<?php
}}
?>
</table>
insertDetailMedicines.php
<?php
if (isset($_POST['add'])) {
$idMR = $_POST['id_medical_record'];
$medicineName = $_POST['medicineName'];
$qty_medicines = $_POST['qty_medicines'];
$insert_detail = "insert into tbl_detail_medicines (id,id_service,id_medicine,qty)
VALUES
(null,'$idMR','$medicineName','$qty_medicines')";
if (mysqli_query($conn,$insert_detail)) {
//echo "inserting success!";
}
}
?>
deleteDetail.php
<?php
require 'koneksi.php';
if(isset($_GET['delete'])){$delete = mysqli_query($conn, "DELETE FROM tbl_detail_medicines WHERE id = '".$_GET['delete']."' ");
header('location:services.php');
}
?>
apppearance

Inserting selected value from populated mysql dropdown

I am trying to insert the selected value from a drop down that was populated from a reference table in my database. I followed a tutorial for a dynamic dropdown but now I would like to take the value and insert it. The problem is it keeps taking the echo the tutorial uses. Is there a way I can make that selected value a new variable? It currently inserts "< php echo $team_name"
<div>
<label>Home Team</label>
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
mysqli_query($db, "SELECT * FROM team_name");
// loop
foreach ($results as $team_name) {
?>
<option value="<php echo $team_name["cid"]; ?><?php echo $team_name["team_name"]; ?></option>
<?php
}
?>
</select>
How I attempted to insert:
$db = mysqli_connect('localhost', 'root', 'root', 'register');
if(mysqli_connect_errno())
{
echo "failed" . mysqli_connect_error();
}
//var_dump($_POST);
$home_team = mysqli_real_escape_string($db, $_POST['home_team']);
$home_team = $home_team;
$query = "INSERT INTO game_table (home_team)
VALUES('$home_team')";
mysqli_query($db, $query);
//echo $query;
//echo $home_team;
//header('location: index.php');
please follow this.
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($results)) {
?>
<option value="<php echo $row['cid']; ?>"><?php echo $row["team_name"]; ?></option>
<?php } ?>
</select>
may be this should work
Try this. There was a couple of missing " and ? in your code.
<select name="home_team" style="width:125px;">
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
foreach ($row = mysqli_fetch_assoc($results)) {
?>
<option value="<?php echo $row["cid"]; ?>">
<?php echo $row["team_name"]; ?>
</option>
<?php
}
?>
</select>

How to get data from the database and display it in a combobox in html

<tr>
<td>Subject:</td>
<td>
<select name="Subject">
<option value="<?php echo $Subject;?>"></option>
</select>
</td>
</tr>
<?php
$connection = mysqli_connect("localhost", "root", "", "phobiamming") or die (mysqli_error());
$sql = "SELECT * FROM section";
$result = mysqli_query($connection, $sql);
while ($row= mysqli_fetch_array($result)) {
echo $row['Subject'];
}
$connection->close();
?>
I can't display data from the DB to the combobox. I don't know if my code was right.
<tr>
<td>Subject:</td>
<td><select name="Subject">
<option value="Subject"><?php $connection = mysqli_connect("localhost", "root", "", "phobiamming") or die (mysqli_error());
// SQL query
$strSQL = "SELECT * FROM sections";
// Execute the query (the recordset $rs contains the result)
$rs = mysqli_query($connection, $strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysqli_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $row['Subject']. "<br>";
}
// Close the database connection
$connection->close(); ?></option>
</select></td>
</tr>
this is my updated code, I can already display the data from the DB but the another problem is the rows in the DB that will display, it can't create a line break
Put option value in your while loop
<tr>
<td>Subject:</td>
<td>
<select name="Subject">
<?php
$connection = mysqli_connect("localhost", "root", "", "phobiamming") or die
(mysqli_error());
$sql = "SELECT * FROM section";
$result = mysqli_query($connection, $sql);
while ($row= mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['$Subject']; ?>"><?php echo $row['$Subject']; ?></option>
<?php
}
$connection->close();
?>
</select>
</td>
</tr>

Deleting data in database with dropdown menu in PHP

I am struggling with deleting data in my database with my drop-down menu.
My drop-down menu looks like this
<form method="post" action="admin.php">
<h3>Delete a user</h3>
<select name="username">
$sql = mysqli_query($connection, "SELECT username FROM users");
while ($row = $sql->fetch_assoc()){
?>
<option value="username" name="username">
<?php echo $row['username']; ?></option>
<?php } ?>
<input type="submit" name="delete" value="Delete User">
</form>
And this is displaying the users all good like i want it, so here is the php for it
<?php
include('connect.php');
if(isset($_POST['delete'])) {
$username = $_POST['username'];
mysqli_query("DELETE FROM `users` WHERE `username` = '$username' ");
echo "User was deleted!";
}
?>
So when i hit the submit button "Delete User", it looks like i get sent to admin.php and nothing happens.
How can i fix this?
Thanks.
Replace name="username" from <option></option>
Echo value in value of option.
Connection variable missing in admin.php page
Updated Code
<select name="username">
<?php
$sql = mysqli_query($connection, "SELECT username FROM users");
while ($row = $sql->fetch_assoc()){?>
<option value="<?php echo $row['username']; ?>"><?php echo $row['username']; ?></option>
<?php }?>
</select>
admin.php
$stmt = $connection->prepare("DELETE FROM `users` WHERE `username` = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
1.<option value="username" name="username"> Need to be <option value="<?php echo $row['username']; ?>">
2.Connection variable is missing . Need to be:-
mysqli_query($connection,"DELETE FROM `users` WHERE `username` = '$username' ");
Modified code need to be:-
Form code:-
<?php
//comment these two lines when code started working fine
error_reporting(E_ALL);
ini_set('display_errors',1);
include('connect.php');
?>
<form method="post" action="admin.php">
<h3>Delete a user</h3>
<select name="username">
<?php
$sql = mysqli_query($connection, "SELECT username FROM users");
while ($row = mysqli_fetch_assoc($sql)){?>
<option value="<?php echo $row['username']; ?>"><?php echo $row['username']; ?></option>
<?php }?>
</select>
<input type="submit" name="delete" value="Delete User">
</form>
Php code:-
<?php
//comment these two lines when code started working fine
error_reporting(E_ALL);
ini_set('display_errors',1);
include('connect.php');
if(isset($_POST['delete'])) {
$username = $_POST['username'];
if(mysqli_query($connection,"DELETE FROM `users` WHERE `username` = '$username' ")){
echo "User was deleted!";
}
}
?>
Note:- Always do some error-reporting so that you will get error and rectify that.
Your query is vulnerable to SQL INJECTION so read about prepared statements and use them.
Change
<option value="username" name="username">
<?php echo $row['username']; ?></option>
To
<option value="<?php echo $row['username'] ?>" name="username">
<?php echo $row['username']; ?></option>
You are not putting the value in select option that's why nothing happens
Just replace
<option value="username" name="username">
<?php echo $row['username']; ?></option>
<?php } ?>
with
<option value="<?php echo $row['username']; ?>">
<?php echo $row['username']; ?></option>
<?php } ?>
It will work for you.

Fetch array for dropdown list with MySQL data in a form

How can i start it? I know that i need to make an mysql query, but how to transform data, in options in the dropdown list. And remember, it's inside a form, to send the result from a MySQL table.
(EDIT)
I am working inside a printf That is what i want:
<?php ob_start();
include('/../../config.php');
if(isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {
$edit_id = mysql_real_escape_string($_POST['edit_id']);
$result = mysql_query("SELECT username, password, nome, cidade, pais, base, isactive, admin, dov, checador, dinheiro, email, datanascimento, profissao, idivao, idvatsim, horas, rank FROM acars_users WHERE `id`='".$edit_id."'");
$resultdl = mysql_query("SELECT * FROM acars_hubs");
$data = mysql_fetch_array($result);
$dl = mysql_fetch_array($resultdl);
printf("<div align=\"center\">
<br><form method=\"post\" action=\"editar2.php\">
<p><font size=\"2\" face=\"Segoe UI, Arial, Helvetica, sans-serif\" align=\"center\">Modifique os campos que deseja para <strong>editar este membro.</font><br>
<br>
<table width=\"700\" border=\"0\" align=\"center\" >
<tr>
<td>Base Operacional:</td>
<td><label for=\"hub\"></label>
<select name=\"hub\">
<option>".$dl['name']."</option>
</select>
</td>
</td>
</tr>
</table></br></br>
<input name=\"edit_id\" value=\"$edit_id\" type=\"hidden\">
<input type=\"image\" src=\"img/Editar.PNG\" width='85' height='30'></form>
</form>
</table>
</div>
");
while ($data = mysql_fetch_array($result));
while ($dl = mysql_fetch_array($resultdl));
ob_end_flush();
?>
Do you mean something like this?
<select>
<?php while($row = mysql_query("SELECT * FROM table")){ ?>
<option><?=$row['column']; ?></option>
<?php } ?>
</select>
I've discovered the problem. My first $dl = mysql_fetch_array($resultdl, MYSQL_ASSOC); pulls the first row from the table. When I start while loop the mysql_fetch_array picks up with the next row. So, the solution is remove the first call. This is the final code:
<?php
include('../../../../../config.php');
if(isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {
$edit_id = mysql_real_escape_string($_POST['edit_id']);
$result = mysql_query("SELECT * FROM acars_users WHERE `id`='".$edit_id."'");
$data = mysql_fetch_array($result);
$resultdl = mysql_query("SELECT * FROM acars_hubs");
printf("<div align=\"center\">
<form method=\"post\" action=\"actions/actions_editar.php\">
");
while ($dl = mysql_fetch_array($resultdl, MYSQL_ASSOC)){
printf("
<option value=".$dl["id"].">".$dl["name"]."</option>;
");
}
printf("
<input name=\"edit_id\" value=\"$edit_id\" type=\"hidden\">
<input type=\"image\" src=\"../../images/botao_editar.PNG\" width='85' height='30'></form>
");
?>

Categories