I am new to php, mysql. currently i'm building a website. with the search query the data is displayed well. (in this case the search query is 'name %like%'). now i want to open another page that will display the profile of any one of the name displayed (when clicked) in the previous query. how to acheive this?
you'll have to select, for example, user_id when searching.
and then, your code could look like
<?php
include("db.php");
$result = mysql_query("SELECT user_id, username FROM user WHERE username LIKE '%".mysql_real_escape_string($_POST['username'])."%'");
echo "<table>";
while($row = mysql_fetch_assoc($result)
{
echo '<tr><td>'.$row['username'].'</td></tr>';
}
echo "</Table>";
Related
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
So, I have a database full of information, essentially what I want is I have a table with a list of Bands, they have things like contact info, home cities and whom to contact.I want to turn the page table entries into links that link to a html page that get loaded with that database entry's full information. I know this sounds weird, I'm having trouble describing it too. But I have a coverpage that has a table with bands, it states their name,contact info and homecity. I want to turn the table entries of the bands into links that link to a NEW page that store all the database information on them. For example the database has a more details like genres, descriptions, band members etc.
<?php
//echo("Hello Retrieving Table");
$bandquery = "SELECT Bands.Name,Bands.City,Bands.Contact,Bands.Primary_Genre FROM Bands";
//echo("Trimming Query");
trim($bandquery);
//echo("Stripping Slashes");
$bandquery = stripslashes($bandquery);
//echo("Setting up Query");
$results = $db->query($bandquery);
if (!$results){
echo("<h2>Error: The query could not be executed.</h2>");
$error = $db->lastErrorMsg();
echo("<p>$error<p>");
exit;
}
echo("<table><tr>");
for($i=0;$i<$num_cols;$i++){
$head = $results->columnName($i);
echo("<th>$head</th>");
}
echo ("</tr>");
// Write rows into table
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
echo ("<tr>");
foreach($row as $v){ //I want id of the link to be the band id which Idk how to grab those and use this.
echo ("<td>"$v </td>");
}
echo ("</tr>");
}
echo ("</table>");
?>
I am new to php and js and I am working on a simply on one webpage but i get stuck. What I am trying to do is next:
I am logging on the web
I am using the user id to retrieve data associated with that id from table in db. I am retrieving just the "name" column and using it as a hyperlink.
Here is my code:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
echo '<tr>
<td><ul><li><a href="casedetails.php">'.$rows['Name'].'</li></ul></td>
</tr>';
}
It is retrieving data that is in the row named "name" in the table, and I want when i click on the hyperlink to save the name of the hyperlink (for ex. Murder(link to cases.php) ). because i want to use it in a query on the page of the hyperlink. I will appreciate if someone can help me!
If you want to click the link and it takes you a different page while remembering what ID (or name) was clicked, you would write your code like this:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
$name - $rows['Name'];
echo '<tr>
<td><ul><li><a href="casedetails.php?name='.$name.'">'.$name.'</li></ul></td>
</tr>';
}
So, whenever you go to casedetails.php, the variable $name will be in a GET that you can retrieve and use for queries. For example, on casedetails.php you would have $newName = $_GET['name']; and that would retrieve the name.
Some parts of my page should be shown only for ID's that match this criteria:
<?php
$admins = mysql_query("SELECT id FROM users WHERE is_admin='y'");
?>
so it can be like 1,4 or 1,4,7,3,10,[...] whatever, so these people (ID's) can see smth dedicated, now...
Previously, I had smth like:
<?php
if ($_SESSION['id'] == $customer['id'])
{
echo "<br />";
[...] Dedicated content [...]
}
?>
Which worked until today. Now I need to make it for some more people, database related, if ($_SESSION['id'] == result of the SELECT. How can I achieve that?
Thanks in advice!
just put all those people into the database and check like this:
<?php
$admins = mysql_query("SELECT * FROM users WHERE id='". mysql_real_escape_string($_SESSION['id']) ."' and is_admin='y'");
?>
At that point, if at least one row will return (and yes, it should return exactly one only row) you can proceed with the dedicated content.
You can drop the session id into the query and use mysql_num_rows() to check if you selected any rows:
<?php
$admins = mysql_query("SELECT id FROM users WHERE id=".(int)$_SESSION['id']." AND is_admin='y'");
?>
if the session id is the same as the users id in the database, you could check where session id is an admin in the database. this will simplify your needs
needing some advice.
I am wanting to include 4 drop down lists on a website, which all contain data from different fields in a mysql table.
I then want to be able to press a submit button and display the required data on a webpage.
I am having trouble with knowing what programming language to use and also finding it difficult to find any tutorials. Any help would be greatly appreciated.
Thanks
You mean a HTML dropdown list or just a select dropdown in a form?
If you mean a select dropdown in a form you could do something like this:
<?PHP
$query = mysql_query("SELECT value FROM table ORDER BY value ASC") or die(mysql_error());
$result = mysql_num_rows($result);
// If no results have been found or when table is empty?
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="your_result.php">';
echo '<select name="list" id="lists">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['value'].'">'.$fetch['value'].'</option>';
}
echo '</select>';
echo '</form>';
}
?>
And then in your_result.php you should fetch the data from your MySQL database based on value from the (when you fetch use mysql_real_escape_string):
<?PHP $_POST['list']; ?>
You could also do everything in just one file, but thats up to you. Try to use google, there are dozens of tutorials out there.