not retrieving id from browser url when clicking on back button - php

I found a problem in my code, when I want to remove a user from database, it has to remove him, the code for removing works perfect, but afterwards, the if condition shows that the user has been removed and I have a Back button there, I click on it and it should redirect me back to the classroom with the rest of the users in it. Classroom is identified by ID of course, but when I click on Back, it shows me classroom without an ID of a class...so its not getting ID...
// get value of id that sent from address bar
$id_student=$_GET['id_student'];
$id_trieda = $_GET['id_triedy'];
// Delete data in mysql from row that has this id
$zmaz='DELETE FROM $tbl_name WHERE id_student="'.$id_student.'" AND id_triedy="'.$id_trieda.'"';
mysqli_real_escape_string($prip, $zmaz);
$row = mysqli_query($prip,$zmaz);
// if successfully deleted
if($row){
echo "Študent bol úspešne vymazaný.";
echo "</br>";
echo "<a href='./trieda.php?id_triedy=".$_GET['id_triedy']."'>Späť do triedy<a/>";
}
else {
echo "Chyba";
}
?>
EDIT:
// get value of id that sent from address bar
$id_student=$_GET['id_student'];
// Delete data in mysql from row that has this id
$zmaz="DELETE FROM $tbl_name WHERE id_student='$id_student'";
$result=mysql_query($zmaz);
// if successfully deleted
if($result){
$id_trieda = $_GET['id_triedy'];
echo "Študent bol úspešne vymazaný.";
echo "</br>";
echo "<a href='./trieda.php?id_triedy=".$id_trieda."'>Späť do triedy<a/>";
}
else {
echo "Chyba";
}
here is edited code which is similar to one that is working when I add a student into the class and then there is a Back button again, but it works....so problem will be in deleting the student, it cant find the ID of class he was in..i think..but I have no idea how to get the ID of the class before he is removed..

Looking at the page you linked to in your comment, I see the problem - you aren't passing id_triedy in your zmazat link. It reads:
http://www.xxx.xx/project/zmazat_studenta.php?id_student=15
Where it should read:
http://www.xxxx.xx/project/zmazat_studenta.php?id_student=15&id_triedy=18
(or whatever the relevant id_triedy is).
Then the $_GET['id_triedy'] in your question code actually has something to get.
You should really build in a check for this kind of thing:
if(isset($_GET['id_triedy'])){
$id_trieda = $_GET['id_triedy'];
} else {
echo 'No trieda ID';
}
This will check the URL for id_triedy and tell you if it's not there.

Is the trieda.php the file that contains the Classroom? If so, this line should be amended:
echo "<a href=\"./trieda.php?id_triedy=".$_GET['id_triedy']."&id_student=".$_GET['id_student']."\">Späť do triedy<a/>";
It looks like you left out the id_student in the URL

Related

Clear all displayed records from mysql on page

