I'm new to PHP and MySQL. I have an HTML table and form that submits the entered data to MySQL, but doesn't display the MySQL data in my HTML table. Here is an image for reference: https://i.imgur.com/OEDd6Px.png. I want the submitted data to display upon submission if possible but am unable to find a solution. Thanks in advance.
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if(isset($_POST["asin"]))
{
$asin = $_POST["asin"];
$category = $_POST["category"];
$submit = "INSERT INTO `user_input`(`id`, `asin`, `category`, `date`) VALUES (NULL, '$asin', '$category', CURRENT_DATE())";
$sql = mysqli_query($conn, $submit);
if (!$sql) {
die ('SQL Error: ' . mysqli_error($conn));
}
$display = "SELECT * FROM user_input";
$result = mysqli_query($conn, $display);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>testEnv</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<style>
form {
padding-bottom: 10px;
padding-top: 10px;
}
table, thead, tbody, th, td {
padding: 4px;
border-collapse: collapse;
border: 1px solid black;
}
form {
font-size: 13px;
}
th, td {
width: auto;
text-align: center;
font-size: 13px;
}
</style>
</head>
<body>
<div class="container-fluid">
<form id="form" method="post">
<div>
<label id="asinLabel" for="asin">ASIN:</label>
<input id="asinInput" type="text" name="asin"></input>
<label id="categoryLabel" for="category">Category:</label>
<input id="categoryInput" type="text" name="category"></input>
<input id="submit" type="submit" value="Submit"></input>
</div>
</form>
</div>
<div class="container-fluid">
<table class="container-fluid">
<thead>
<tr>
<th>ID</th>
<th>ASIN</th>
<th>Category</th>
<th>Date</th>
<th>6:00 AM</th>
<th>8:00 AM</th>
<th>10:00 AM</th>
<th>12:00 PM</th>
</tr>
</thead>
<tbody id="tableBody">
<?php
while($row = mysqli_fetch_array($result));
{
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['asin'].'</td>
<td>'.$row['category'].'</td>
<td>'. date('m d, Y', strtotime($row['date'])) .'</td>
<td>'.$row['6am'].'</td>
<td>'.$row['8am'].'</td>
<td>'.$row['10am'].'</td>
<td>'.$row['12pm'].'</td>
</tr>';
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script rel="script" type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script rel="script" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.js"></script>
<script rel="script" type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.js"></script>
</body>
</html>
Try to write
$display = "SELECT * FROM user_input";
$result = mysqli_query($conn, $display);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
block out side of the condition.
Related
hi guys i want to create a zip from the hole files names into my database in my code i can just download just one file but i want to get the hole files from my database into a zip
<html>
<title>Files | github</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.4.1/font/bootstrap-icons.css">
<link href="globe.png" rel="shortcut icon">
<?php
date_default_timezone_set("Asia/Calcutta");
//echo date_default_timezone_get();
?>
<?php
$conn=new PDO('mysql:host=localhost; dbname=github', 'root', '') or die(mysqli_error($conn));
if(isset($_POST['submit'])!=""){
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$date = date('Y-m-d H:i:s');
$caption1=$_POST['caption'];
$link=$_POST['link'];
move_uploaded_file($temp,"files/".$name);
$query=$conn->query("INSERT INTO upload (name,date) VALUES ('$name','$date')");
if($query){
header("location:index.php");
}
else{
die(mysqli_error($conn));
}
}
?>
<html>
<body>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen">
<link rel="stylesheet" type="text/css" href="css/DT_bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css"/>
<style>
body{
background-color:#24292f;
}
</style>
</head>
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" language="javascript" src="js/DT_bootstrap.js"></script>
<?php include('dbcon.php'); ?>
<style>
.table tr th{
border:#eee 1px solid;
position:relative;
#font-family:"Times New Roman", Times, serif;
font-size:12px;
text-transform:uppercase;
}
table tr td{
border:#eee 1px solid;
color:#000;
position:relative;
#font-family:"Times New Roman", Times, serif;
font-size:12px;
text-transform:uppercase;
}
#wb_Form1
{
background-color: #00BFFF;
border: 0px #000 solid;
}
#photo
{
border: 1px #A9A9A9 solid;
background-color: #00BFFF;
color: #fff;
font-family:Arial;
font-size: 20px;
}
</style>
<div class="alert alert-info">
</div>
<!--<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered">
<tr><td><form enctype="multipart/form-data" action="" id="wb_Form1" name="form" method="post">
<input type="file" name="photo" id="photo" required="required"></td>
<td><input type="submit" class="btn btn-danger" value="SUBMIT" name="submit">
</form> <strong>SUBMIT HERE</strong></tr></td></table>
<div class="col-md-18">-->
<div class="container-fluid" style="margin-top:0px;">
<div class = "row">
<div class="panel panel-default">
<div class="panel-body">
<div class="table-responsive">
<form method="post" action="delete.php" >
<table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
<thead>
<tr>
<th>ID</th>
<th>FILE NAME</th>
<th>Date</th>
<th>Download</th>
<th>code editor</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$user = $_SESSION["username"];
$project= $_GET['project'];
echo $project;
$query=mysqli_query($conn,"SELECT * FROM project S WHERE date=( SELECT MAX(date) FROM project WHERE pointedname = S.pointedname) and (user='$user' and directoryName ='$project')")or die(mysqli_error($conn));
while($row=mysqli_fetch_array($query)){
$id=$row['user'];
$name=$row['pointedname'];
$date=$row['date'];
$filpath=$row["path"];
?>
<tr>
<td><?php echo $row['user'] ?></td>
<td><?php echo $row['pointedname']; ?></td>
<td><?php echo $row['date'] ?></td>
<td>
<span class="glyphicon glyphicon-paperclip" style="font-size:20px; color:blue"></span>
</td>
<td>
<?php
echo "<a href='../repositories/codeEditorGit/index.php?project=".$row["path"]."'><i class='bi bi-code-slash'></i> ".$row["pointedname"]."</a>";
echo "<a href='zip.php?project=".$row["path"]."'><i class='bi bi-code-slash'></i> ".$row["pointedname"]."</a>"; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
i tried this code but it didnt work for me
<?php
$conn=new PDO('mysql:host=localhost; dbname=github', 'root', '') or die(mysqli_error($conn));
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
//echo $file_path.$files,$files."<br />";
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
session_start();
$user=$_SESSION["username"];
$project = $_GET["project"];
$cqurfetch=mysql_query("SELECT * FROM project where user='$user' and accept='1'");
while($row = mysql_fetch_array($cqurfetch))
{
$file_names[] = $row['user_album_images'];
}
$archive_file_name=time().'.gallery.zip';
$file_path="/uploads/";
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
echo '^^^^^^Zip ended^^^^^^';
?>
also i want to check if the user exist by email but i got an error that when i execute the code he escape the if statment and he go throw executing the insert even the email exist
if(mysqli_num_rows($check_email) > 0){
echo('Email Already exists');
}
code :
$textarea = $_POST["textarea"];
$email = $_POST["email"];
$name = $_POST["name"];
$pswd = $_POST["password"];
$check_email = mysqli_query($conn, "SELECT * FROM sign where email = '$email' ");
if(mysqli_num_rows($check_email) > 0){
echo('Email Already exists');
}
else{
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$result = $sql = "INSERT INTO sign
VALUES ('$email', '$name', '$password','$textarea');
INSERT INTO connected
VALUES ('$email', '')
";
$conn->exec($result);
header("Location: ../image-upload-php-and-mysql-main/index.php");
}
echo('Record Entered Successfully');
}
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.
basic question is im trying to make a table similar to this
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_caption_test
for my webpage: http://lamp.cse.fau.edu/~mcuervo5/p6/
in my php file for "name" & "votes"
by making the table for my boy table and girl table all border up and separated neatly
but it my first time using margin and padding to make the space neat & been having a lot of trial and error here & cant seem to solve the problem
tried to replace this:
$myTable = "<table>
with this:
$myTable = "<table style="margin: 5px; border: 1px; border-color: black;">
but got error code :(.
You can right click "inspect" the page or take it from here from my php code so far. Thanks guys if you can help me with this
<?php
require_once './php/db_connect.php'; // Will call this php file
?>
<!DOCTYPE html>
<html lang="en">
<head>
Just added this to make the border line
<style>
table, th, td
{
border: 2px solid black;
}</style>
End new edit
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Maundy | Comming Soon Page</title>
<meta name="description" content="Free Bootstrap Theme by BootstrapMade.com">
<meta name="keywords" content="free website templates, free bootstrap themes, free template, free bootstrap, free website template">
<link href='https://fonts.googleapis.com/css?family=Lobster|Open+Sans:400,400italic,300italic,300|Raleway:300,400,600' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/animate.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.countdown.min.js"></script>
<script src="js/wow.js"></script>
<script src="js/custom.js"></script>
<script src="contactform/contactform.js"></script>
<body>
<div class="col-lg-12">
<form method="post" action="index.php">
<label>Enter Name for Baby:</label>
<input type="text" name="baby_name" />
<label>Select Gender:</label>
<select name="gender">
<option value="B">Boy</option>
<option value="G">Girl</option>
</select>
<input type="submit" value="Sumbit!" />
</form>
</div>
<?php
if (isset($_POST['gender'])&& isset($_POST['baby_name']) )
{
$gender = $_POST['gender'];
$val_input = $_POST['baby_name'];
// Add two rows to the table
$selectStmt = 'SELECT `Votes`
FROM `Free_Names`
WHERE `Gender` = \''.$gender.'\' AND `Name` = \''.$val_input.'\';';
$result = $db->query($selectStmt); //query the "select" statement and update the result
if($result->num_rows > 0) { //if has a results
while($row = $result->fetch_assoc()) { //updates the statments
$UpdateStmt = 'UPDATE `Free_Names`
SET `Votes` = '.($row["Votes"] +1).'
WHERE `Gender` = \''.$gender.'\' AND `Name` = \''.$val_input.'\';';
$db->query($UpdateStmt); //this will run, but wont work
}
}
else
{
$insertStmt = 'INSERT INTO `Free_Names` (`Name`, `Gender`, `Votes` )' . PHP_EOL
. ' VALUES (\''.$val_input.'\',\''.$gender.'\',1);';
$db->query($insertStmt); //
}
}
?>
<?php
// Get the rows from the table, and \\ for single quote to show up
$selectStmt = 'SELECT `Name` , `Votes`
FROM `Free_Names`
WHERE `Gender` = \'B\'
ORDER BY `Votes` DESC
LIMIT 5';
?>
<div class="col-md-6">
<div class="mx-auto" style="width: 200px;">
It around here is the table for the names
BOY NAMES
</div>
<?php
$result = $db->query($selectStmt); //query the "select" statement and store the result
if($result->num_rows > 0) {
$myTable = "<table>
<thead>
<tr><th>Name</th>
<th>votes</th></tr>
</thead><tbody>";
while($row = $result->fetch_assoc()) {
$myTable.= "<tr>
<td>".$row["Name"]."</td>
<td>".$row["Votes"]."</td>
</tr>";
}
$myTable.= "</tbody></table>";
echo $myTable;
} else {
echo ' <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
</div>
<?php
// Get the rows from the table, and \\ for single quote to show up
$selectStmt = 'SELECT `Name` , `Votes`
FROM `Free_Names`
WHERE `Gender` = \'G\'
ORDER BY `Votes` DESC
LIMIT 5';
?>
<div class="col-md-6">
<div class="mx-auto" style="width: 200px;">
GIRL NAMES
</div>
<?php
$result = $db->query($selectStmt); //query the "select" statement and store the result
if($result->num_rows > 0) {
$myTable = " <table>
<thead>
<tr >
<th>Name</th>
<th>votes</th>
</tr>
</thead><tbody>";
while($row = $result->fetch_assoc()) {
$myTable.= "<tr>
<td>".$row["Name"]."</td>
<td>".$row["Votes"]."</td>
</tr>";
}
$myTable.= "</tbody></table>";
echo $myTable;
} else {
echo ' <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
</div>
</body>
</html>
I'll just use the W3Schools example. When it comes to cellpadding, add the style to <td> . When it comes to cell margins, add the style to <table> using border-spacing :
td{
padding: 12px 6px;
}
table{
border-spacing: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
td{
padding: 12px 6px;
}
table{
border-spacing: 10px;
}
</style>
</head>
<body>
<table>
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
</body>
</html>
Hi there i am trying to create a screen in php.
in which i select scenario and the screen displayed accordingly.
but i am stuck in a simple problem that my simple select query is not working
which is
$deptQuery = "Select * from mcb_department";
echo mysql_real_escape_string($deptQuery);
mysql_query($deptQuery) or die("adfasdf");
in same code if change the table name it just work fine, also this table is created in the db as well with the same name i have shared.
here is my complete code.
<?php
include "include/conn.php";
include "include/session.php";
if(isset($_SESSION['logged_user']) && $_SESSION['logged_user'] != '99999'){
header('location: login.php');
}
$query = mysql_query("select curdate() as todayDate");
$show = mysql_fetch_array($query);
if(isset($show)){
$todayDate= $show['todayDate'];
}
$group[] = array();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta Content="no-cache, no-store, must-revalidate" http-Equiv="Cache-Control" />
<meta Content="no-cache" http-Equiv="Pragma" />
<meta Content="0" http-Equiv="Expires" />
<link href="styles/style.css" type="text/css" rel="stylesheet" />
<link href="styles/popupstyle.css" type="text/css" rel="stylesheet" />
<link href="styles/ts.css" type="text/css" rel="stylesheet" />
<link href="styles/calendar.css" type="text/css" rel="stylesheet" />
<style>
table {
font-family: arial;
border-collapse: collapse;
width: 100%;
font-size: 11px;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 3px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body >
</body>
<select id='select_opt'>
<option> Select Assigment </option>
<option value="1"> assign quiz to all Employees </option>
<option value="2"> assign quiz to Sapcific Group </option>
<option value="3"> assign quiz to Sapcific Department </option>
<option value="4"> assign quiz to Sapcific Employee </option>
</select>
<!-- all Users -->
<div id='allUsers' style='margin-left:20px; margin-top: 20px; width: 50%; height:100px; display: none;' >
<form action="" mathod="post">
<select>
<option value=""> select Quiz</option>
</select>
<input type="submit" >
</form>
</div>
<!-- group -->
<div id='group' style='margin-left:20px; margin-top: 20px; width: 50%; height:100px; display: none;' >
<form action='group_assigment.php' mathod="post">
<table>
<tr>
<th>All <input type="checkbox"> </th>
<th>Group Name</th>
<th>Group Code</th>
</tr>
<?php
$group[] = array();
$groupQuery = "Select * from mcb_groups";
$query = mysql_query($groupQuery);
?>
<tr>
<?php if($query){
while($group = mysql_fetch_array($query)){
?>
<td><input type="checkbox" value="<?php echo $group['group_name']; ?>"></td>
<td><?php echo $group['group_name']; ?></td>
<td><?php echo $group['group_code']; ?></td>
</tr>
<?php }
} else{ echo "";} ?>
</table>
</form>
</div>
<!--
####################################
department
####################################
-->
<div id='Department' style='margin-left:20px; margin-top: 20px; width: 50%; height:100px; display: none;' >
<form action='group_assigment.php' mathod="post">
<table>
<tr>
<th>all <input type="checkbox"> </th>
<th>name</th>
<th>code</th>
<th>group</th>
</tr>
<tr>
<?php
$deptQuery = "Select * from mcb_department";
echo mysql_real_escape_string($deptQuery);
mysql_query($deptQuery);
?>
<td><input type="checkbox"></td>
<td>code</td>
<td>name</td>
<td>group</td>
</tr>
</table>
<input type="submit" >
</form>
</div>
<!--
####################################
Employee
####################################
-->
<div id='employee' style='margin-left:20px; margin-top: 20px; width: 50%; height:100px; display: none;' >
<form action="" mathod="post">
<label>employee id : </label><input type="text" >
<input type="submit" >
</form>
</div>
<script language="javascript" type="text/javascript">
var elem = document.getElementById("select_opt");
elem.onchange = function(){
console.log("yes i am running");
if( document.getElementById("select_opt").value == "1" ){
document.getElementById("allUsers").style.display = "Block";
document.getElementById("group").style.display = "none";
document.getElementById("Department").style.display = "none";
document.getElementById("employee").style.display = "none";
}
else if( document.getElementById("select_opt").value == "2" ){
document.getElementById("group").style.display = "Block";
document.getElementById("allUsers").style.display = "none";
document.getElementById("Department").style.display = "none";
document.getElementById("employee").style.display = "none";
}
else if( document.getElementById("select_opt").value == "3" ){
document.getElementById("Department").style.display = "block";
document.getElementById("group").style.display = "none";
document.getElementById("allUsers").style.display = "none";
document.getElementById("employee").style.display = "none";
}
else if( document.getElementById("select_opt").value == "4" ){
document.getElementById("employee").style.display = "block";
document.getElementById("Department").style.display = "none";
document.getElementById("group").style.display = "none";
document.getElementById("allUsers").style.display = "none";
}
else{
}
};
</script>
</
html>
regard,
Shafee jan
in same code if change the table name it just work fine
Then I would make sure you're connected to the right database. It's surprisingly common for developers to have multiple versions of their database, either on different MySQL instances or else on the same instance under a different schema name. Then they get mixed up, connecting to one database with MySQL Workbench while their app is connecting to a different database.
I would advise that you temporarily add a query to your page to run SHOW TABLES and then dump the result of that query to the log, to confirm that the mcb_department table is present in the database that your PHP script is connected to.
$deptQuery = "Select * from mcb_department";
echo mysql_real_escape_string($deptQuery);
mysql_query($deptQuery);
Where's your error checking? You need to check the return value of mysql_query() every time you run a query, so if there's a problem, you output the error message to your log. Only this way can you start to solve some of these problems.
$result = mysql_query($deptQuery);
if (!$result) {
trigger_error("Error in file " . __FILE__ . " near line " . __LINE__
. " for query $deptQuery: " . mysql_error());
die("Database error");
}
PS: The advice of some commenters that mysql_* functions are deprecated is true, but probably irrelevant to your question. Folks who focus on the API, when you have said the API is working, are just being pedantic.
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.