Display wrong information with the good row value - php

I have a problem with the code below. In fact, it's supposed to show the information of a table, where the MomentEvent value match with the row requested. The only problem, is that sometimes, it show the information of a complete diffrent row! And I don't have any idea why! Does the problem come from my code?
And in my table, the information are placed in order, so they are always added one after an other.
<?php
include('base.php');
?>
<?php
if(isset($_GET['MomentEvent']))
{
$MomentEvent = intval($_GET['MomentEvent']);
$dn = mysql_query("select Ordre, Confidentialite, ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent=$MomentEvent");
if(mysql_num_rows($dn)>0)
{
$dnn = mysql_fetch_array($dn);
?>
This is the profile of "<?php echo htmlentities($dnn['TitreEvent']); ?>" :
<table style="width:500px;">
<tr><td>
<?php
if($dnn['avatar']!='')
{
echo '<img src="'.htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8').'" alt="Avatar" style="max-width:100px;max-height:100px;" />';
}
else
{
echo 'This user dont have an avatar.';
}
?>
</td>
<td class="left"><h1><?php echo ($dnn['TitreEvent']); ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</tr>
</table>
<?php
}
else
{
echo 'Sorry, any event found';
}
}
else
{
echo 'The user ID is not defined.';
}
?>
</body>
</html>

you have missed </td> in your code.
and use echo $dnn['TitreEvent']; without parenthesis
try this
<td class="left"><h1><?php echo $dnn['TitreEvent']; ?></h1>
Email: <?php echo htmlentities($dnn['DescriptionEvent']); ?><br />
<a href=*****.php>Retour.</a>
</td>
^^----you missed this
EDIT:
Ithink you have many result returned from your sql and you need to specify more in your query .
like that
where MomentEvent=$MomentEvent AND user = $theUser

Now all works fine with these changes:
$MomentEvent = $_GET['MomentEvent'];
$sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent from users_event where MomentEvent='$MomentEvent'";
$dn = mysql_query($sql);

Related

