PHP: global variable not being stored? - php

It turns out this has nothing to do with global variables (sorry discussed in another post).
I was trying to keep all the code on one page (self-processing), but I'm pretty sure it's not possible. Arranging the following code outlined in the answer below, on the same page, does not work. Figures.
*edited to add the whole script minus the CSS (unnecessary).
<!DOCTYPE html>
<head>
<title>Inventory Tables</title>
</head>
<style></style>
<body>
<?php //IRCinventoryhome.php
require("IRCpage.inc");
require_once 'IRCinventoryconfig.php';
$homepage = new IRCtemplate();
$homepage->Display();
session_start();
?>
<!-- Dropdown Menu for Table Selection -->
<div id="contentHeader">
<?php //Menu for table selection
ini_set('display_errors',1); error_reporting(E_ALL);
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($connection->connect_error) die($connection->connect_error);
$result = $connection->query("SHOW TABLES");
$table = array();
while ($row = $result->fetch_row()){
$table[] = $row[0];
}
$count = count($table);
?>
<div id="select">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<select name="value">
<?php for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
echo <<<_END
<pre>
<option value="$table[$pointer]">$table[$pointer]</option>
</pre>
_END;
}
?>
</select>
<input type="submit" value="go">
</form>
</div> <!-- End .select -->
</div> <!-- End #contentHeader -->
<div id="content">
<!-- Code for Database Tables and Actions -->
<?php //inventory mysql tables
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['value'])) {
$thisTable = $_POST['value'];
global $thisTable;
ini_set('display_errors',1); error_reporting(E_ALL);
$queryColumns = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
if (!$resultColumns) die ("Database access failed: " . $connection->error);
$columns = array();
while ($column = $resultColumns->fetch_row()){
$columns[] = $column[0];
}
echo "<div id=\"table\"><table class=\"CSSTableGenerator\" >\n";
$count = count($columns);
$insertColumns = array();
for ($pointer = 1 ; $pointer < $count ; ++$pointer) {
$insertColumns[] = $columns[$pointer];
}
for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
echo "<th scope=\"col\" bgcolor=\"#efefef\">";
echo $columns[$pointer];
echo "</th>";
}
echo "<th>ACTIONS</td>";
$queryRows = "SELECT * FROM $thisTable";
$resultRows = $connection->query($queryRows);
if (!$resultRows) die ("Database access failed: " . $connection->error);
$rows = $resultRows->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$resultRows->data_seek($j);
$row = $resultRows->fetch_array(MYSQLI_NUM);
$count = count($row);
echo "<tr>";
for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
echo "<td>";
echo $row[$pointer];
echo "</td>";
}
?>
<td>
<input action="<?php echo $_SERVER['PHP_SELF']?>" type="submit" value="edit" name="edit">
<input action="<?php echo $_SERVER['PHP_SELF']?>" type="submit" value="delete" name="delete">
</td>
</tr>
<?php
}
echo "</table></div>"; //end table, end content
$connection->close();
}
?>
<!-- FORM FOR ADDING ROWS TO CURRENT TABLE -->
<div id="table">
<table id="formTable">
<form name="addRow" action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type="hidden" name="control">
<td>
Add row?
</td>
<?php for ($pointer = 1 ; $pointer < $count ; ++$pointer) { ?>
<td>
<input type="text" name="<?php echo $columns[$pointer];?>">
</td>
<?php } ?>
<td>
<input type="submit" value="go">
</td>
</form>
</table>
</div> <!-- end form, end table, end content -->
<?php
global $thisTable;
echo $thisTable;
if (isset($_POST['control'])) {
global $thisTable;
echo $thisTable;
$valuesArray = array();
if (isset($_POST)) {
$valuesArray = $_POST;
}
$columnsArray = array_keys($valuesArray);
//array_splice($columnsArray, 0, 1);
$columnsString = implode(", ", $columnsArray);
print_r($columnsString);
$insertValues = array();
foreach($valuesArray as $values) {
$insertValues[] = $values;
}
$valuesString = implode(" ", $insertValues);
$valuesString = "'".$valuesString."'";
$valuesString = str_replace(" ", "', '", $valuesString);
$valuesString = substr($valuesString, 3);
print_r($valuesString);
/*
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($connection->connect_error) die($connection->connect_error);
$queryInsert = "INSERT INTO $thisTable ($columnsString) VALUES ($valuesString)";
$result = $connection->query($queryInsert);
if (!$result) echo "INSERT failed: $query<br>" .
$connection->error . "<br><br>";
$connection->close();
*/
}
?>
</div> <!-- End #content -->
</body>
</html>

