I'm stuck in a place where I want to view the other fields of a mysql table when I click onto a linked field.
Here is the code which is used to create a field into a link
<td><a href="fieldview.php?id=' .$row[0]. '"><b>' .$row[1]. '</b></td>
Here $row[0] is the field named "id" in mysql table(exampletable) & $row[1] is the field named "title".
Now, at the "fieldview.php" page I used this code to view other fields:
$id=$_GET['id'];
$sql="SELECT * FROM exampletable WHERE id= '$id'";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
echo '<table id="customers">';
echo '<tr><th>Field 1</th> <th>Field 2</th> <th>Field 3</th></tr>';
echo '<tr class="alt"><td><b>' .$row[1]. '</b></td><td><b>' .$row[2]. '</b></td><td><b>Today!</b></td></tr>';
echo '</table>';
}
But I get nothing at that page. Please can anyone tell me what's the solution?
Try jakenoble's suggestion. if you dont see the problem maybe its due to how you construct your link in the first place.
try modifying to to something like this:
<td><b> <?php echo $row[1] ?></b></td>
Did you connected your database ?
FIRST CONNECT DB!!!!!!!!!
Try Echo:
echo $_GET['id'];
in your file.
But I get nothing at that page. Please can anyone tell me what's the solution?
Did you got the HTML table with nothing?
TRY the following:
<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo $id=$_GET['id'];
$sql="SELECT * FROM exampletable WHERE id= '$id'";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
?>
<table id="customers">
<tr><th>Field 1</th> <th>Field 2</th> <th>Field 3</th></tr>
<tr class="alt"><td><b><?php echo $row[0]; ?></b></td><td><b><?php echo $row[1]; ?></b></td><td><b>Today!</b></td></tr>
</table>
<?
}
?>
If you get no data in your HTML Tables then run the MySQL query in your Query browser with the ID to find you have data for the ID.
Do specify what you have tried?
Related
I'm trying to bind the id from a row to the href output. that is getting a url something?id=*** in order to use $_GET and bring the id on the next page.
I need to be the id on the same row that is clicked on a table I'm displaying.
If I try to bind it by stating href=" wahtever?id=<php echo $row['id'] ?> the id will return as empty. If I use a loop it works but give me all the id's on the table.
I tried different solutions I found on internet like stating echo '<td> <a href="****?id='.$row['id'].' </a></td>' or making a new selection using php code on the href link... nothing seems to work.
I'm confused, how can I make a link on a table that will include the id of the clicked row?
My code looks like this now:
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php $sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($rows = mysqli_fetch_assoc($result){
echo $rows['ID'] ; ?>">
Just to make it clearer, it is a simple table displaying 4 columns with different data using a loop, the first column is the id and the second one would be the topic, where I trying to build the links.
It sounds like you have 5 columns in a database table and you want to show them on the page, and link the topic cell to the topic page and pass the id of that topic.
I cleaned your code up a little and gave an example of how to do that. Keep in mind, I'm using an associative array so you'll need to be sure it matches what the columns are called in your database.
<table>
<tr>
<th>ID</th>
<th>Topic</th>
<th>Answers</th>
<th>Views</th>
<th>Date</th>
</tr>
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = myslqi_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td bgcolor="#FAB1CA">
<a href="view_topic.php?id=<?php echo $row['id']; ?>">
<?php echo $row['topic']; ?>
</a>
</td>
<td><?php echo $row['answers']; ?></td>
<td><?php echo $row['views']; ?></td>
<td><?php echo $row['theDate']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I cant see $row variable in your code
you can use myslqi_fetch_assoc for get $row variable
i think this answer its true
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php echo $row['id']?>go to view_topic</a></td>
<?php
}
?>
I have a Table that displays titled and first name columns and delete link on the 3rd column.
Unfortunately for a reason i don't understand, records are not deleting when the Delete link is clicked.
Please friends, help me figure out what's wrong here.
<?php
$user_id = $_SESSION["user_id"]; //brought here via
session
//select statement here
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["titled"]. '</td>
<td> '.$row["firstname"] .'</td>
<td><a href="user_delete.php?
delete=$row[user_id]">Delete</a>
</td>
</tr>';
}
} else {
echo "0 results";
}
?>
user_delete.php code
<?php
session_start();
require_once $_SERVER['DOCUMENT_ROOT'] .
`enter code here`'/soap/includes/server.php';
if(isset($_GET["delete"]) )
{
$user_id = $_GET["delete"];
$sql= "DELETE FROM users WHERE user_id='$user_id'";
$res= mysqli_query($con, $sql) or
die("Failed".mysqli_error($con));
echo "<meta http-equiv='refresh'
content='0;url=user_settings.php'>";
}
?>
While you must debug code and add results for check what could be the issue. But one issue I think is using variable $row[user_id] in single quoted string. PHP do not parse variable in single quoted string. So either transfer all html code from single to double quotes or atleast append $row[user_id] seperatly like other $row[firstname]
echo '<tr>
<td scope="row">' . $row["titled"]. '</td>
<td> '.$row["firstname"] .'</td>
<td><a href="user_delete.php?
delete='.$row["user_id"].'">Delete</a>
</td>
</tr>'
I have a MySql database which has users and each user gets a grade see below:
ID NAME GRADE
1 Sean 10
2 Sarah 15
3 Seo 12
4 Ste 32
Basically, I want to be able to dynamically print out whats stored in the database and add a text input box which enables me to enter in grades which are stored for each individual person. I then want this grade to be stored in the database for the user. Is this possible?
I have code which prints out the users but doesn't include a text box
<table style="width:50%" style = "background-color: transparent;">
<tr>
<th>ID</th>
<th>NAME</th>
<th>GRADE</th>
</tr>
<?php
$sql = "SELECT id, name, grade FROM students";
$result = $conn -> query ($sql);
if($result -> num_rows >0){
//output data of each row
while($row = $result -> fetch_assoc()){
?>
<tr>
<td> <?php echo $row["id"] ?> </td>
<td> <?php echo $row["name"] ?> </td>
<td> <?php echo $row["grade"] ?> </td>
</tr>
?>
</table>
Research a bit on HTML forms here
HTML Forms
You can either use post or get methods for the form to send the values to PHP.
After you have set up your form you can simply use the variables $_GET[formvariable] if you are using get or
$_POST[formvariable] if you are using post.
You can then use mysql to update values in the database when the form is submitted.
I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course
I'm very new to php and have been given the task to update the table in the database when user select the check box and then edit the information in the text box. I'm able to pull the information from database and display it on the screen, however I'm not too sure how to update the database when user selects the checkbox and update the field
Here is what I want to achieve:
1) On clicking the checkbox, the row gets editable.
2) After updating the field value, and clicking button "update", the value gets updated in the d/b
<div class="table-responsive">
<table class="table table-condensed">
<thead>
<tr>
<th width="20%">Field Name</th>
<th width="52%">Field Text</th>
<th width="32%">Select to Update</th>
</tr>
</thead>
<tbody>
<?php
if($table = true)
{
while($row = $result->fetch_assoc())
{
$fieldName[] = $row['fieldName'];
$fieldText[] = $row['fieldText'];
$fieldID[] = $row['ID'];
echo "<tr>";
echo "<td>".$row['fieldName']."</td>";
echo "<td>".$row['fieldText']."</td>";
echo "<td>"."<input type= 'checkbox' value='{$row['ID']}', name = ID[]>"."</td>";
echo "</tr>";
}
}
else
{echo "No results found";}
?>
</table>
</div>
Not sure how to proceed further. After checking forums, I found that Jquery is the way to go, but not sure how to make the field updatable.
Please note that I haven't provided the code for the "Update" functionality.
Sorry, i am not good in jquery. But i think for this u could use
$('.your-check-box').on('change', function() {
if(this.checked)
{
//change text on inputs
}
else {
//change inputs on text
}
});
this is quick example:
http://jsfiddle.net/qr4ova98/
For DB update u need to send ajax with data on event.