$("").click(function(){ Allow of click of images in a loop

I am trying to allow user to click on the picture that is displayed on the website, currently, the picture is pull out from a database and is on a loop. If i set a id, user can only click on the first pic that is shown, is there anyway for the $("#HELP").click(function(){ to allow me to have one code but allow all the pic to be click ?
<div id = 'HotelContentsmallpic'>
<?php
$cols=4; // Here we define the number of columns
echo "<table>"; // The container table with $cols columns
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysqli_fetch_array($result2);
if($row){
$img = $row['IMG'];
$cat = $row['Category'];
?>
<td>
<table>
<tr align="top">
<td><img id = "HELP" src="images/Hotel/<?php echo $img ?>.jpg" class="img-responsive" /></td>
<td>
</td>
<td width="50"> </td> <!-- Create gap between columns -->
</tr>
</table>
</td>
<?php
}
else{
echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
?>
<script>
$(document).ready(function(){
$("#HELP").click(function(){
window.location='hotels.php?Category=<?php echo $cat ?>&Pic=<?php echo $img ?>';
});
});
</script>
<?php
}
} while($row);
echo "</table>";
?>
</div>
You need to change two things:
In HTML use img tag like that:
<img onclick="redirect_url('<?=$cat?>','<?=$img?>')" src="images/Hotel/<?=$img?>.jpg" class="img-responsive" />
And in Javascript use this:
<script type="text/javascript">
function redirect_url(cat,img)
{
window.location='hotels.php?Category='+cat+'&Pic='+img+'';
}
</script>
In this solution, no need to use #HELP id in this.
You are using the the ID attribute, in this case you should use the class attribute since there are multiple elements of the type "HELP". So basically #HELP to .HELP and id="HELP" to class="HELP"

$_GET does not work and query the property

I have a problem with $_GET method. I have retrieved some data about admins of a webpage from database & I added a hyperlink for users to get the information about that them.
Here's the code in my 1st page:
<?php if(($adminlevel)==1){
echo '
<h4 class="widgettitle">List of admins</h4>
<table class="table responsive">
<thead>
<tr>
<th>Admin Level</th>
</tr>
</thead>
'; getAdmins(); echo '
</table>
';
}else{
echo '<h4 class="widgettitle">You dont have permission to see this table</h4>';
}
?>
<div class="divider15"></div>
The function getAdmins() goes like this:
<?php
function getAdmins(){
global $con;
$get_admin = "select * from admins order by id";
$run_admin = mysqli_query($con,$get_admin);
while($row_admin = mysqli_fetch_array($run_admin)){
$id_admin = $row_admin['id'];
echo "
<tbody>
<tr>
<td>Trident</td>
<td class='center'><a href='editlevel.php?id=$id_admin' title='Clik to change admin level' target='_blank'>$adminlevel_admin</a></td>
</tr>
</tbody>
";
}
}
?>
As you see I link the users from my first page to another page which is called editlevel.php by the function getAdmins().
Therefore I made my hyperlink like this:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
And Here's the editlevel.php page:
<body>
<?php
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
$get_result = "select * from admins where id='$result_id'";
$run_result = mysqli_query($con,$get_result);
while($row_result= mysqli_fetch_array($run_result)){
$id_target = $row_result['id'];
$username_target = $row_result['username'];
$adminlevel_target = $row_result['adminlevel'];
$email_target = $row_result['email'];
echo '
<div class="mainwrapper">
<div class="header">
'; include "php/php_includes/overall/header.inc.php"; echo'
</div>
<div class="leftpanel">
';include "php/php_includes/overall/leftpanel.inc.php"; echo '
</div><!-- leftpanel -->
<div class="rightpanel">
'; include "php/php_includes/gadgets/rightpanel.editlevel.php"; echo '
</div><!--rightpanel-->
</div><!--mainwrapper-->
';
}
}
?>
</body>
So basically I used if (isset($_GET['id_admin'])){ to get the results of the item which user clicked & try to retrieve the data of that item from database via that.. But the problem is nothing appears at my screen. No error message & no result. Please if you know how can I solve it please let me know!
It appears that your link is:
<a href='editlevel.php?id=$id_admin'>$adminlevel_admin</a>
When it should be:
<a href='editlevel.php?id_admin=$id_admin'>$adminlevel_admin</a>
In order for it to work with:
if (isset($_GET['id_admin'])){
$result_id = $_GET['id_admin'];
Edit: It goes without saying, you should never trust user input (such as $_GET). These values should be validated and sanitised before being used in SQL queries.

Detecting empty cells

I had to display a table using php and html, that select data from sql server. Their is a cell called echo photo that sometimes is empty, and sometimes is loaded with an image. How to change my code to test:
if (it is empty){
echo "No echo files";
}
else{
<a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a>
}
My php and html codes are:
<?php
...
while($rows=mysqli_fetch_array($result)){
$data =$rows['echo_photo'];
$dataFile = str_replace('/', '\\', $data);
...
?>
here in this td of html table I want to put if else conditions
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>
<?php if (!empty($data)) : ?>
<td align="center"><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Click to download</a></td>
<?php else: ?>
<td align="center">No Data Available</td>
<?php endif; ?>
Why dont you change the query.
Select * FROM table WHERE echo_photo NOT LIKE ''
Than you will only get the values that exists
Otherwise you can do it with a if statement like:
if(!empty($data)){
// echo html code
}else{
echo "No Data Found!";

Delete PDO OOP Problems

Good day everyone.
I totally need your help.I am pretty new to oop & pdo and I am having problems with my delete function. The item will not be delete whenever I click the delete button. It seems like I can't call the id. I just don't know what to do. Please help me solve this problem.
Here is my code.
For the class(codex):
public function deleteData($id,$table)
{
$q = "DELETE FROM $table WHERE id = :id";
$stmt = $this->con->prepare($q);
$stmt->execute(array(':id'=>$id));
}
For the UI and the page where I call the id:
<?php
include_once "../styles/header-menu-out.php";
include_once "dbconnection.php";
function __autoload($class){
include_once("../main/".$class.".php");}
$code = new codex("localhost","library_books","root","" );
$books = $code->showData("book_info");
if(isset($_REQUEST['id'])){
if($code->deleteData($_REQUEST['id'],"book_info")){
echo "<script type='text/javascript'>
alert('Book Information have been deleted.');
window.location='bookDeleteUI.php';
</script>";
}}
?>
<html>
<head><link rel="stylesheet" type="text/css" href="../styles/library_style.css"></head>
<title>Book-A-Holic: Delete & Update Books</title>
<body><br /><center>
<div id="content"><div class="echoname"><br/><br/><b><h2>Book Settings</h2></b><br/></div>
<table id="tablecolor" class="echoname" border="1">
<td>ID</td>
<td>Title</td>
<td>Author</td>
<td>ISBN</td>
<td>Publisher</td>
<td>Language</td>
<td>Genre</td>
<td>Quantity</td>
<td>Availability</td>
<td>Queue</td>
<td><center>Settings</center></td>
<?php
echo "<pre>";
foreach ($books as $book)
{
echo "<tr>";
extract($book);
echo "<td>".$id."</td>";
echo "<td>".$title."</td>";
echo "<td>".$author."</td>";
echo "<td>".$isbn."</td>";
echo "<td>".$publisher."</td>";
echo "<td>".$language."</td>";
echo "<td>".$genre."</td>";
echo "<td>".$quantity."</td>";
echo "<td>".$availability."</td>";
echo "<td>".$queue."</td>";
?>
<td><button class="btn"><a href="bookUpdateUI.php?update=$id" >Edit</a></button>
<button class="btn">Delete</button></td>
<?php echo "</td>";
}
echo "</pre>";
?>
</table><br />
</div></center>
</body>
</hmtl>
<?php include_once "../styles/footer-admin.php"; ?>
Looks like you've almost got it and well done on using PDO with parametized queries. The problem is that you're not outputting the $id through the php interpreter. You need to do this:
<button class="btn">Delete</button></td>

PHP $_GET is empty trying first REST requests

trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>

Categories