I have display.php page and all records from mysql are being displayed on this page with help of while loop. There is a search box on top, now i want when i search particular id in search box then all the displayed records should be hidden and only particular id record should be displayed on screen against which i am searching for.
i tried to hide the the div using jquery but only 1 record hide, but i want to hide all old recorrds
<?php
//all data is displaying through this code
while ($row=mysqli_fetch_assoc($n))
{
echo "<div>Student Name</div>";
echo "<div>".$row['studentName']."</div>";
}
if(isset($_POST['searchBtn']))
{
//here i want hide old data from page and then display searched data
$id=$_POST["stdID"];
$query = "SELECT * FROM std_table Where studentID='$id'" ;
$n=$connection->query($query);
if ($n->num_rows > 0)
{
while ($row=mysqli_fetch_assoc($n))
{
echo "<div>Student Name</div>";
echo "<div>".$row['studentName']."</div>";
}
}
?>
i tried to resolve this problem, but when I search id of specific data then a new row is created below the already displayed records, but i want clear the old data and want just display new data against the searched id
You are always printing all, and then you are asking if there is and id and printing. you want to use else for the print all part
<?php
if(isset($_POST['searchBtn']))
{
//here i want hide old data from page and then display searched data
$id=$_POST["stdID"];
$query = "SELECT * FROM std_table Where studentID='$id'" ;
$n=$connection->query($query);
if ($n->num_rows > 0)
{
while ($row=mysqli_fetch_assoc($n))
{
echo "<div>Student Name</div>";
echo "<div>".$row['studentName']."</div>";
}
}
}else{
//all data is displaying through this code
while ($row=mysqli_fetch_assoc($n)){
echo "<div>Student Name</div>";
echo "<div>".$row['studentName']."</div>";
}?>

How do I change my page to display different information based on how the user access it?

I have a page called profiles.php that displays the users own information when logged in. Recently I made it so the user can click on the name of someone else and it will take them to profiles.php. I want it to display the user's they clicked on profile/information, but it only shows your own information.
The way I have the page now is, it uses a session varaible based on if your logged in and from there it puts your data out in its designated places if that makes sense.
//if theuser is logged in then it turns their database id into a variable for later use
if(isset($_SESSION['userUid'])){
$current = $_SESSION['Id'];
}
//This is the link a person clicks on thats supposed to take them to that user's page. $post is a varaible from a foreach. I tried to make it so once they click it takes them to profiles.php users id (href = "profiles.php/id"'.$post["idUser"].'")
<a href = "profiles.php/id"'.$post["idUser"].'" ><h1>'.$post["UserID"].'</h1></a>
I expect the output to be taking the the current user to the desired user's profile page, but the actual output is taking the current user to their own profile page.
From what I understand you would like to show specific users profile information.
// define your connection
require_once('connection.php');
$users = mysql_query($conn, "Select id, name from users);
$row = mysql_fetch_array($users)
?>
<ul>
<?php
while($row){
echo <? <a href='profile_display.php?id=<?php $row['id'] ?> > <li> <?php $row['name'] ?> </li></a> ;
}
</ul>
in profile display.php
<?php
require_once'connection.php' ;
$id = $_GET['id'];
if(isset($id)) {
$result = mysql_query($conn, 'select * from users where id=$id);
if(!empty($result)) {
// display all value here
} else {
echo "No user profile information was found!"
}
?>
This code is not tested by me. But I would say this is the strategy needed to be used here.
Pseudocode:
List all users
User click specific other user, grab their id
make a query on db for the user information
display if available else display error information
Hope this helps!

php mysql show result one after another

I wanna build a presence check for our choir in the style of tinder but not as complex.
The database contains names and file paths of pictures of the members. When you click on the "present" or "not present" button, the next picture and name should be shown. In the background, the database table should be updated with true/false for presence. (this will be done later)
My problem is that it almost works, but instead of showing one member, it shows all members with their pictures in one single page.
I understand that I could fire with Javascript to continue and paused php-function but I don't get the clue how.
I tried "break" in the php and call the function again but that didn't work.
<?php
$conn = new mysqli(myServer, myUser, myPass, myDbName);
$sql = "SELECT * FROM mitglieder";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img class='pic' src='" .$row["folder"]. "/" .$row["img"]. "'><br>" ;
echo "<div id='name'>" .$row["vorname"]. " " .$row["name"]. "</div> <br>";
echo "<img src=''img.png' id='present' onclick='isPresent()'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<html>
<script>
$( document ).ready(function() {
console.log("Ready");
);
</html>`
You can use php function
mysqli_fetch_all()
assign it on the variable outside the while loop and loop or access the indexes in your code.
For Example:
$data = mysqli_fetch_all();
echo $data[0]['name'];
foreach($data as $item)
{
echo $item['name'];
}
You need a way to establish a "state" between your web page and the PHP backend so that you can step through the data. I suggest something like this:
Use an auto-increment integer primary key for the database. That way you can access the data in index order. Let's name the column id
Have your JS code send a form variable - named something like LAST_ID to the PHP in your get. i.e http://someurl.com/script.php?LAST_ID=0
On your first call to the server, send LAST_ID = 0
In your PHP code, fetch the value like this: $id = $_GET('LAST_ID');
Change your SQL query to use the value to fetch the next member like this:
$sql = sprintf("SELECT * FROM mitglieder where id > %d limit 1", $id); That will get the next member from the DB and return only 1 row (or nothing at the end of data).
Make sure to return the id as part of the form data back to the page and then set LAST_ID to that value on the next call.
You can use a HTTP POST with a form variable to the server call that sets that member id to present (maybe a different script or added to your same PHP script with a test for POST vs GET). I suggest a child table for that indexed on id and date.
I hope that puts you in a good direction. Good luck

Give logged in user delete option only

I am writing a script that allows users to create teams, send contracts to others that are registered, so on and so forth. I want the logged in user to be able to withdraw from a team. The html tables are dynamically populated from mysql using php. I can get it to apply my delete option to all the users within the <td></td> but not just the one that is logged in. Here is some code snippets to help I hope. Basically I want only the user that is logged in to have the delete option.
Id Player
1 User 1 - No Delete Option
2 User 2 - Delete Option (Is the logged in user)
3 User 3 - No Delete Option
session_start();
$get_users_name = $_SESSION['username_u'];
$sql = ("SELECT id, username FROM user WHERE username='$get_users_name'");
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$grab_id = $row['id'];
$grab_user = $row['username'];
//the rest of the table generation done here such as <tr> etc
echo "<td>";
if ($grab_user == $get_users_name) {
echo ''.$grab_user.'';
}
else
{
echo $grab_user;
}
echo "</td>";
//the rest of the table generation done here such as </tr> etc
}
**Edited to fix echo problem caught by #easteregg
Just be sure you will style your code for readability ,then you will notice that you have your if condition inside the echo ;)
Try this it should work!
echo "<td>";
if ($grab_user == $get_users_name) {
echo ''.$grab_user.'';
} else {
echo $grab_user;
}
echo "</td>";
Be aware of the fact, that you should check again in the user_delete.php if a user has the right to delete something , otherwise you will run into some strange sort of trouble ;)

Knowing which <li> item is selected using PHP and mySQL

I have 2 questions. One is if there is any error in this code.
The 2nd question I want to ask is how do you know which <li> item is selected. Right now, the code performs a search in mySQL for all rows that matches city, language and level and returns the results in a list item.
I want it so that when the user clicks on anyone of the list items, it will goes into another page displaying a more detail description by querying the selected list item.
I have a guess, which is for step 2, I also grab the ID (primary key) for each row and somehow keep that stored within the list but not echo.. Would I need to wrap <a> in <form action="XX.php" method="get">?
<?php
//1. Define variables
$find_language = $_GET['find_language'];
$find_level = $_GET['find_level'];
$find_city = $_GET['find_city'];
//2. Perform database query
$results = mysql_query("
SELECT name, city, language, level, language_learn, learn_level FROM user
WHERE city='{$find_city}' && language='{$find_language}' && level='{$find_level}'", $connection)
or die("Database query failed: ". mysql_error());
//3. Use returned data
while ($row = mysql_fetch_array($results)){
echo "<li>";
echo "<a href=\"#result_detail\" data-transition=\"flow\">";
echo "<h3>".$row["name"]."</h3>";
echo "<p>Lives in ".$row["city"]."</p>";
echo "<p>Knows ".$row["level"]." ".$row["language"]."</p>";
echo "<p>Wants to learn ".$row["learn_level"]." ".$row["language_learn"]."</p>";
echo "</a>";
echo "</li>";}
?>
Your code looks alright from what I can see. Does it not work?
One way would be to get the ID through your SQL-query as well and then add the id to the href in your link. Then you can fetch it through the querystring on the other page, to display the proper post depending on which li-element the user clicked on:
echo "<a href=\"details.php?id=". $row["id"] ."\" data-transition=\"flow\">";
Not sure how you mean "somehow keep that stored within the list but not echo" - it wouldn't be possible to store anything in the list, if it is not echoed, as it then wouldn't be sent to the client. You can of course store the id in a data-attribute on the li-element, which won't be displayed to the user. It will however be visible through the source code! Don't know why that should be a problem though?

Categories