my case: after user login ... it will display in table what the book he has ... i added delete link in side each book ... for example i clicked on the link delete in side Math book i want to delete it .. how i can do that please.
i get the books but when i click on delete link nothing happen.
this function display books when user login
public function getBooks($start = 0, $limit = 2)
{
$sql_start = $start * $limit;
$sql_limit = $limit;
//SELECT loginUser.username, Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username="loay";
$query = "SELECT Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $sql_start, $sql_limit";
$statment = $this->db->prepare($query);
$statment->execute([
':username' => $this->username
//,':start' => $start, ':limit' => $limit
]);
$result = $statment->fetchAll();
echo "<table border='1'>
<tr>
<th>Books</th>
<th>Action</th>
</tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>" . $row['nameOfBook'] . "</td>";
echo "<td>" ."<input type='submit' name='delete' value='Delete' method='post' " . "</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['delete'])){
deleteBooks($id1, $id2);
}
}
And this function deleteBooks
public function deleteBooks($id1, $id2)
{
$id1 = $_GET['user_id'];
$id2 = $_GET['book_id'];
$query = "Delete FROM userBook WHERE userBook.user_id=id1 AND userBook.book_id =id2";
$statment = $this->db->prepare($query);
$statment->execute([$id1, $id2]);
$result = $statment->rowCount();
$this->deleteBooks= ($result == "1");
return $this->deleteBooks;
}
First of all you have to add book.id on the link
echo "Delete";
After you click yes you will have a get request.
Not sure where delete function is called after but you can pass $_GET
parameter on it deleteBooks($_GET['id]).
and the function it self
public function deleteBooks($book_id) {
$query = "Delete FROM userBook WHERE userBook.user_id=? AND userBook.book_id =?";
$statment = $this->db->prepare($query);
$statment->bind_param("ss", $user_id, $book_id);
$statment->execute();
//
}
You should store user_id on a session i guess.
Related
I'm trying to compare products and I'm already finished. I just have a problem that my product features are not under the right product names because I need to fill the gaps between with empty <td></td>.
Here is my code from the function that fills the values.
function datatable($id)
{
$conn = connection();
$productPost = $_POST["product"];
$sqlSpecTitle = "Select title as title from product where uid = '$id'";
$resultTitle = mysqli_query($conn, sqlSpecTitle) or die("database error:" . mysqli_error($conn));
foreach ($productPost as $product)
{
$sqlSpecValue = "Select productname, title, value from text join product on uid = uid join feature on uid = uid where productname = '$product" and uid = '$id';
$resultValue = mysqli_query($conn, $sqlSpecValue or die("database error:" . mysqli_error($conn));
if(mysqli_num_row($resultValue) > 0
{
while($row = mysqli_fetch_assoc($resultTitle))
{
echo "<td>" . $row['title'] . "<td>";
}
while ($row = mysqli_fetch_assoc($resultValue))
{
if($row['value'] == null)
{
echo "<td>" . "empty" . "<td>";
}
else
{
echo "<td> . $row['value'] . "</td>";
}
}
}
}
}
The productnames are getting filled in another function that is as much the same.
function headerTable()
{
$conn = connection();
$productPost = $_POST["product"];
foreach ($productPost as $product) {
$sqlSpecValue = "SELECT productname, title, value from text
join product on uid = uid
join feature on uid = uid
where productname = '$product';
$resultValue = mysqli_query($conn, $sqlSpecValue) or die("database error:" . mysqli_error($conn));
$row = mysqli_fetch_assoc($resultValue);
echo "<td id='product'>" . $row['productname'] . "</td>";
}
}
You make a "join" over between your tables, so you only get data if you have something in "text" table.
Just switch to "right join" and it should work.
I want to access this function and change the variable from the URL how can i Do that . i try this to echo something and its work
http://test.local/UUser.php
<?php echo '<p>'. 'Hello Husam' . '</p>'; ?>
but how i can access this function inside UUser.php .
For example i need to change $order = "ASC" to $order = "DESC".
public function getBooks($start = 0, $limit = 2, $order = "ASC")
{
$sql_start = $start * $limit;
$sql_limit = $limit;
$sql_order_by = $order;
$query = "SELECT Library.nameOfBook, userBook.book_id, userBook.user_id FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username ORDER BY Library.nameOfBook $sql_order_by LIMIT $sql_start, $sql_limit";
$statment = $this->db->prepare($query);
$statment->execute([
':username' => $this->username
]);
$result = $statment->fetchAll();
echo "<table id='myTable' border='1'>
<tr>
<th><a id='sorter' href='#'>Books</a></th>
<th>Action</th>
</tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>" . $row['nameOfBook'] . "</td>";
echo "<td>" ."<input type='submit' id='delete".$row['book_id']."-".$row['user_id']."' onclick='deleteBook(this)' name='delete' value='Delete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
echo "";
return count($result);
echo '<p>'. 'Hello Husam' . '</p>';
}
when specific user login it will display what he has books.. and inside each book delete button .. i want delete row when i clicked on delete button ... but when i clicked on delete button it reload the page please i need help
this getbooks function
public function getBooks($start = 0, $limit = 2)
{
$sql_start = $start * $limit;
$sql_limit = $limit;
//SELECT loginUser.username, Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username="loay";
$query = "SELECT Library.nameOfBook FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $sql_start, $sql_limit";
$statment = $this->db->prepare($query);
$statment->execute([
':username' => $this->username
//,':start' => $start, ':limit' => $limit
]);
$result = $statment->fetchAll();
echo "<table border='1'>
<form method='POST'>
<tr>
<th>Books</th>
<th>Action</th>
</tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>" . $row['nameOfBook'] . "</td>";
echo "<td>" ."<input type='submit' name='delete' value='Delete' method='post' >" . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form";
if(isset($_POST['delete'])){
die("SS");
}
}
I made the delete functionality using jquery and ajax.
Above your form code add:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
function deleteBook(b) {
$(document).ready(function() {
var book = $(b).parent('td').prev('td').html();
if(confirm("Are you sure you want to delete book - "+book+"?") == false){
return;
}
var ids = $(b).attr('id').substr(6).split('-');
var book_id_to_delete = ids[0];
var user_id = ids[1];
//alert("book_id is " + book_id_to_delete + ", user_id is " + user_id);
$.ajax({
type: "POST",
url: "" + "deletebook.php",
data: {
'book_id': book_id_to_delete,
'user_id': user_id,
submit: 'submit',
},
success: function(res) {
if (res == "deleted") {
$(b).closest('tr').remove();
} else {
alert(res);
}
}
});
});
}
</script>
In getBooks() function, I added id attribute (containing book and user ids) in delete button so our js code will know which books of a user should be deleted.
Replace your getBooks() function with:
<?php
public function getBooks($start = 0, $limit = 2)
{
$sql_start = $start * $limit;
$sql_limit = $limit;
$query = "SELECT Library.nameOfBook, userBook.book_id, userBook.user_id FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username LIMIT $sql_start, $sql_limit";
$statment = $this->db->prepare($query);
$statment->execute([
':username' => $this->username
]);
$result = $statment->fetchAll();
echo "<table border='1'>
<tr>
<th>Books</th>
<th>Action</th>
</tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>" . $row['nameOfBook'] . "</td>";
echo "<td>" ."<input type='submit' id='delete".$row['book_id']."-".$row['user_id']."' onclick='deleteBook(this)' name='delete' value='Delete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
echo "";
if(isset($_POST['delete'])){
die("SS");
}
}
?>
Create a class function that handles user's book deletion.
In your User class in User.php, add the following function:
public function deleteBook($book_id, $user_id)
{
$stmt = $this->db->prepare("DELETE FROM userBook WHERE book_id = :book_id AND user_id = :user_id");
$stmt->bindValue(":book_id", $book_id);
$stmt->bindValue(":user_id", $user_id);
return $stmt->execute();
}
The below code will be executed by via ajax so that the chosen book will be deleted from database.
Create a file named - deletebook.php
And add this code:
<?php
include_once('User.php');
if(isset($_POST['submit'])){
$object = new User();
if($object->deleteBook($_POST['book_id'], $_POST['user_id'])){
die('deleted');
}
else {
die("fail");
}
}
?>
this is showing all floors of building and i want to show selected building floors how can i do this i use this link floors.php?id=Building1 but its is not working please help me
if i write in there building1 it is working fine where buildingname='building1'
if i use this one where buildingname='$id' it is not working
how can i use like this
floors.php?id=Building1
if i enter this link then it will show all selected building result
<?php
$max_results = 8;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>";
$count++;
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
if(empty($_POST)) {
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
the right is:
if (isset($_GET['id'])) {
$id = filter_input(INPUT_GET, 'id');
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
that may help you.
<?php
$id =isset($_GET['id'])?$_GET['id']:null; // here you put value in $id only if it has some value.
$max_results = 8;
$from = (($page * $max_results) - $max_results);
if($id != null) {
$query = sprintf("SELECT * FROM floors WHERE buildingname='%s' ORDER BY floorno ASC LIMIT $from, $max_results", mysql_real_escape_string($id)); // safe from sql injection
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>";
$count++;
}
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
I have the following code I want to run, but the problem is $this->type is set when the class is created by specifying either petition, proposal, or amendment. As you can see my $sql statement is a UNION of all three, and I want to specify which table (pet,prop,or amend) each row of data comes from.
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT * FROM petition WHERE author = '$username'
UNION SELECT * FROM proposition WHERE author = '$username'
UNION SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) { // $this->type needs to specify pet,prop,amend
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$this->type&id=$row[id]'>$row[title]</a></td>
<td>$this->type</td>
<td>$state</td>
</tr>";
}
}
As you can see, $this->type will only say one of the three. To get my function work how i wanted to, I did this (which I feel is too long & there must be a shorter way).
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$sql = "SELECT * FROM petition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=petition&id=$row[id]'>$row[title]</a></td>
<td>Petition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM proposition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=proposition&id=$row[id]'>$row[title]</a></td>
<td>Proposition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=amendment&id=$row[id]'>$row[title]</a></td>
<td>Amendment</td>
<td>$state</td>
</tr>";
}
}
I would normally do something like the following:
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$tables = array('petition', 'proposition', 'amendment');
foreach($tables as $table) {
$label = ucwords($table);
$sql = "SELECT * FROM $table WHERE author = '" . mysql_real_escape_string($username) . "'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$table&id=$row[id]'>$row[title]</a></td>
<td>$label</td>
<td>$state</td>
</tr>";
}
}
}
Why don't you try the modified SQL:
SELECT 'petition' as typ,title,id FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,title,id FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,totle,id FROM amendment
WHERE author = '$username'"
and then use the typ from each returned row ($row[typ]) instead of $this->type?
The whole thing should be:
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT 'petition' as typ,id,title FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,id,title FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,id,title FROM amendment
WHERE author = '$username'"
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) {
echo "<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$row[typ]&id=$row[id]'>
$row[title]
</a></td>
<td>$row[typ]</td>
<td>$state</td>
</tr>";
}
}
based on what was in your question.