Inserting MySQL data into exsisting row - php

I need to insert a timestamp into table visitors column out after the user clicks on the sign-out button. The sign-out button already deletes rows in the table liveroster but its not updating rows in table visitors at column out with the timestamp.
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
<?php
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
mysql_query("UPDATE visitors SET out=test WHERE ID=" . $ID);
mysql_query("DELETE FROM liveroster WHERE ID = " . $ID);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$data = mysql_query("SELECT * FROM liveroster") or die(mysql_error());
while($info = mysql_fetch_array($data)){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $info['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $info['timestamp']; ?>
</td>
<td>
<?php echo $info['name']; ?>
</td>
<td>
<?php echo $info['teacher']; ?>
</td>
<td>
<?php echo $info['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>

Use PDO:
<?php
$dsn = "mysql:host=localhost;port=$port;dbname=$dbname";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
/*
Connection Stuff
*/
if (isset($_POST['ID'])) {
$ID = $_POST['ID'];
if (isset($_POST['delete_id'])) {
/* Update */
$count = $dbh->exec("UPDATE `visitors` SET `out` = 'test' WHERE `ID` = '" . $ID ."'");
/* Delete */
$count = $dbh->exec("DELETE FROM `liveroster` WHERE `ID` = '" . $ID ."'");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#delete-post {
width: 100%;
margin: auto;
background-color: #999;
}
</style>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<div>
<table border cellpadding="3">
<tr>
<td></td>
<td><strong>Time</strong></td>
<td><strong>Name</strong></td>
<td><strong>Teacher</strong></td>
<td><strong>Reason</strong></td>
</tr>
<?php
$sql = "SELECT * FROM liveroster";
$sth = $dbh->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
foreach($rows as $value){ ?>
<tr>
<th>
<form method="post" action="">
<input type="hidden" name="ID" value="<?php echo $value['ID']; ?>" />
<input type="submit" name="delete_id" value="Sign-Out" />
</form>
</th>
<td>
<?php echo $value['timestamp']; ?>
</td>
<td>
<?php echo $value['name']; ?>
</td>
<td>
<?php echo $value['teacher']; ?>
</td>
<td>
<?php echo $value['reason']; ?>
</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>

You could create a column and update it using the "NOW()" function every time you click submit.
UPDATE visitors
SET out = test, getdate = NOW()
WHERE ID = $ID

Related

update.php file in not working properly. and error is not showing

i create a curd apllication with the help of php and mysql there is work all operation insert, read and delete but in update option it not work. when i click on update button it is jump directly on display.php page.
this is my display.php code
<?php
include 'conc.php';
// $username = $_POST["username"];
// $password = $_POST["password"];
$q = "select * from cued_data";
$query = mysqli_query($con , $q);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>display data</title>
<link rel="stylesheet" href="curd.css">
</head>
<body>
<div class="container">
<h1>display Table Data</h1>
<table class="table">
<tr>
<th>Id</th>
<th>Username</th>
<th>Password</th>
<th>Delete</th>
<th>Update</th>
</tr>
<?php
include 'conc.php';
$q = "select * from cued_data";
$query = mysqli_query($con , $q);
while($res = mysqli_fetch_array($query)){
?>
<tr>
<td> <?php echo $res['id']; ?> </td>
<td> <?php echo $res['name']; ?> </td>
<td> <?php echo $res['password']; ?> </td>
<td><button class="dlt"> <a href="delete.php?id= <?php echo $res['id']; ?>" > Delete: </a> </button> </td>
<td><button class="dlt"> <a href="update.html?id= <?php echo $res['id']; ?>" > Update: </a> </button> </td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
this is my update.php file
<?php
include "conc.php";
include "display.php";
$id = $_GET["id"]; # i think error in this section
$username = $_POST["name"];
$password = $_POST["password"];
// $q = "delete from `cued_data` where id = $id" ;
$msql= " update cued_data set id ='$id', name = '$username', password = '$password' where id = '$id' ";
$query = mysqli_query($con, $msql);
header("location:display.php");
?>
this is my delete.php file
<?php
include "conc.php";
$id = $_GET["id"];
$q = "delete from `cued_data` where id = $id" ;
$query = mysqli_query($con, $q);
header("location:display.php");
?>

Style is not being applied except for the first row from sql query in html code

I'm retrieving all the rows from sql database through a query, and i'm using a loop to get all the rows
I have a style.css file which i'm specifying a style for the , however, only the first row is receiving the style and the style is not being applied to the rest.
<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
<title>IIACSS search center</title>
</head>
<body>
<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
</table>
<?php
}
}
?>
body {
background-color: white;
}
h1 {
color: black;
}
td {
color: black;
font-family: sans-serif;
}
th {
color: blue;
font-family: sans-serif;
}
It is not a CSS problem, it is because you are closing your </table> in the loop, so it is ending your table after the first tr.
Just remove it from the loop:
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
<?php
} ?>
</table>
<?php }
?>

Read out data and edit

i would like to read out all data records from a table and give individual data records a new value in "status" according to their "id".
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th><button type="submit" name="change">CHANGE STATUS</button></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<?php
if (isset($_POST['change'])) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>
My problem is, when I click the button, all records are changed and not just one. I hope someone can help to solve the problem.
.........................................................................................................................................................................................................
Try using {} to embed array elements into a string and enclose 'id' value
$update = "UPDATE data Set status = '0' WHERE id = {$row['id']}"
Otherwise, your query will interpret $row and [id] as separated items:
UPDATE data Set status = '0' WHERE id = [id]
and as a result, all your rows updated
Also consider to apply the status update button to each single row, there is no way to update only one row with a general "update status" button without sending a parameter of which row id will update. For example:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<button type="submit" name="change" value="<?php echo $row['id']; ?>">CHANGE STATUS</button>
<?php
if (isset($_POST['change']) && $_POST["change"]==$row['id']) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>

index of fetch array

i want to get the index of td that the user clicked , i have an html table fill from database using php ...
this is my index.php :
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<tr>
<?php
session_start();
$connect = mysqli_connect("localhost","root","","test");
if (!$connect) {
die(mysql_error());
}
$results = mysqli_query($connect,"SELECT * FROM family where parent_id = 0");
while($row = mysqli_fetch_assoc($results)) {
?>
<td onclick="window.location='index2.php'"
<?php $id = $row['id'];
$_SESSION['varname'] = $id;?>>
<?php echo $row['name']?> <br/>
<?php echo $row['description']?> <br/>
<?php echo $row['parent_id']?> <br/>
</td>
<?php
}
?>
</tr>
</tbody>
</table>
</body>
</html>
this is my index2.php :
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<tr>
<?php
session_start();
$gg = $_SESSION['varname'];
echo $gg;
$connect = mysqli_connect("localhost","root", "","test");
if (!$connect) {
die(mysql_error());
}
$results = mysqli_query($connect,"SELECT * FROM family where parent_id = '$gg' ");
while($row = mysqli_fetch_array($results)) {
?>
<td>
<?php echo $row['id']?> <br/>
<?php echo $row['name']?> <br/>
<?php echo $row['description']?> <br/>
<?php echo $row['parent_id']?> <br/>
</td>
<?php
}
?>
</tr>
</tbody>
</table>
</body>
</html>
now i want to take the "id" of the td that the user click on,, but this code always give me the last id in my database ...
what can i do ?
Replace in Index.php:
<td onclick="window.location='index2.php'"
With:
<td onclick="window.location='index2.php?parent_id=<?php echo $row['id']; ?>'"
And in
Index2.php:
$gg = $_SESSION['varname'];
With:
$gg = (int)$_GET['parent_id'];
It's better to use $_GET variable for this than $_session (urls are search engine friendly)

I can't update MySQL PHP

I don't get any errors but I can't update MySQL PHP. I want to change dropdown status from "sedang diproses" dropdown into "Berjaya or Tidak Berjaya". Also I can't change in the database "sedang diproses" into "Berjaya or Tidak Berjaya"
This is my Code Html
<?php
session_start();
include ('include/myFunction.php');
require('include/connect.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<link href="css/myStyle.css" rel="stylesheet" type="text/css" />
<link href="css/myStyle1.css" rel="stylesheet" type="text/css" />
<!-- Button to open the modal login form -->
<link href="css/myStyle3.css" rel="stylesheet" type="text/css" />
<body>
<div align="right">
<a href="logout.php" ><img src="img/logout.png" width="63" height="46" /></a>
</div>
<div class="header" align="left"><img src="img/gmbar.PNG" width="223" height="126" /><!-- end .header --><!-- end .header --></div>
<div class="content" style="font:Verdana, Geneva, sans-serif">
<div class="content" style="font:Verdana, Geneva, sans-serif">
<div class="topnav" id="myTopnav" style="font-family:Verdana, Geneva, sans-serif">Kembali
</div>
<p> </p>
<?php if($_SESSION['namaadmin'] ==null)
{
header('location:index.php');
}
else
{
?>
<form action="prosesstatus.php" method="post">
<table width="1002" height="87" border="0" align="center" bgcolor="#CCCCFF">
<tr bgcolor="#00CCFF">
<td width="30"><strong>Bil</strong></td>
<td width="298"><strong>Nama</strong></td>
<td width="176"><strong>Nama Kursus</strong></td>
<td width="150"><strong>Tarikh Daftar</strong></td>
<td width="150"><strong>Status</strong></td>
<td width="168"><strong>Kemaskini Status</strong></td>
<td width="131"><strong>Pilihan</strong></td>
</tr>
<?php
$namakursus = '';
if( isset( $_GET['namakursus'])) {
$namakursus = $_GET['namakursus'];
}
$sql1 = "Select * from pemohonan INNER JOIN kursus ON pemohonan.idkursus = kursus.idkursus
INNER JOIN pemohon ON pemohonan.idPemohon = pemohon.idPemohon WHERE kursus.namakursus = '$namakursus' ";
$result1=mysqli_query($dbc,$sql1) or die (mysqli_error());
$i=1;
while($row1 = mysqli_fetch_assoc($result1))
{
?>
<tr>
<td><strong><?php echo $i; ?><input name="idkursus" type="hidden" size="50" value="<?php echo $row1['idkursus'];?>"/><input name="idPemohonan" type="hidden" size="50" value="<?php echo $row1['idPemohonan'];?>"/></strong></td>
<td><strong><?php echo $row1['nama']; ?></strong><input name="id" type="hidden" size="50" value="<?php echo $row1['idPemohon'];?>"/></strong></td>
<td><strong><?php echo $row1['namakursus']; ?></strong></td>
<td><strong><?php echo $row1['tarikhpemohon']; ?></strong></td>
<td><strong><?php echo $row1['status']; ?></strong></td>
<td><select name="status" value="<?php echo $row1['status']; ?>">
<option value="sila pilih">-Sila Pilih-</option>
<option value="Berjaya">Berjaya</option>
<option value="Tidak Berjaya">Tidak Berjaya</option>
</select></td>
<td><input name="btnKemaskini" type="submit" value="Kemaskini" /></td>
</tr>
<?php
$i++;
}
mysqli_close($dbc);
}
?>
</table></form>
<p> </p>
<p> </p>
<div class="footer" style="font-family:Arial, Helvetica, sans-serif">
<?php footertext(); ?>
</div>
</body>
</html>
This is Image
This Is My Image DB
This is my Code Php
<?php
require ('include/connect.php');
$idPemohonan = '';
$idPemohon = '';
$idkursus = '';
if (isset($_GET['idPemohon'],$_GET['idkursus'],$_GET['idPemohonan']))
{
$idPemohon = $_GET['idPemohon'];
$idkursus = $_GET['idkursus'];
$idPemohonan = $_GET['idPemohonan'];
}
$tarikhharini=date('y-m-d');
$status=$_POST['status'];
$sql1 = "UPDATE pemohonan SET idPemohonan ='$idPemohonan' , idPemohon ='$idPemohon' , idkursus ='$idkursus' , tarikhpemohon ='$tarikhharini' , status='$status'
where idPemohonan = '$idPemohonan' and idPemohon = '$idPemohon'";
$result1 = mysqli_query($dbc,$sql1) or die (mysqli_error());
$num_row1 = mysqli_affected_rows($dbc);
echo "<script language=\"JavaScript\">\n";
echo "alert('Status Pemohonan Telah Dihantar!');\n";
echo "window.location='senaraipemohon.php'";
echo "</script>";
mysqli_close($dbc);
?>
Your code is not working because you haven't sent in
<form action="prosesstatus.php" method="post">
Parameters:
$idPemohon = $_GET['idPemohon'];
$idkursus = $_GET['idkursus'];
$idPemohonan =
$_GET['idPemohonan'];
If you want use $_GET link should look like:
prosesstatus.php?idPemohon=value&idkursus=value&idPemohonan=value
Or you should add these parameters in
Input type hidden and change in php to $_POST
I agree with others than you should use MVC for example some kind of framework (Laravel, CodeIgniter)
Or separate as much as possible php and html because your code looks unprofessional.
Your MySQL queries are vulnerable to SQL injection and most likely file
include/connect.php
is not outside public folder which is not good practice to do.

Categories