I have to add delete button into each section of the table.
<?php
$link = mysqli_connect("localhost", "root", "", "demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$contact = mysqli_real_escape_string($link, $_POST['contact']);
$sql = "INSERT INTO persons (name, email, contact) VALUES ('$name', '$email', '$contact')";
if(mysqli_query($link, $sql)){
echo "<h2>Records added successfully.The new updated records are...</h2><br>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$selectData = "select * from persons";
$res = mysqli_query($link, $selectData);
?>
<table border="2">
<tr>
<th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
</tr>
</table>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<?php
}
}
?>
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td><a onClick="delTopicContentRow('<?php echo $row['id'] ?>')"><img src="../admin_assets/images/delete.png"></a></td>
<?php
}
}
The delete javascript function
function delTopicContentRow(topic_content_id)
{
var confirm_delete = confirm("Do you want to delete topic content ?");
if (confirm_delete === true)
{
$(".deleteall"+topic_content_id).addClass("deletealloncomplete");
$.ajax({
type:'POST',
url:'delRow.php',
data:{topic_content_id:topic_content_id},
success:function(data)
{
if(data == 1)
{
$(".deletealloncomplete").fadeOut("slow");
setTimeout(function(){
window.location.reload(1);
}, 2000);
}
else
{
alert("Try again.!");
}
}
});
}
}
You should start with HTML basic http://www.w3schools.com/html/html_basic.asp
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td>Delete item
</td>
<?php
}
}
//This last statement passes the id of the function to delete
function delete(){
if(isset($_GET['delete'])){
$id=$_GET['delete'];
$sql="DELETE FROM `trial` WHERE `id` ='$id'";
$stmt=$this->conn->prepare($sql);
$stmt->execute();
}
}
?>
Related
My Query don't work in PHP but when I test it in my database it works well.
Code:
<?php
//Database connectie
$link = mysqli_connect("*", "*", "*", "*") or die("Error ". mysqli_error($link));
ini_set('memory_limit', '1024M'); // or you could use 1G
//Query //
$query = "SET SQL_BIG_SELECTS = 1;";
$query = "SELECT * FROM datakram, datakram2, datakram3, datakram4 WHERE datakram.ISIN = datakram2.ISIN AND datakram2.ISIN = datakram3.ISIN AND datakram3.ISIN = datakram4.ISIN";
$result = mysqli_query($link, $query);
?>
<table>
<thead>
<tr class="warning">
<th>Naam</th>
<th>ISIN</th>
<th>Jaar</th>
<th>Closely Held Shares</th>
<th>Outstanding Sahres</th>
<th>Market capitalistion</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['company-name'];?></td>
<td><?php echo $row['ISIN'];?></td>
<td>2017</td>
<td><?php echo $row['2017'];?></td>
<td><?php echo $row['2017-shares'];?></td>
<td><?php echo $row['2017-market'];?></td>
</tr>
<?php
}
?>
Knows somebody why? I have tried: $result = mysqli_multi_query($link, $query); But that didn't work.
I have fix it, don't tell me how ;) but it works. Here if somebody need it:
//Database connectie
$link = mysqli_connect("*", "*", "*", "*") or die("Error ". mysqli_error($link));
ini_set('memory_limit', '1024M'); // or you could use 1G
//Query //
$query = "SET SQL_BIG_SELECTS = 1;";
$query .= "SELECT * FROM datakram, datakram2, datakram3, datakram4 WHERE datakram.ISIN = datakram2.ISIN AND datakram2.ISIN = datakram3.ISIN AND datakram3.ISIN = datakram4.ISIN";
if (mysqli_multi_query($link, $query)) {
do {
?>
<table>
<thead>
<tr class="warning">
<th>Naam</th>
<th>ISIN</th>
<th>Jaar</th>
<th>Closely Held Shares</th>
<th>Outstanding Sahres</th>
<th>Market capitalistion</th>
</tr>
</thead>
<tbody>
<?php
if ($result = mysqli_store_result($link)) {
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['company-name'];?></td>
<td><?php echo $row['ISIN'];?></td>
<td>2017</td>
<td><?php echo $row['2017'];?></td>
<td><?php echo $row['2017-shares'];?></td>
<td><?php echo $row['2017-market'];?></td>
</tr>
<?php
}
}
} while (mysqli_next_result($link));
}
?>
I am new to PHP,
I'm trying to delete a particular row in html table which is populated based on MySQL database values.
Every row in html table have return button, when I click on particular row return button it will deleting newly added row in database but.
Here is my Html & PHP code :
<?php
include('connection.php');
echo "
<tr>
<th>ID </th>
<th>Component Type </th>
<th>Component Name</th>
<th>Number of Components</th>
<th>Name</th>
<th>Phone</th>
<th>Company Name</th>
<th>Company Number</th>
<th>Alloted Date</th>
<th>Return</th>
</tr>";
$data=#$_POST['search'];
$sql = "select * from alloted_comp where emp_phone='$data'";
$result = mysql_query($sql) or die(mysql_error());
If (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<form action="" method="post">
<tr>
<td><?php echo $id=$row['id']; ?></td>
<td><?php echo $row['comp_type']; ?></td>
<td><?php echo $row['comp_name']; ?></td>
<td><?php echo $row['num_comp']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_phone']; ?></td>
<td><?php echo $row['emp_compeny']; ?></td>
<td><?php echo $row['emp_compeny_num']; ?></td>
<td><?php echo $row['date']; ?></td>
<td>
<input type="submit" value="Retun" name="submit" class="button">
</td>
</tr>
</form>
<?php
}
}
?>
<?php
if(isset($_POST['submit']))
{
if(isset($id)){
$sql1="INSERT INTO prev_alloted_comp(id,emp_name,emp_phone,emp_compeny,emp_compeny_num,comp_type,comp_name,alloted_date) SELECT id,emp_name,emp_phone,emp_compeny,emp_compeny_num,comp_type,comp_name,date FROM alloted_comp WHERE id='$id'";
$retval1 = mysql_query( $sql1, $con );
if(! $retval1 ) {
die('Could not store data: ' . mysql_error());
}
$sql2="delete from alloted_comp where id='$id' ";
$retval = mysql_query( $sql2, $con );
if(! $retval ) {
die('Could not delete data: ' . mysql_error());
}
mysql_close($con);
}
}
?>
1- Change your MySQL into MySQLi (I agree eith #Magnus Eriksson)
Further, You need to do 2 changes for that.
in HTML
<tr id="<?php echo $row['id];?>">
it will print
<tr id="1">
<tr id="2">
Then add Jquery code
$(document).ready(function(){
$('tr').click(function(){
var id = $(this).attr('id');
// if we need to remove this row from only HTML
$('tr#'+id).css('display', 'none');
// if we need to remove from db
$.ajax({
url: '/del-record.php',
type: 'POST',
data: {id: id}
});
});
});
if we need to delete record from db then we need to create a PHP File to delete record
<?php
//include db connection
$query = "delete from table where id = ".$_POST['id'];
mysqli_query($query); // if using mysqli
echo '1';
?>
After i check it with echo "$_SESSION('success')"; it says my Session is in array. $_SESSION('success') is filled with username, after Login.
<?php
$uname=$_SESSION("success");
$sql = $mysqli->query("SELECT * FROM `msuser` WHERE `Username` = '".$uname."' AND `Status` > 0");
if($sql->num_rows>0)
{
while($row=$sql->fetch_assoc())
{
?>
<table>
<tr>
<td>Username:<?php echo $row["Username"]; ?></td>
</tr>
<tr>
<td>Name:<?php echo $row["Name"]; ?></td>
</tr>
<tr>
<td>Email:<?php echo $row["Email"]; ?></td>
</tr>
<tr>
<td>Address:<?php echo $row["Address"]; ?></td>
</tr>
<tr>
<td>Phone: <?php echo $row["Phone"]; ?></td>
</tr>
<tr>
<td>Role:<?php echo $row["Role"] ?></td>
</tr>
</table>
<?php
}
}
?>
Here is my Login Validation code:
$uname=$_POST["uname"];
$sql = $mysqli->query("SELECT * FROM msuser WHERE Username = '".$uname."' AND Password = md5('".$pass."') AND Status > 0");
if($sql->num_rows>0)
{
setcookie(user,$pass,time() + (86400 * 30), "/");
$_COOKIE[user];
while($row=$sql->fetch_assoc())
{
if($row["Role"] == "Admin")
{
$_SESSION["success"]=$uname;
header("Location: ../../../public/Admin/home.php");
}
else
{
$_SESSION["success"]=$uname;
header("Location: ../../../public/Member/home.php");
}
}
}
PHP Session variable format is like $_SESSION['variable_name']
So in your case it should be $_SESSION['success'] and not $_SESSION("success");
And by the way, have you started the session with session_start();?
I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>
SO i have form which consist of "Event Name" "Event Description" "Event Date" and checkbox "is important". When i check checkbox value "yes" its important, it sends to the sql value = "1" to table "is_important". Everything is all right, but i give the bootstrap style "bg-danger" for that "is_important" = 1 table and it doesnt show up. What's the problem?
You can see in the code:
<?php
if (isset($_POST['important'])) {
$error = array();
$success = array();
$eventTime = time();
$important = $_POST['important'];
$eventName = trim(mysql_real_escape_string($_POST['EventName']));
$eventDesc = htmlentities(trim(mysql_real_escape_string($_POST['EventDesc'])), ENT_QUOTES);
if (!isset($eventName) || empty($eventName)) {
$error['eventName'] = "Prasome ivesti ivykio varda";
} else if (strlen($eventName) > 32 || strlen($eventName) < 3) {
$error['eventName'] = "Ivykio pavadinimas turi buti tarp 3 ir 32 simboliu";
}
if (!isset($eventDesc) || empty($eventDesc)) {
$error['eventDesc'] = "Prasome ivesti ivykio aprasyma";
}
if (empty($error)) {
$sql = "INSERT INTO notes_list (title, description, timestamp,is_important) VALUES ('$eventName', '$eventDesc','$eventTime','$important')";
$result = mysqli_query($con, $sql);
$success[] = "SEKME !";
} else {
}
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Event name</th>
<th>Event description</th>
<th>Event date</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM notes_list ORDER BY id DESC LIMIT 10";
$result2 = mysqli_query($con, $query);
print_r($_POST);
if ($result2) {
while ($note = mysqli_fetch_assoc($result2)) {
?>
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
<td><?php echo $note['title']; ?></td>
<td><?php echo $note['description'] ?></td>
<td><?php echo date('l M jS', $note['timestamp']); ?></td>
</tr>
<?php
}
mysqli_free_result($result2);
}
/* close connection */
mysqli_close($con);
?>
</tbody>
</table>
Full Example in this picture:
https://www.dropbox.com/s/h650h2spy2487dm/chechbox.jpg?dl=0
This:
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
would render this:
<trclass='bg-danger'>
in case is_important is 1. You need a space there, before the class.