Updating a PHP variable through HTML - php

I want to be able to change the PHP variables $dispname and $banstat in the code below with a HTML form.
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
$banstat = '1';
$dispname = "Brendan";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE display_name=$dispname";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
<li id="cid_4" class="form-input-wide" data-type="control_head">
<div class="form-header-group">
<div class="header-text httal htvam">
<h2 id="header_4" class="form-header">
Ban Tool
</h2>
</div>
</div>
</li>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> method="post">
Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>
<input type="submit">
</form>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
</ul>
</div>
<input type="hidden" id="simple_spc" name="simple_spc" value="51970718174158" />
<script type="text/javascript">
document.getElementById("si" + "mple" + "_spc").value = "51970718174158-51970718174158";

There were some multiple issues in your code.
Try
<?php
if (isset($_GET['p']) && $_GET['p'] == "login")
{
/* Database Connection Info */
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
/* getting form values */
$banstat = mysqli_real_escape_string($conn, $_GET['banstat']);//'1';
$dispname = mysqli_real_escape_string($conn, $_GET['dispname']);
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url='$banstat' WHERE display_name='$dispname'";
if ($conn->query($sql) === TRUE)
{
echo "Record updated successfully";
}
else
{
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="get">
<input type="hidden" name="formID" value="51970718174158" />
<div class="form-all">
<ul class="form-section page-section">
<li id="cid_4" class="form-input-wide" data-type="control_head">
<div class="form-header-group">
<div class="header-text httal htvam">
<h2 id="header_4" class="form-header">
Ban Tool
</h2>
</div>
</div>
</li>
Name: <input type="text" name="dispname"><br>
E-mail: <input type="text" name="banstat"><br>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
<input type="submit">
</form>
</ul>
</div>
Errors in your code
You open two forms.
You are using post method in form but using get method in php code.
Your variables were not updating.

You need get $_POST and set variable
$dispname = $_POST['name'];
Errors in your code:
You open two forms and just close one
If you want use post instead get check $_POST
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST) {
//update variables and update bd
}
}
?>

Okay, so I took both of the other answers provided from #javi and #Hassan and used them to re-write it, I took the majority of the code from Hassan's and then changed a few things using Javi's and put this together. I figured maybe someone else could use this. Thanks again guys.
<?php
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST) {
$servername = "localhost";
$username = "foo";
$password = "bar";
$dbname = "wordpress";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$banstat = $_POST['banstat'];
$dispname = $_POST['dispname'];
$sql = "UPDATE wp_oxygenpurchaseusers SET user_url=$banstat WHERE user_login='$dispname'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
}
?>
</script>
<link href="http://jotform.us/static/formCss.css?3.3.8019" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://jotform.us/css/styles/nova.css?3.3.8019" />
<link type="text/css" media="print" rel="stylesheet" href="http://jotform.us/css/printForm.css?3.3.8019" />
<style type="text/css">
.form-label-left{
width:150px !important;
}
.form-line{
padding-top:12px;
padding-bottom:12px;
}
.form-label-right{
width:150px !important;
}
.form-all{
width:650px;
color:#555 !important;
font-family:"Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, sans-serif;
font-size:14px;
}
</style>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
Display Name: <input type="text" name="dispname"><br>
Ban Status: <input type="text" name="banstat"><br>
<input type="submit">
</form>
<li style="display:none">
Should be Empty:
<input type="text" name="website" value="" />
</li>
</ul>

Related

create zip from mysql to all files using php

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');
}

Connecting tables PHP

I'm a number of days busy with a PHP script.
This is what I want:
- People can type their ZIP code. If their ZIP code is in table1 than do .. if their ZIP code is in table2 than do... If their ZIP code is in none of the tables than ...
- I can insert ZIP code with other data example name.
But I don't know how to build this. I have searched on the internet I have tried to get what I have. But the function to search in other tables en than do that. It don't work for me
This is what I have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.

Connect php with phpmyadmin