After 20 hours or so I'm still not sure if this possible to do all on the same page. But this solution works...
Page one:
<!DOCTYPE html>
<head>
<title>Interactive Resource Center Inventory</title>
<meta charset="UTF-8">
</head>
<script src="../_js/jquery.min.js"></script>
<script src="../_js/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$('form').submit(function() {
$('input[type=submit]')
prop('disabled',true);
});//end submit
var max_fields = 4; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>Column: <input type="text" name="mytext[]"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});//end ready
</script>
<style>
#table {
margin-top:2px;
}
#formTable {
width:100%;
margin:0px;padding:0px;
border:1px solid #000000;
border-bottom:none;
border-left:none;
border-right:none;
}
#formTable td{
vertical-align:middle;
border:1px solid #000000;
border-width:0px 1px 1px 0px;
text-align:left;
padding:7px;
width:100px;
font-size:14px;
font-family:arial;
font-weight:normal;
color:#000000;
}
#formTable input[type="text"]{
width:95%;
}
.CSSTableGenerator {
margin:0px;padding:0px;
width:100%;
border:1px solid #000000;
border-top:none;
border-right:none;
border-left:none;
}
.CSSTableGenerator table{
width:100%;
height:100%;
margin:0px;padding:0px;
}
.CSSTableGenerator tr:nth-child(odd){ background-color:#e5e5e5; }
.CSSTableGenerator tr:nth-child(even) { background-color:#ffffff; }
.CSSTableGenerator th{
border:1px solid #000000;
border-width:0px 1px 1px 0px;
width:100px;
}
.CSSTableGenerator td{
vertical-align:middle;
border:1px solid #000000;
border-width:0px 1px 1px 0px;
text-align:left;
padding:7px;
width:100px;
font-size:14px;
font-family:arial;
font-weight:normal;
color:#000000;
}
.CSSTableGenerator tr:last-child td{
border-width:0px 1px 0px 0px;
}
.CSSTableGenerator tr td:last-child{
border-width:0px 0px 1px 0px;
}
.CSSTableGenerator tr:last-child td:last-child{
border-width:0px 0px 0px 0px;
}
.CSSTableGenerator tr:first-child td{
background:-o-linear-gradient(bottom, #cccccc 5%, #b2b2b2 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #cccccc), color-stop(1, #b2b2b2) ); background:-moz-linear-gradient( center top, #cccccc 5%, #b2b2b2 100% ); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#cccccc", endColorstr="#b2b2b2"); background: -o-linear-gradient(top,#cccccc,b2b2b2);
background-color:#cccccc;
border:0px solid #000000;
text-align:center;
border-width:0px 0px 1px 1px;
font-size:14px;
font-family:arial;
font-weight:bold;
color:#000000;
margin-top:-2px;
</style>
<body>
<?php //IRCinventoryhome.php
require("IRCpage.inc");
require("IRCinventoryfunctions.php");
require_once 'IRCinventoryconfig.php';
$homepage = new IRCtemplate();
$homepage->Display();
session_start();
$_SESSION['value'] = $_POST['value'];
$thisTable = $_SESSION['value'];
var_dump($_SESSION);
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
if ($connection->connect_error) die($connection->connect_error);
global $connection;
$result = $connection->query("SHOW TABLES");
$table = array();
while ($row = $result->fetch_row()){
$table[] = $row[0];
}
$count = count($table);
?>
<!-- Dropdown Menu for Table Selection -->
<div id="contentHeader">
<div id="select">
<form action="http://localhost:8888/IRC/IRCinventoryhometest.php" method="POST">
<select name="value">
<?php for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
echo <<<_END
<pre>
<option value="$table[$pointer]">$table[$pointer]</option>
</pre>
_END;
}
?>
</select>
<input type="submit" name="go">
</form>
</div> <!-- End .select -->
</div> <!-- End #contentHeader -->
<div id="content">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$thisTable = "OUTGOING";
$queryColumns = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
if (!$resultColumns) die ("Database access failed: " . $connection->error);
global $resultColumns;
$queryRows = "SELECT * FROM $thisTable";
$resultRows = $connection->query($queryRows);
if (!$resultRows) die ("Database access failed: " . $connection->error);
global $resultRows;
$rows = $resultRows->num_rows;
global $rows;
global $connection;
drawTable();
$connection->close();
} else {
if(isset($_POST['value'])) {
$thisTable = $_POST['value'];
}
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
if ($connection->connect_error) die($connection->connect_error);
global $connection;
$queryColumns = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
if (!$resultColumns) die ("Database access failed: " . $connection->error);
global $resultColumns;
$queryRows = "SELECT * FROM $thisTable";
$resultRows = $connection->query($queryRows);
if (!$resultRows) die ("Database access failed: " . $connection->error);
global $resultRows;
$rows = $resultRows->num_rows;
global $rows;
global $connection;
drawTable();
?>
<!-- FORM FOR ADDING ROWS TO CURRENT TABLE -->
<div id="table">
<table id="formTable">
<form name="addRow" action="http://localhost:8888/IRC/IRCprocessinventory.php" method="POST">
<input type="hidden" name="control">
<td>
Add row?
</td>
<td></td>
<?php for ($pointer = 2 ; $pointer < $countColumnsGlobal ; ++$pointer) { ?>
<td>
<input type="text" name="<?php echo $columns[$pointer];?>">
</td>
<?php } ?>
<td>
<input type="submit" value="submit">
</td>
</form>
</table>
</div> <!-- end table -->
<?php
}
?>
</div> <!-- End #content -->
</body>
</html>
Page 2:
<?php
require_once 'IRCinventoryconfig.php';
ini_set('display_errors',1); error_reporting(E_ALL);
session_start();
if(isset($_SESSION['value'])) {
$thisTable = $_SESSION['value'];
$valuesArray = array();
if (isset($_POST)) {
$valuesArray = $_POST;
}
$columnsArray = array_keys($valuesArray);
array_splice($columnsArray, 0, 1);
$columnsString = implode(", ", $columnsArray);
$insertValues = array();
foreach($valuesArray as $values) {
$insertValues[] = $values;
}
$valuesString = implode("','", $insertValues);
$valuesString = "'".$valuesString."'";
$valuesString = substr($valuesString, 3);
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
if ($connection->connect_error) die($connection->connect_error);
$queryInsert = "INSERT INTO $thisTable ($columnsString) VALUES ($valuesString)";
$result = $connection->query($queryInsert);
if (!$result) echo "INSERT failed: $query<br>" .
$connection->error . "<br><br>";
else echo "Successful entry";
$connection->close();
}
?>

Related

How to export data from my database and import it in CSV file in the correct format

I'm trying to search for some data then download it as CSV file, but when I click download button to download the data I have found, it's writing all my HTML code at the beginning of the file then it writes my data in the downloaded file. So how can I get only my data without writing the html code with it?.
Code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "CNG492";
$message = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<html>
<head>
<title>Search By Keyword</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Upload Data Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<style>
table {
font-family: Raleway;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
body
{
background-color: #722f37;
}
.btn{
background-color: #722f37;
color: #ffffff;
border: none;
padding: 10px 20px;
font-size: 17px;
font-family: Raleway;
cursor: pointer;
}
.box
{
background-color: #ffffff;
margin: 100px auto;
font-family: Raleway;
padding: 40px;
width: 80%;
min-width: 300px;
}
.has-error
{
border-color:#cc0000;
background-color:#ffff99;
}
.form{display:block;width:100%;height:34px;padding:6px 12px;font-size:14px;line-height:1.42857143;color:#555;background-color:#fff;background-image:none;border:1px solid #ccc;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075);box-shadow:inset 0 1px 1px rgba(0,0,0,.075);-webkit-transition:border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s}
</style>
</head>
<body>
<div class="container box">
<br />
<h1 align="center">Search</h1><br />
<form method="post" id="search_form">
<div class="form-group">
<label>Keyword:</label>
<input type="text" name="keyword" id="keyword" class="form-control"/>
<span id="error_keyword" class="text-danger"></span>
</div>
<div align="inline">
<button type="button" name="find_keyword" id="find_keyword" class="btn btn-dark btn-lg">Find</button>
<button type="button" name="adv_search" id="adv_search" class="btn btn-dark btn-lg">Advance Search</button>
</div>
<br>
<?php
if(isset($_POST["keyword"])){
$keyword = mysqli_real_escape_string($conn,$_POST["keyword"]);
$dataset_result = -1;
//Search for keyword inside tables
$search_query = "SELECT dataset_id,title,description FROM dataset WHERE dataset_id IN (SELECT dataset_id FROM dataset WHERE title LIKE '%{$keyword}%' OR collector LIKE '%{$keyword}%' OR description LIKE '%{$keyword}%' OR d_procedure LIKE '%{$keyword}%'
UNION DISTINCT
SELECT dataset_id FROM group_table WHERE group_desc LIKE '%{$keyword}%'
UNION DISTINCT
SELECT dataset_id FROM material WHERE m_type LIKE '%{$keyword}%' OR URI LIKE '%{$keyword}%'
UNION DISTINCT
SELECT dataset_id FROM task WHERE type LIKE '%{$keyword}%' OR description LIKE '%{$keyword}%'
UNION DISTINCT
SELECT dataset_id FROM dataset WHERE eq_id IN (SELECT e.eq_id FROM tracker t,equipment e WHERE t.tracker_id=e.tracker_id AND (t.brand LIKE '%{$keyword}%' OR t.model LIKE '%{$keyword}%')))";
$search_result = $conn->query($search_query);
$dataset_result = mysqli_num_rows($search_result);
if ($dataset_result == 0) {
echo '
<div class="alert alert-success">
No reasults found.
</div>
';
}else{
echo '
<h4>Results</h4>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Dowload Link</th>
</tr>';
while ($row = mysqli_fetch_assoc($search_result)) {
echo '<tr>
<td>'.$row["title"].'</td>
<td>'.$row["description"].'</td>
<td><button type="submit" name="download" id="download" value="'.$row["dataset_id"].'">Dowload</button></td>
</tr>';
}
echo '</table>
';
if (isset($_POST["download"])) {
$d_id = $_POST["download"];
include 'download.php';
}
}
}
mysqli_close($conn);
?>
</form>
</div>
</body>
</html>
<script>
$(document).ready(function() {
$('#find_keyword').click(function(){
var error_keyword = '';
if($.trim($('#keyword').val()).length == 0){
error_keyword = 'Enter a keyword please.'
$('#error_keyword').text(error_keyword);
$('#keyword').addClass('has-error');
}else
{
error_keyword = '';
$('#error_keyword').text(error_keyword);
$('#keyword').removeClass('has-error');
}
if(error_keyword != ''){
return false;
}else{
$('#find_keyword').attr("disabled", "disabled");
$(document).css('cursor', 'prgress');
$("#search_form").submit();
}
});
})
</script>
Download.php
<?php
//include database configuration file
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "CNG492";
$message = "";
// Create connection
$connect = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//get records from database
$query = $connect->query("SELECT * FROM dataset");
if($query->num_rows > 0){
$delimiter = ",";
$filename = "members_" . date('Y-m-d') . ".csv";
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('title', 'description');
fputcsv($f, $fields, $delimiter);
//output each row of the data, format line as csv and write to file pointer
while($row = $query->fetch_assoc()){
$lineData = array($row['title'], $row['description']);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
}
exit;
?>
The whole problem is then that you echo some html and inside it include download.php
echo '
<h4>Results</h4>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Dowload Link</th>
</tr>';
while ($row = mysqli_fetch_assoc($search_result)) {
echo '<tr>
<td>'.$row["title"].'</td>
<td>'.$row["description"].'</td>
<td><button type="submit" name="download" id="download" value="'.$row["dataset_id"].'">Dowload</button></td>
</tr>';
}
echo '</table>
';
if (isset($_POST["download"])) {
$d_id = $_POST["download"];
include 'download.php';
}
just put a link to the download.php instead of including it.
echo '
<h4>Results</h4>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Dowload Link</th>
</tr>';
while ($row = mysqli_fetch_assoc($search_result)) {
echo '<tr>
<td>'.$row["title"].'</td>
<td>'.$row["description"].'</td>
<td><button type="submit" name="download" id="download" value="'.$row["dataset_id"].'">Dowload</button></td>
</tr>';
}
echo '</table>
';
if (isset($_POST["download"])) {
$d_id = $_POST["download"];
echo 'Download File';
}
If you want to redirect user after submitting form to the download.php automatically then you may use attribute action in form.
See how it is done HTML Attribute where a redirection is set to the page /action_page.php.

Updating and deleting in a same html form

I have added update and delete button in the same form using following codes. Deletion is working perfectly. But updating is again not taking the value of "id".
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dp = "tool";
$dp= new mysqli($servername, $username, $password, $dp) or die("Unable to connect");
//echo"Great work";
?>
<!DOCTYPE html>
<html>
<head>
<title>registration</title>
<meta charset="UTF-8">
<link href="site.css" rel="stylesheet">
<div align="center">
<link rel="stylesheet" href="mine.css"/>
<table border="0" align="center" style="border-spacing: 40px 20px;">
<align="center"> <td>
</head>
<body bgcolor=" #b3ffe0">
<style>
html {
font-family: "Lucida Sans", sans-serif;
}
ul li {display: block;position: relative;float: left;border:1px }
ul li a {display: block;text-decoration: none; white-space: nowrap;color: #fff;}
ul {
list-style-type: none;
padding: 2px ;
margin-left: auto;
background-color: #666;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 10px 20px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
</style>
</head>
<body>
<form method="post">
<ul>
<li><a class="active" href="df1.php">Disease</a></li>
<li><a href="drug.php" >Drug</a></li>
<li>Interaction</li>
Alternate Drug
</ul>
<?php
$query = "SELECT * FROM disease;";
$result = mysqli_query($dp, $query);
echo "<table border=5>
<tr>
<th>Disease ID</th>
<th>Disease</th>
<th>Sub Disease</th>
<th>Associated Disease</th>
<th>Ethinicity</th>
<th>Source</th>
<th>Edit</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row{'id'}."</td>";
echo "<td>".$row{'Disease'}."</td>";
echo "<td>".$row{'SubDisease'}."</td>";
echo "<td>".$row{'Associated_Disease'}."</td>";
echo "<td>".$row{'Ethinicity'}."</td>";
echo "<td>".$row{'Source'}."</td>";
echo "<td><input type='radio' name='id' value='".$row[id]."'></td>";
echo "</tr>";}
echo "</table>";
// $selectedRow=$_POST['id'];
?>
<div>
<table border="0" align="center" style="border-spacing: 40px 30px;">
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="4" WIDTH="40%">
</br><center>
<button style="color: red">Add</button>
<input type = 'submit' value = 'Update' name = 'submitupdate'>
<input type = 'submit' value = 'Delete' name = 'submitdelete'>
</center></TABLE>
<?php
if(isset($_POST[submitupdate]))
{
header ("Location: http://localhost/card/edit3.php");
}
if ($_POST[submitdelete])
{
$conn = mysqli_connect('localhost','root','','tool');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
//
$sql="DELETE FROM disease WHERE id=".$_POST['id'];
echo "Data deleted successfully";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
?>
</body>
</html>
Edit3.php
<?php
$conn = mysqli_connect('localhost','root','','tool');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
$query = "SELECT * FROM disease where id=".$_POST['id'];
$result = mysqli_query($conn, $query);
$count= mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
echo $count;
?>
<form action="update.php" method="post">
<input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
Disease (ICD10) <select id= "Disease" name="Disease">
<option value="Certain infectious and parasitic diseases">Certain infectious and parasitic diseases</option>
<option value="Neoplasms">Neoplasms</option>
<option value="Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism ">Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism</option>
SubDisease<input type="text" name="SubDisease" value="<?php echo $row['SubDisease'];?>"/>
Associated Disease<input type="text" name="Associated_Disease" value="<?php echo $row['Associated_Disease'];?>"/>
<td>Ethinicity<input type="text" list="Ethinicity" id="color" name="Ethinicity" value="<?php echo $row['Ethinicity'];?>" style="width:100px;">
<datalist id="Ethinicity">
<option value="Indian">
<option value="American">
<option value="Srilankan">
</datalist>
</td>
Source<input type="text" name="Source" value="<?php echo $row['Source'];?>"/>
<input type="submit" value="update">
</form>
update.php
<?php
$conn = mysqli_connect('localhost','root','','tool');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
$disease = $_POST['Disease'];
$SubDisease = $_POST['SubDisease'];
$Associated_Disease = $_POST['Associated_Disease'];
$Ethinicity = $_POST['Ethinicity'];
$Source = $_POST ['Source'];
$id = $_POST ['id'];
$update= "Update disease set Disease='".$disease."', SubDisease='".$SubDisease."', Associated_Disease='".$Associated_Disease."', Ethinicity='".$Ethinicity."', Source='".$Source."' where id=".$_POST["id"];
if(!mysqli_query($conn,$update))
echo mysqli_error;
?>
And drop down of Disease, is also not getting reading the databse value and not getting display in the editing page.

How To Retrieve Column's Data of a Table, Store It Into An Array, and Finally Echo The Array

Here's what I'm basically trying to do:
Retrieve the values of a column
Store those values into an array in PHP
Echo each value with a line break in between each value
Here's my attempted code:
<?php
$connection = mysqli_connect("localhost", "root", "bruhfrogzombie098", "growtapi_social");
if (!$connection) {
die("Failed to connect to MYSQL: " . mysqli_connect_errno());
};
$members = mysqli_query($connection, "SELECT Username FROM s_users");
$members_status = mysqli_query($connection, "SELECT Status_Content FROM s_users");
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
};
$members_status_array = array();
while ($status = mysqli_fetch_assoc($members_status)) {
$members_status_array[] = $status;
};
?>
And this is where I want to echo out the values:
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?php echo $members_array['$member'];
echo "<br />";
?>
</div>
</center>
I don't receive any errors, but the problem is that nothing shows up in the div, meaning that I either didn't retrieve the data properly or didn't use it right.
Note: I've finally moved on to writing up-to-date code, so I hope no one here comments that somewhere in this code I have outdated code ( ͡° ͜ʖ ͡°)
$members_array = array();
while ($member = mysqli_fetch_assoc($members)) {
$members_array[] = $member;
//$members_array is array and $member is array so $members_array like $members_array[][];
};
<center>
<h1>Members Directory</h1>
<div style="width: 20%; height; 75%; border: 3px solid black; margin: auto; overflow: hidden; overflow-y: scroll;">
<?
$member_count = count($members_array);
for( $i = 0 ; $i < $member_count ; $i++ ){
echo $member_array[$i]['Username'];
echo "<br />";
}
?>
</div>
</center>

How can I make a select statement appear when another select statement is selected

So my "type1" select statement will select an issue category, and then based on that issue I want a dropdown of employees that deal with that issue in another drop down. Also how do I make all the hidden select statements occupy the same space?
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>IssueReport</title>
<script src = "../../jquery.js"></script>
<style>
body{
padding:0;
margin:0;
}
#box {
padding: 5px;
color:black;
margin: 0 auto;
border-style: solid;
border-color: #0000E6;
border-width: 2px;
background-color: #58C6EB;
width: 162px;
height: 687px;
float: left;
border-radius: 10px;
}
.word{
color: #0000E6;
}
.buttons{
background-color: #CCFFFF;
border-radius: 5px;
}
</style>
<script>
function showForm(){
var issue = document.getElementById("type1").value;
if(issue == 'Program Glitch'){
document.getElementById("IT1").style.display = "block";
}
$("#type1").onchange(function{
});
}
$(document).ready(function(){
$("#sub").click(function(){
var user_issue = $("#issue").val();
var user_priority = $("#priority").val();
var user_type = $("#type1").val();
var user_author = $("#author").val();
$.post("BugReport.php",{issue:user_issue,priority:user_priority,type1:user_type,author:user_author},function(data){
$("#result").html(data);
});
});
$("#sub").click(function(){
document.getElementById('issue').value='';
$('#type1').prop('selectedIndex', 0);
$('#priority').prop('selectedIndex', 1);
});
});
</script>
</head>
<body>
<div id="box">
<h3 class = "word" style = "margin:10px 30px 30px 30px;">Issue Report</h3>
<div class = "word" style = "width: 100px; margin-left: auto; margin-right: auto">Type Of Issue:</div>
<div style = " max-width: 150px; margin-left: auto; margin-right: auto; padding: 2px">
<form action="BugReport.html" method="post">
<select onchange = "showForm()" class = "buttons" name = "type1" id = "type1" style = 'max-width: 150px;'>
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$database = "database";
$con = mysqli_connect($servername,$username,$password,$database);
if($con->connect_error){
die("Connection failed " . $con->connect_error);
}
$sql1 = "select issue_name, issue_description from Issue";
$result = mysqli_query($con,$sql1);
while ($row = mysqli_fetch_array($result)) {
$issue = $row['issue_name'];
$des = $row['issue_description'];
echo "<option value = '$issue' title = '$des'>$issue</option>";
}
?>
</select>
</form></div>
<form action="BugReport.html" method="post">
<div id = "Buyers1" style = "visibility:hidden;"><select class = "buttons" name = "author" id = "Buyers" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = '1'";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></div></form>
<div id = "Operations1" style = "visibility:hidden;"><form action="BugReport.html" method="post">
<select class = "buttons" name = "author" id = "Operations" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = 2";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></form></div>
<div id = "IT1" style = "display:none;"><form action="BugReport.html" method="post">
<select onchange = "showForm()" class = "buttons" name = "author" id = "IT" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = 3";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></form></div>
<div id = "CustomerService1" style = "visibility:hidden;"><form action="BugReport.html" method="post">
<select class = "buttons" name = "author" id = "CustomerService" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = 4";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></form></div>
<div id = "HR1" style = "visibility:hidden;"><form action="BugReport.html" method="post">
<select class = "buttons" name = "author" id = "HR" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = 5";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></form></div>
<div id = "Logistics1" style = "visibility:hidden;"><form action="BugReport.html" method="post">
<select class = "buttons" name = "author" id = "Logistics" style = "margin: 0px 30px 0px 30px;">
<?php
$sql = "select first, last from Employee where department_id = 6";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$name = $row['first'] . ' ' . $row['last'];
echo "<option value = '$name'>$name</option>";
}
?>
</select></form></div>
<form action="BugDisplayAndReply.html" method="post">
<label></label><br>
<textarea style = "max-width: 156px; background-color: #F3F9FF; border-color: #0000E6;" cols="20" rows="34" name="issue" id = "issue" placeholder = "Enter Your Issue Here" "></textarea></form><br>
<div class = "word" style = "width: 50px; margin-left: auto; margin-right: auto">Priority:</div>
<div style = "width: 70px; margin-left: auto; margin-right: auto; padding: 2px">
<form action="BugReport.html" method="post">
<select class = "buttons" name = "priority" id = "priority">
<option value = "Low">Low</option>
<option value = "Regular" selected>Regular</option>
<option value = "High">High</option>
<option value = "Urgent">Urgent</option>
</select>
</form></div>
<div style = "width: 50px; margin-left: auto; margin-right: auto; padding: 2px">
<input class = "buttons" type ="submit" name = "sub" value = "Submit" id = "sub"></div>
<div id="result"></div>
</div>
</body>
</html>

How do I dipslay the attributes of the tables from a lists of database tables when I click that table? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
<td>
<p>
<div class="column">
<p><b>SELECT A DATABASE</b></p>
<p> <?php echo$this->form->getInput('Database_1');
include "db1.js.php" ?>
</div>
</td>
<td>
<p>
<div class="column" id="tbDiv">
<p><b>TABLES</b></p>
<select name="List of Tables" size="25" multiple id='table1' name='table1' title='List of Tables' class='inputbox'>
<option >Tables will be listed here...</option></select>
</div><p>
</td>
This the code in displaying the tables in a list from a selected database. I want to display the attributes of the table when i click it.
Try this using ajax and php
<?php
$db = new PDO('mysql:host=localhost;dbname=mysql','root','');
$dbs = $db->query( 'SHOW DATABASES' );
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
li:hover{
cursor: pointer;
}
#list{
float: left;
width: 30%;
}
#table{
float: right;
width: 68%;
}
#main{
width: 70%;
margin: 0 auto;
}
table,th,td{
border: 1px solid #000;
}
th{
width: 120px;
background-color: #000;
color: #fff;
text-transform: capitalize;
}
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<div id="main">
<div id="list">
<select id="dbase" onchange="getTables(this.value)">
<option>Select databse</option>
<?php
while( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
{
echo '<option>',$db,'</option>';
}
?>
</select>
<!-- displaying dropdown ************* -->
<option id="bulk" onchange="drop(this.value)"></option>
</div>
<div id="table"></div>
</div>
<script type="text/javascript">
//*************update for displaying dropdown menu *******
function drop(table){
var table = table;
var db = dbase.value;
var data = new XMLHttpRequest();
data.open("POST","list.php");
data.onreadystatechange = function(){
if(data.readyState === 4 && data.status === 200){
document.getElementById('table').innerHTML = data.responseText;
}
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send('table='+table+'&dbase='+db);
}
//***********end of update **********
function getTables(table){
var data = new XMLHttpRequest();
data.open("POST","get_tables.php");
data.onreadystatechange = function(){
if(data.readyState === 4 && data.status === 200){
document.getElementById('bulk').innerHTML = data.responseText;
}
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send('table='+table);
}
</script>
</body>
</html>
create get_tables.php
<?php
$dbname = $_POST['table'];
$db = new PDO("mysql:host=localhost;dbname=$dbname","root","");
$query = $db->prepare('show tables');
$query->execute();
$tabe_in = 'Tables_in_'.$dbname;
$results = $query->fetch(PDO::FETCH_ASSOC);
while ($results != null) {
// change <li> to <option> here **********
echo '<option>',$results[$tabe_in],'</option>';
$results = $query->fetch(PDO::FETCH_ASSOC);
}
?>
and res.php
<?php
$table = $_POST['table'];
$dbname = $_POST['dbase'];
$sql = "SHOW COLUMNS FROM $table";
$db = new PDO("mysql:host=localhost;dbname=$dbname","root","");
$table_headers = array();
$results = $db->prepare($sql);
$results->execute();
$res = $results->fetch();
?>
<table>
<tr>
<?php
while ($res != null) {
echo '<th>',$res['Field'],'</th>';
$table_headers[] = $res['Field'];
$res = $results->fetch();
}
?>
</tr>
<?php
$query = $db->prepare("SELECT * FROM $table");
$query->execute();
$rs = $query->fetchAll();
foreach ($rs as $value) {
echo '<tr>';
foreach ($table_headers as $val) {
echo '<td>',$value[$val],'</td>';
}
echo '</tr>';
}
?>

Categories