I have an HTML table being displayed from php data and I'm trying to move data from one table to another based on what is clicked. I cannot get it to move from table to another but I"m not getting any code error.
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests
</center>
</div>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
// get results from database
$result = mysql_query("SELECT * FROM temp")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['User_id'] . '</td>';
echo '<td>' . $row['First_name'] . '</td>';
echo '<td>' . $row['Last_name'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>Approve</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
PHP EDIT SCRIPT:
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
I needed to use
$id = $_get['id];
and in the insert statement i had to replace id with User_id
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
Related
I am attempting to print out mySQL database into a html table. I have watched many tutorials on how to do this but am unsure as to how I refer to the html table in my php code. The information gets printed fine and connects to the database but for some reason it isn't output in the table format.
<?php
$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');
if (!$conn) {
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";
//Querying and getting results
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["First Name"] . "</td></tr>" . $row["Last Name"] . "</td></tr>"
. $row["Emails"] . "</td></tr>" . $row["Date Created"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 result";
}
//Fetch resulting rows as an array
$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Freeing result from the memory.
mysqli_free_result($result);
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<div class="Contained">
<div class="row">
<?php foreach ($informed as $inform) { ?>
<div class="col s6 medium-3">
<div class="card z-depth-0">
<div class="card-content center">
<h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>
<div><?php echo htmlspecialchars($inform['Last Name']); ?></div>
</div>
<div class="card-action right-align">
<a class="brand-text" href="#">More Info
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<title> Email and Name List </title>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emails</th>
<th>Date Created</th>
</tr>
</table>
</body>
</html>
Output in browser gyazo:
You must change code for output all table in php like:
<body>
<?php
$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');
if (!$conn){
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date
Created`";
//Querying and getting results
$result = mysqli_query($conn,$sql);
if ($result->num_rows>0){
echo '
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emails</th>
<th>Date Created</th>
</tr>';
while($row = $result->fetch_assoc()){
echo "<tr> ";
echo "<td>" . $row["First Name"] . "</td>";
echo "<td>" . $row["Last Name"] . "</td>";
echo "<td>" . $row["Date Created"] . "</td>";
echo "</tr> ";
}
echo"</table>";
}
else{
echo "0 result";
}
//Fetch resulting rows as an array
$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Freeing result from the memory.
mysqli_free_result($result);
mysqli_close($conn);
?>
</body>
Another question are you sure is $row["First Name"] and not $row["First_Name"]?
Last tip learn how prepare stm for prevent sql inject
Based on your code, you are trying to print the table before the actual table is defined below. You can try something like this:
<?php
$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');
if (!$conn){
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";
//Querying and getting results
$result = mysqli_query($conn,$sql);
$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<div class="Contained">
<div class="row">
<?php foreach($informed as $inform){?>
<div class="col s6 medium-3">
<div class="card z-depth-0">
<div class="card-content center">
<h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>
<div><?php echo htmlspecialchars($inform['Last Name']);?></div>
</div>
<div class="card-action right-align">
<a class="brand-text" href="#">More Info</div>
</div>
</div>
<?php }?>
</div>
</div>
<title> Email and Name List </title>
</head>
<body>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emails</th>
<th>Date Created</th>
</tr>
<?php
if ($result->num_rows>0) {
while($row = $result->fetch_assoc()){
echo "<tr><td>" . $row["First Name"] . "</td><td>" . $row["Last Name"] . "</td><td>" . $row["Emails"] . "</td><td>" . $row["Date Created"] . "</td></tr>";
}
} else {
echo "<tr><td rowspan=\"5\">0 result</td></tr>";
}
?>
</table>
</body>
<?php
// Freeing result from the memory.
mysqli_free_result($result);
mysqli_close($conn);
?>
I have a list of nba teams which are in a table and the data is coming from a php database and whenever i click on the team name it directs me to the teams roster and displays to me the names of the players but i cannot get the name or the logo of each team to show up whenever that team is clicked, i will attach an image of how it should look like to give a better idea of what im talking about.
I have a folder with all the images corresponding to each team.
this is my teams code
<?php
$connection = mysqli_connect('localhost', 'root', '','nba201819');
$result = mysqli_query($connection,"SELECT * FROM teams");
echo "<table border ='1'>
<tr>
<th></th>
<th>Code</th>
<th>Team</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><img src='/logos/".$row['TEAMCODE']."'></td>" ;
echo "<td>" . $row['TEAMCODE'] . "</td>";
echo '<td><a href="roster.php?teamcode=' .$row['TEAMCODE'] .'">' . $row['NAME'] . "<a/></td>";
echo "</tr>";
}
echo "</table>";
?>
this is my roster code
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Roster</title>
</head>
<body>
<header>
<a href='teams.php'>Teams |</a>
<a href='players.php'>Players |</a>
<a href='teamStats.php'>Teams Stats |</a>
<a href='playerStatsJS.php'>Player Stats JS |</a>
<a href='playerFilterStatsJS.php'>Player Filter Stats JS |</a>
<a href='liveGamesJS.php'>Live Games JS |</a>
<a href='liveGamesUpdate.php'>Live Games Update |</a>
</header>
<hr>
<?php $teamcode = $_GET['teamcode']; ?>
<img src ="/logos/<?php echo $teamcode; ?>_logo.svg" width ="100" height="100">
<h1><?php echo $teamcode; ?> Roster </h1>
<hr>
</body>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$connection = mysqli_connect('localhost', 'root', '','nba201819');
$teamcode = $_GET['teamcode'];
$result = mysqli_query($connection,"SELECT * FROM players WHERE TEAMCODE = '$teamcode' ");
echo "<table border ='1'>
<tr>
<th>Name</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NAME'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
So, I am trying to design a php website, and so far it works well in terms of adding an entry to the list table.
The problem is, it isnt able to update the table using edit.php. When the edit link is clicked it shows a message:
"There is no data to be edited."
But if I try to manually put localhost/edit.php**?id=1** it shows the id numbered list and works fine. Please help.
home.php
<html>
<head>
<title>My first PHP Website</title>
</head>
<?php
session_start(); //starts the session
if($_SESSION['user']){ // checks if the user is logged in
}
else{
header("location: index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
?>
<body>
<h2>Home Page</h2>
<hello>!
<!--Display's user name-->
Click here to go logout<br/><br/>
<form action="add.php" method="POST">
Add more to list: <input type="text" name="details" /> <br/>
Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
<input type="submit" value="Add to list"/>
</form>
<h2 align="center">My list</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>Details</th>
<th>Post Time</th>
<th>Edit Time</th>
<th>Edit</th>
<th>Delete</th>
<th>Public Post</th>
</tr>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("first_db") or die("Cannot connect to database");
$query = mysql_query("select * from list");
while($row = mysql_fetch_array($query))
{
print "<tr>";
print '<td align="center">'. $row['id'] . "</td>";
print '<td align="center">'. $row['details'] . "</td>";
print '<td align="center">'. $row['date_posted'] . " - " . $row['time_posted'] . "</td>";
print '<td align="center">'. $row['date_edited'] . " - " . $row['time_edited'] . "</td>";
print '<td align="center">edit</td>';
print '<td align="center">delete</td>';
print '<td align="center">'. $row['public'] . "</td>";
print "</tr>";
}
?>
</table>
</body>
</html>
)
edit.php
<html>
<head>
<title>My first PHP website</title>
</head>
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
$id_exists = false;
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
Click here to logout<br/><br/>
Return to Home page
<h2 align="center">Currently Selected</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>Details</th>
<th>Post Time</th>
<th>Edit Time</th>
<th>Public Post</th>
</tr>
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
$_SESSION['id'] = $id;
$id_exists = true;
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //connect to database
$query = mysql_query("Select * from list Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['details'] . "</td>";
Print '<td align="center">'. $row['date_posted']. " - ". $row['time_posted']."</td>";
Print '<td align="center">'. $row['date_edited']. " - ". $row['time_edited']. "</td>";
Print '<td align="center">'. $row['public']. "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
}
?>
</table>
<br/>
<?php
if($id_exists)
{
Print '
<form action="edit.php" method="POST">
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
$details = mysql_real_escape_string($_POST['details']);
$public = "no";
$id = $_SESSION['id'];
$time = strftime("%X");//time
$date = strftime("%B %d, %Y");//date
foreach($_POST['public'] as $list)
{
if($list != null)
{
$public = "yes";
}
}
mysql_query("UPDATE list SET details='$details', public='$public', date_edited='$date', time_edited='$time' WHERE id='$id'") ;
header("location: home.php");
}
?>
and here's the one with ?id=1 in the url
http;//s15,postimg,org/yoabiq0p7/screenshot_21,png (change the commas with fullstops).
You are printing only the edit.php, you need to print the entire edit link.
print '<td align="center">edit</td>';
Replace this line with:
print '<td align="center">edit</td>';
This will solve the problem.
P.S: Be careful, your code is open for SQL Injection! Make sure to use mysql_real_escape_string() in this place:
$id = mysql_real_escape_string($_GET['id']);
If the id is only number, you can do the following too to avoid SQL Injection:
$id = intval($_GET["id"]);
The SQL Injection thing is very serious and you need to filter what comes from outside. I recommend using prepared statement PDO too.
you arent passing the data in the home.php
you are doing normal link to edit.php and you arent passing the data ?id=1
try edit this:
print '<td align="center">edit</td>';
to this:
print '<td align="center">edit</td>';
I built a simple CRUD using PHP but the home file is displaying all the info I have in the database, even the info added by other users. How can I filter this to show only the logged users info?
Here is the home.php file:
<?php
session_start();
if(isset($_SESSION['user'])){
echo "Logado como ". $_SESSION['user'];
}
else {
echo"<script language='javascript' type='text/javascript'>alert('Voce deve estar logado');window.location.href='index.php';</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Perfil</title>
</head>
<body>
</br>
Sair</br>
<h2 align="center">Lista de contatos</h2>
<table align="center" border="1">
<tr>
<th>Nome</th>
<th>Telefone</th>
<th>Endereco</th>
<th>Editar</th>
<th>Deletar</th>
</tr>
<?php
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("forms") or die("Cannot connect to database"); //connect to database
$query = mysql_query("Select * from contatos"); // SQL Query
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['nome'] . "</td>";
Print '<td align="center">'. $row['telefone'] . "</td>";
Print '<td align="center">'. $row['endereco'] . "</td>";
Print '<td align="center">Editar </td>';
Print '<td align="center">Deletar </td>';
Print "</tr>";
}
?>
</table>
<div align="center">
Adicionar contato</br>
</div>
</body>
</html>
Wherever you are assigning $_SESSION['user'] also assign the user record id to the session.. this way you can add a where clause to your sql and fetch only the desired record..
Quick example:
if(isset($_SESSION['user_id'])) {
$sql = "SELECT * FROM contatos WHERE id = {$_SESSION['user_id']}";
// ....
}
Use id or name to differentiate records
$sql = "SELECT * FROM contatos WHERE nome = ".$_SESSION['user'];
$sql = "SELECT * FROM contatos WHERE id = ".$_SESSION['id'];
You must have an autoincremented primary key which wil be your id.
I need to delete a specific row in php.. so how could I get the ID or other way to delete a specific record
dbconnect.php just a simple database connection
<?php
$con = mysqli_connect("localhost","root","","phpractice");
if(mysqli_connect_errno()){
echo "Database connection failed";
}
?>
index.php the page where the user can see
<html>
<head>
<link rel="stylesheet" type="text/css" href="../styles/index.css">
</head>
<title>Home Page</title>
<?php include'../script/dbconnect.php';?>
<body>
<div id="container">
<div id="table">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th colspan=2>Controls</th>
</tr>
";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>"."<a href='#'>EDIT</a>"."</td>";
echo "<td><a href='../script/delete.php?id=".$row['user_id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
?>
Add User
</div><!--table-->
</div><!--container-->
</body>
</html>
delete.php the delete script
<?php
include '../script/dbconnect.php';
$id = $_GET['user_id'];
$query = "DELETE FROM users WHERE user_id = $id";
mysqli_query($con, $query) or die (mysqli_error($con));
echo "DELETE USER SUCCESSFUL!";
echo "</br>";
echo "<a href='../main/index.php'>RETURN TO DISPLAY</a>";
?>
thanks in advance
in index.php use:
echo "<td><a href='../script/delete.php?user_id=".$row['user_id']."'>DELETE</a></td>";
then use $_GET['user_id']; in delete.php