I'm trying to update my course_code into my database when the checkbox checked but it fail. I tried to modify from others but seem like I make mistake. can anyone tell me what is the problem?
here is my assigncourse.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codefac = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function Change(id) {
$.ajax({
type: "GET",
url: "updateassigncourse.php",
data: {"id": id},
'success': function (response) {
console.log(response);
//TODO: use server response
}
});
};
</script>
<?php include("includes/header.php"); ?>
<div id="main">
<div class="full_w">
<?php
$querysel = "SELECT * FROM tblstudent ORDER BY student_id " ;
$resultsel = mysql_query($querysel, $connection);
echo "<h2><div class=\"h_title\">Please tick the student to join this
".$course_codefac." course</div></h2>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th scope=\"col\">Matric ID</th>";
echo "<th scope=\"col\">Name</th>";
echo "<th scope=\"col\">Assign</th>";
echo "</tr>";
echo "</thead>";
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['course_code'] == NULL){
$id = $rowsel['id'];
echo "<tr>";
echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."
</td>";
echo "<td class=\"align-center\">".$rowsel['name']."</td>";
echo "<td class=\"align-center\">";
echo "<input type=\"checkbox\" onchange=\"javascript:
Change($id);\">";
echo "</td>";
}
}
echo "</table>";
?>
</div>
</div>
<?php include("includes/footer.php"); ?>
then here is my updateassigncourse.php
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$course = $course_codeapp;
$sql="UPDATE tblstudent set course_code = ". mysql_real_escape_string($course)
." WHERE id = " .mysql_real_escape_string($id);
$result = mysql_query($sql);
?>
If this is your exact code, then there is a missing ';'
$course = $course_codeapp
it should have been
$course = $course_codeapp;
Related
I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post
I am going to make a pagination for my members page, I have a created 8 pagination numbers from 1 to 8 so that when i click on each page , it should display only 1 row of particular database table,database table consists of 8 rows, But in my code when i click , correct page numbers are passing but it doesn't update and display particular selected pagination row , please can u help me in getting solution to this please.
members.php
<?php
include('assets/page_header.php');
include('db/db.php');
$page="";
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<?php
/*$memno=mysql_real_escape_string($_POST['memno']);
$memname=mysql_real_escape_string($_POST['name']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phno']);
$renewaldate=mysql_real_escape_string($_POST['renewaldate']);
$maxborrowlimit=mysql_real_escape_string($_POST['maxborrowlimit']);
$status=mysql_real_escape_string($_POST['status']);*/
?>
<h1>MEMBERS PAGE</h1>
<?php
$str="select * from member LIMIT 1,1 ";
$query1=mysql_query($str);
echo "<table align='center'>";
echo "<tr><th><MemberNumber</th><th>MemberName</th><th>Address</th><th>Phonenumber</th><th>RenewalDate</th><th>MaxBorrowLimit</th><th>Status</th><th colspan=2>Action</th></tr>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$query2['memno']."</td>";
echo "<td>".$query2['name']."</td>";
echo "<td>".$query2['address']."</td>";
echo "<td>".$query2['phno']."</td>";
echo "<td>".$query2['renewaldate']."</td>";
echo "<td>".$query2['maxborrowlimit']."</td>";
echo "<td>".$query2['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$query2['memno']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='membereditform.php?mem_no=".$query2['memno']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
if($page=="" || $page=='1')
{
$page1=0;
}
else
{
$page1=($page*4)-4;
}
$query1=mysql_query("select * from member");
$count=mysql_num_rows($query1);
$s=$count/1;
echo $s;
echo "<br>";
for($i=1;$i<=$s;$i++)
{
?><a href="members.php?page=<?php echo $i ?>" style="text-decoration":none,"Text-align":center;><?php echo $i?></a><?php
}
?>
Try your code like this:
<?php
include('assets/page_header.php');
include('db/db.php');
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<?php
/*$memno=mysql_real_escape_string($_POST['memno']);
$memname=mysql_real_escape_string($_POST['name']);
$address=mysql_real_escape_string($_POST['address']);
$phonenumber=mysql_real_escape_string($_POST['phno']);
$renewaldate=mysql_real_escape_string($_POST['renewaldate']);
$maxborrowlimit=mysql_real_escape_string($_POST['maxborrowlimit']);
$status=mysql_real_escape_string($_POST['status']);*/
?>
<h1>MEMBERS PAGE</h1>
<?php
$num_rec_per_page = 10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$str = "select * from member LIMIT $start_from, $num_rec_per_page";
$query1=mysql_query($str);
echo "<table align='center'>";
echo "<tr><th><MemberNumber</th><th>MemberName</th><th>Address</th><th>Phonenumber</th><th>RenewalDate</th><th>MaxBorrowLimit</th><th>Status</th><th colspan=2>Action</th></tr>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$query2['memno']."</td>";
echo "<td>".$query2['name']."</td>";
echo "<td>".$query2['address']."</td>";
echo "<td>".$query2['phno']."</td>";
echo "<td>".$query2['renewaldate']."</td>";
echo "<td>".$query2['maxborrowlimit']."</td>";
echo "<td>".$query2['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$query2['memno']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='membereditform.php?mem_no=".$query2['memno']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql = "select * from member";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
if($total_records > 0) {
echo "<a href='members.php?page=1'> ".'<'." </a> "; // Goto 1st page
}
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='members.php?page=".$i."'> ".$i." </a> ";
};
if($total_records > 0) {
echo "<a href='members.php?page=$total_pages'> ".'>'." </a> "; // Goto last page
}
?>
</html>
I am going to delete the particular row of my database, but when i delete the button it tells me that undefined display in deletebookajax.php on line no 54..
So can u please tell me where i have gone wrong...
here is my code
book.php is for displaying the entire book details....
<?php
include('assets/page_header.php');
include('db/db.php');
?>
<html>
<head><title></title>
<style>
h1 {
text-align:center;
}
</style>
</head>
<h1>BOOKS PAGE</h1>
<?php
error_reporting(0);
$str="select * from books";
$query1=mysql_query($str);
$q=mysql_num_rows($query1);
//$query2=mysql_query("select status from bookrentalinfo where bookid=$bookid");
//echo $query2;
//$res=mysql_fetch_array($query2);
echo "<table align='center'>";
echo "<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>Numcopies</th><th>Shelfno</th><th>Status</th><th colspan=2 >Action</th></tr>";
while($rows=mysql_fetch_array($query1))
{
echo "<tr>";
echo "<td>".$rows['bookid']."</td>";
echo "<td>".$rows['title']."</td>";
echo "<td>".$rows['author']."</td>";
echo "<td>".$rows['publisher']."</td>";
echo "<td>".$rows['numcopies']."</td>";
echo "<td>".$rows['shelfno']."</td>";
echo "<td>".$rows['status']."</td>";
echo "<td><button class='button1' data-toggle='tooltip' title='Delete' id=".$rows['bookid']." value='delete' name='delete'><img id='image' src='./images/trash.png'/></button></td>";
echo "<td><a id='colour' class='tooltip' href='edit1form.php?book_id=".$rows['bookid']."'><img id='image' src='./images/small.gif'/><span class='tooltiptext' >Edit</span></a></td>";
echo "</tr>";
}
echo "</table>";
?>
<div id="display">
</div>
<!--echo "<td><button type='button'>delete</button></td>";
/*if($res['status']=="BORROWED")
{
echo "Sorry You Can't Delete The Book";
}
else
{
echo "The Row Is Deleted";
}*/-->
<script type="text/javascript">
$('document').ready(function() {
$(".button1").click(function(e) {
$('[data-toggle="tooltip"]').tooltip();
var strconfirm = confirm("Are you sure you want to delete?");
if(strconfirm)
{
var bookid=$(this).attr("id");
alert(bookid);
dataString='book_id='+bookid;
alert(dataString);
// AJAX Code To Submit Form.
$.ajax({
type: "GET",
url: "db/deletebookajax.php",
data: dataString,
cache: false,
success: function(result){
alert("submitted"+result);
$('#display').html(result);
window.location.href="books.php";
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
else {
return false;
}
});
});
</script>
</body>
</html>
deletebookajax.php
<?php
if(isset($_GET['book_id']))
{
$bookid = $_GET['book_id'];
echo $bookid;
}
include('../assets/page_header.php');
?>
<input type="hidden" name="bookid" value=<?php if(isset($bookid)) echo $bookid; ?>>
<?php
include('db.php');
//if(isset($_POST['bookid']))
//
//$bookid=mysql_real_escape_string($_POST['bookid']);
$delete = "delete from books WHERE bookid=$bookid";
$query1=mysql_query($delete);
echo $query1;
if($query1)
{
$q=mysql_query("select * from books");
echo $q;
$display="<table>";
$display.="<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>numcopies</th><th>shelfno</th><th>status</th><th>Action</th></tr>";
while($row=mysql_fetch_array($q))
{
$display.="<tr>";
$display.= "<td>".$row['bookid']."</td>";
$display.= "<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.="<td>".$row['numcopies']."</td>";
$display.="<td>".$row['shelfno']."</td>";
$display.="<td>".$row['status']."</td>";
$display.= "</tr>";
}
$display.="</table>";
}
else
{
$display.= "U can't delete The book";
}
//echo "nothing";
echo $display;
?>
</body>
</html>
Change Your deletebookajax.php code
<?php
if(isset($_GET['book_id']))
{
$bookid = $_GET['book_id'];
echo $bookid;
}
include('../assets/page_header.php');
?>
<input type="hidden" name="bookid" value=<?php if(isset($bookid)) echo $bookid; ?>>
<?php
//include('db.php');
//if(isset($_POST['bookid']))
//
//$bookid=mysql_real_escape_string($_POST['bookid']);
$user="root";
$server="localhost";
$password="";
$db="library book";
$dbconn = mysqli_connect($server,$user,$password,$db);
$delete = "delete from books WHERE bookid=$bookid";
$query1=mysqli_query($dbconn, $delete);
echo $query1;
if($query1)
{
$q=mysqli_query($dbconn, "select * from books");
echo $q;
$display="<table>";
$display.="<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>numcopies</th><th>shelfno</th><th>status</th><th>Action</th></tr>";
while($row=mysqli_fetch_array($dbconn, $q))
{
$display.="<tr>";
$display.= "<td>".$row['bookid']."</td>";
$display.= "<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.="<td>".$row['numcopies']."</td>";
$display.="<td>".$row['shelfno']."</td>";
$display.="<td>".$row['status']."</td>";
$display.= "</tr>";
}
$display.="</table>";
}
else
{
$display = "U can't delete The book";
}
//echo "nothing";
echo $display;
?>
</body>
</html>
I am working in a project where i want to show only those products to user which are selected by admin from database. Actualy i want to set the approval 1 or 0 in database when admin check or unchecked that checkbox.
jobs.php
$(document).ready(function(){
$('input.check').click(function(){
if($("input.check").is(':checked'))
{
$id = $(this).attr("id");
$.post("handle.php",{action:"checked",id:$id},function(data){
alert("Peoduct is set to display...");
});
}
else
{
alert("unchecked");
$id = $(this).attr("id");
$.post("handle.php",{action:"unchecked",id:$id},function(data){
alert("Peoduct is un-set to display...");
});
}
});
});
<?php
$dbqry = mysql_query("select * from job_category");
echo "<table width='50%', border='2'>
<tr>
<th>Catergory ID</th>
<th>Category Name</th>
<th>Remove</th>
<th>Update</th>
<th>Approval</th>
</tr>";
if($dbqry)
{
while($row = mysql_fetch_array($dbqry))
{
echo "<tr>";
echo "<td align='center'>" . $row['c_id'] . "</td>";
echo "<td align='center'>" . $row['cat_name'] ."</td>";
$cid = $row['c_id'];
$aprv = $row['approval'];
echo "<td align='center'><a href='remove.php?action=cat_remove&cid=$cid'>Remove</a></td>";
echo "<td align='center'>
<a href='Update-form.php?action=cat_update&cid=$cid'>Update</a></td>";
?>
<td align="center">
<input type='checkbox' name='approval' value='approval' id ="<? echo $cid; ?>" class="check"/>
</td>
</tr>
<?
}
echo "</table>";
echo '<br/>';
echo "<a href='add.php?action=cat_add'>Add New Category</a>";
}
else
{
die(mysql_error());
}
?>`
handle.php
`<?php
include 'include/connection.php';
$action = $_POST['action'];
$id = $_POST['id'];
//echo $action;
if($action == "checked")
{
$query = "update job_category set approval=1 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
else if($action == "unchecked")
{
$query = "update job_category set approval=0 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
?>`
Its working but when i refresh the page or seletc the URL and press enter then all the checked data appears unchecked even after that it does not change value of approval from 1 to 0 in database, but still it make me confuse about which items are checked or unchecked. Any suggestion will be appreciated.
modify checkbox line to show checked if approval=1 in database... try this
<input type='checkbox' name='approval' value='approval' <?php if($row["approval"]==1){ echo "checked=\"checked\"";}) ?> id ="<? echo $cid; ?>" class="check"/>
html
<input type='checkbox' name='approval' value='approval' <?php echo $row["approval"]?"checked=\"checked\"":'';?> id ="<? echo $cid; ?>" class="check"/>
then add js
$(function(){
$('input[type=checkbox]').each(function(){
if($(this).attr('checked') && $(this).attr('checked')=='checked'){
$(this).attr('checked', true);
}else{
$(this).attr('checked', false);
}
})
})
and
if($("input.check").is(':checked'))
to
if($(this).is(':checked'))
I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)