I have represented data in tabular form with no problem. But when i click the delete button i want the $id i.e. row[0] of my database to pass to another page delete_user_post.php where i can capture this value of $id using $_REQUEST. When i echo $id it shows its associated id. But i am not able to pass that value in next page.what have i done wrong??
`
<tbody>
<?php while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td>
<div class="control-group">
<label class="control-label"></label>
<div class="controls">
<?php $id = $row[0];
echo $id;
?>
Delete
</td>
</div>
</tr>
<?php } ?>
</tbody>
My user_delete_post.php looks like this:
<?php
include ('include/DB_Functions.php');
$id =$_REQUEST['roomid'];
// sending query
mysql_query("DELETE FROM room_tb WHERE roomID=$id") or die(mysql_error());
?>
It looks like you're not actually echoing the $id.
Replace your a tag with this:
Delete
Also please look up mysqli_ or PDO as a replacement for mysql_
Also think long and hard about if you really want unescaped and unverified IDs to be deleted from your database.
Try to put $id in php tags like,
Delete
Related
I am trying to develop an application form app. .
I want to be able to click on an application id and be able to generate the application form in details. So am using Sessions and it only works for the last id in the database.
Basically passing an application ID into a session then calling it as a parameter for the next page. If this method cant work, could there be another? Hope am clear enough.
Here is my code.
<td>
<a href="applicants_details.php"> <?php echo $Applicant->getApplicationID(); ?>
<?php php $_SESSION["SELECTEDID"] = $Applicant->GetApplicationID(); ?>
</td>
Use $_GET,
applications.php
<a href="applicants_details.php?applicant_id=<?php echo $Applicant->GetApplicationID(); ?>"><?php echo $Applicant->GetApplicationID(); ?><\a>
applicant_details.php
<?php
echo $_GET['applicant_id'];
?>
Change your code on the table to:
<td>
<a href="applicants_details.php?id=<?php echo $Applicant->getApplicationID(); ?>">
<?php echo $Applicant->getApplicationID(); ?>
</a>
</td>
Then on the next page you can get the ID with:
<?php
$id = $_GET['id'];
?>
I have selecting data from MySQL table and returned data looks like:
Name Rank Vessel Type Preview
John 1 ab View
Lisa 1 cd View
Carl 2 bd View
As you see above View should be link to user's profiles depending on their Id.
For now I'm using table as you see below and this is line for View link:
<?php echo "<td> View</td>"; ?>
I don't know why, but It redirecting users like: www.etc.com/preview.php?Id= without provided Id.
If I'm using something like:
<?php echo "<td> View " . $Id . "</td>"; ?>
It returning View Id for example View 1, that's mean $Id variable is fine. Have you ideas what's wrong?
Also I want to ask you If it is correct way for redirecting? Or there is any other / better way to do that?
This is PHP:
<?php
if(isset($UserID)) {
$users = $con->prepare("
SELECT DISTINCT Id,
CONCAT(FirstName, ' ', LastName) AS FullName,
RankApplied,
VesselsType
FROM tbl
");
$users->execute();
$users->bind_result($Id, $FName, $RankApplied, $VesselsType);
} else {
echo "There is no User ID detected, try to refresh browser.";
}
?>
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Vessel Type</th>
<th>Preview</th>
</tr>
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FName; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $VesselsType; ?></td>
<?php echo "<td> View</td>"; ?>
</tr>
<?php
}
?>
</table>
You are closing your href quote before including the id, change the line to the following to get it to work:
<?php echo "<td> <a href='preview.php?Id=" . $Id . "'>View</a></td>"; ?>
Replace
<?php echo "<td> View</td>"; ?>
with
<?php echo "<td> View</td>"; ?>
You are having a ' after ?Id= which was creating the problem..
Instead of concatenating the $ID you could directly use
<?php echo "<td> <a href='preview.php?Id=$Id'>View</a></td>"; ?>
As in the docs
Double quote strings will display a host of escaped characters
(including some regexes), and variables in the strings will be
evaluated. An important point here is that you can use curly braces to
isolate the name of the variable you want evaluated. For example let's
say you have the variable $type and you what to echo "The $types are"
That will look for the variable $types. To get around this use echo
"The {$type}s are" You can put the left brace before or after the
dollar sign. Take a look at string parsing to see how to use array
variables and such
Please see this answer
Solving your problem:
<?php echo "<td> <a href='preview.php?Id=".$Id . "'>View</a></td>"; ?>
Alternatively you may define before using it:
<?php $link = "preview.php?Id=$Id"; ?>
<?php echo "<a href='$link'>View</a>"; ?>
Also you may try PHP in HTML
<a href='<?php echo "preview.php?Id=$Id"?>'>View</a>
I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}
I'm trying to wrap my head around PHP and variable scope. Take a look at the following:
<?php foreach ($data as $tip) { ?>
<tr>
<td><?php echo $tip['id']; ?></td>
<td><?php echo $tip['title']; ?></td>
<td class="delete"><i class="icon-cross"></i></td>
</tr>
<?php } ?>
This just runs a foreach loop that pulls some information out of the database and displays it in a table. The last table cell has an icon in it for deleting that article. What I'm trying to do is have a modal popup that asks for conformation to delete that specific article but I cannot tie the tip id with the delete button because the modal window sits outside the loop. How can I go about accessing the individual id?
Do this :
<?php foreach ($data as $tip) { ?>
<tr>
<td><?php echo $tip['id']; ?></td>
<td><?php echo $tip['title']; ?></td>
<td class="delete" onclick="deleteArticle(<?php echo $tip['id'] ?>)">
<a href="#deleteModal" class="modal">
<i class="icon-cross"></i>
</a>
</td>
</tr>
<?php } ?>
<script>
function deleteArticle(id){
// now you can do what ever you want to do with this id
}
</script>
There is a built it javascript function called "confirm".
If you're using JQuery (And I assume you are) try this:
$('.delete').click(function(){
var check = confirm("Are you sure you want to delete this article?");
if(check)
{
// you code here
}
else return false;
});
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']."'>"; ?>