I'm building a simple user login page when I put in my username and password nothing happens, it seems like its not connected to the database
here's my html code with some CSS and my php code that is below this code:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<title>Intacs Login</title>
<!-- inner css -->
<style>
#login_logo {
margin: 15px 10px 10px 0;
display: block;
margin-left: auto;
margin-right: auto;
}
.container{
background-color: white;
border-radius: 30px;
border-color: lightgray;
border-style:outset;
width: 320px;
margin-top: 30px;
margin:auto;
padding-top: 50px;
padding-bottom: 50px;
}
#login_table{
margin-top: auto;
margin-left: auto;
margin-right: auto;
}
#login_remember{
text-align: center;
}
</style>
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="/js/jquery-3.3.1.slim.min.js"></script>
<script src="/js/popper.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<!-- HTML code -->
<div class="container">
<div id="login_form">
<div id="login_block">
<img id="login_logo" src="img/Intacs Master Logo.jpg"
width="187" height="63"> <!--div container-->
</div>
</div>
<table id="login_table" cellspacing="1" cellpadding="3" border="0">
<tbody>
<tr>
<form method="post" action="connect.php"> <!--form to connect to php file called connect.php -->
<th>Username: </th>
<td>
<input type="text" name="username" id="usernamefield" class="loginfield" validate="/^[a-z0-9_]{2,}$/i" valmsg="Please enter a valid username." value="" size="14" maxlength="32">
</td>
</tr>
<tr>
<th>Password: </th>
<td nowrap="nowrap">
<input type="password" name="password"
id="passwordfield" class="loginfield" validate="/^[^\s]{4,}$/"
valmsg="Please enter a valid password." value="" size="14"
maxlength="32">
</td>
</form>
</tr>
</tbody>
</table>
<div id="login_remember"><input type="checkbox" name="remember" value="1"> Remember me</div>
<br />
<center><input type="submit" value="Submit"></center>
</body>
</html>
<?php
function Db() {
$host = "localhost:8888";
$username = "root";
$password = "";
$db = "intacslogin";
$conn = new mysqli($host, $username, $password, $db);
if(!$conn){
die("Could not connect");
}
}
if(isset($_POST['login'])){
$uid = trim ($_POST['username']);
$pwd = trim($_POST['password']);
if($uid ==""){
$err[] = "Username is missing";
} else if($pwd == ""){
$err[] = "Password is missing";
} else{
$db = Db();
$uid = $db->real_escape_string($uid);
$pwd = $db->real_escape_string($pwd);
$sql = "SELECT * FROM users
WHERE username = '$uid'
and password = '$pwd'";
$result = $db->query($sql);
}
}
?>
Please Help thanks and happy coding thanks for your help.
Its a small personal project
<?php
//THIS FUNCTION SHOULD RETURN A CONNECTION TO BE USED IN A QUERY
function Db() {
$host = "localhost:8888";
$username = "root";
$password = "";
$db = "intacslogin";
//MAKE SURE U CONFIRM A SUCCESSFULL CONNECTION LIKE SO
if ($conn = new mysqli($host, $username, $password, $db)) {
return $conn;
}else {
return false;
}
//if(!$conn){
// die("Could not connect");
//}
}
if(isset($_POST['login'])){
$uid = trim ($_POST['username']);
$pwd = trim($_POST['password']);
if($uid ==""){
$err[] = "Username is missing";
} else if($pwd == ""){
$err[] = "Password is missing";
} else{
if (!$conn) {
//you can die() becouse it returned false
}else {
//proced when we a valid connection
//dont use $db use $conn
$uid = $conn->real_escape_string($uid);
$pwd = $conn->real_escape_string($pwd);
$sql = "SELECT * FROM users
WHERE username = '$uid'
and password = '$pwd'";
$result = $conn->query($sql);
}
}
}
?>

How to do editing and deleting function in auto generated table in php ,mysql

Actually am new in php, i created one fetching page in php. Where am fetching data from the database and will display on the auto generated table. I added two button also. One for Delete the specific row from database and another one for edit the details from the database. In database email_id column is unique. So both the Delete Edit operation will do by email_id. Can Any one tell me , how i write Ajax function for editing and deleting. Code is given below
<html>
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><!--Dquip--></title>
<link href="http://fonts.googleapis.com/css?family=Abel|Arvo" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.dropotron-1.0.js"></script>
<script type="text/javascript" src="jquery.slidertron-1.0.js"></script>
<style>
label
{
display:inline-block;
width:150px;
margin-right:10px;
text-align:;
}
table,tr,th,td
{
border: 2px solid dodgerblue;
border-collapse: separate;
}
table
{
width:75%;
margin-top: 8%;
}
th,td
{
height: 50px;
}
td
{
text-align:center;
vertical-align: middle;
}
.button
{
width:75px;
height: 25px;
background-color:dodgerblue;
color:white;
border:1px solid transparent;
}
</style>
<center>
<script>
function deleteABC()
{
}
function editABC()
{
}
</script>
</head>
<body>
<h4 align="right">Logout</h4>
<body>
<div id="wrapper">
<div id="header-wrapper">
<div id="header">
<div id="logo">
<h1>..</h1>
</div>
</div>
<div id="menu-wrapper">
<ul id="menu">
<li class="current_page_item"><span>Homepage</span></li>
<li><span>Blog</span>
<ul>
<li class="first"> About US </li>
<li> Function Area </li>
<li class="last"> Contact US </li>
</ul>
</li>
<li><span>Photos</span></li>
<li><span>About</span></li>
<li><span>Datas</span>
<ul>
<li class="first"> Add Details </li>
<li> Map view </li>
<li class="last"> view Details </li>
</ul>
</li>
<li><span>Contact</span></li>
</ul>
<script type="text/javascript">
$('#menu').dropotron();
</script>
</div></br>
<h1><font color="white">Enter the dates to retrieve the data</font></h1></br></br>
<form method="POST" action="fetchinghome.php">
<label>Role ID:</label><input type="text" name="role" placeholder="Enter the starting date">
<input type="submit" id="submit" name="submit" value="Go" class="button">
</form>
<?php
//include "loginpage.php";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "calender";
$role=$_REQUEST['role'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
if ($role == 'admin')
{
$sql="SELECT * FROM registration";
}
elseif($role=="M%")
{
$sql="SELECT * FROM registration where reporting_manager='$role' or role='$role'";
}
else
{
$sql="SELECT * FROM registration WHERE role='$role'";
}
// $sql="SELECT * FROM registration where reporting_manager='$role'";
$retval = mysqli_query( $conn,$sql );
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
$arr;
$i=0;
while($row = mysqli_fetch_assoc($retval))
{
$arr[$i]=$row;
$i++;
/*
echo "<tr><td>";
echo $row[0]."</td><td>";
echo $row[1]."</td><td>";
echo $row[2]."</td><td>";
echo $row[3]."</td><td>";
echo $row[4]."</td>";
echo "</tr>";
*/
}
$str='';
for($i=0;$i<count($arr);$i++)
{
$brn='<input type="button" value="Delete" onClick="deleteABC('.$arr[$i]['email_id'].')">';
$brn1='<input type="button" value="Edit" onClick="editABC('.$arr[$i]['email_id'].')">';
$str=$str . '<tr><td>'. $arr[$i]['name'].'</td><td>'.$arr[$i]['email_id'].'</td><td>'.$arr[$i]['mobile_no'].'</td><td>'.$arr[$i]['address'].'</td><td>'.$arr[$i]['role'].'</td><td>'.$brn.'</td><td>'.$brn1.'</td></tr>';
}
echo "<table id='example' class='display' cellspacing='0' width='100%'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>Address</th>
<th>Role</th>
<th>Delete</th>
<th>Edit</th>
</tr>".$str."</table>";
}
echo "Fetched data successfully\n";
mysqli_close($conn);
}
?>
</body>
</html>
Please help me to write the Ajax function for those.
Thanks in advance
function deleteABC(email_id)
{
if(confirm("Are you sure you want to delete..!"))
{
$.ajax
({
url: "your url here",
type: 'POST',
data: {email_id: email_id},
success: function (data)
{
//your success code if deleted..
}
});
}
}
function editABC(email_id)
{
$.ajax
({
url: "your url here",
type: 'POST',
data: {email_id: email_id},
success: function (data)
{
//your success code if edited
}
});
}

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.

Categories