How to retrieve the name of a link? - php

I have a database and I generated a code that creates links depending on the name of the person in the database and I want the names to be
"sent" in the next page ( link's page). So I used the method "get" to do that but it does not work and I don't know why.
$sql = "SELECT * FROM table_personne; ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo ("<a href=\"traitement.php?firsname=".$row["firsname"]."%"."&lastname=".$row["lastname"]."%".">".$row["firsname"]. " ".$row["lastname"]);
echo "<br>";
}
} else {
echo " 0 results";
}
The URL that is created is: traitement.php?prenom=paul%&nom=vincent%>paul%20vincent
Problems: The URL does not match the name of the person displayed and the lastname is "vincent%>paul" meaning lastname=$GET["lastname"]= "firstname%lastname<" and there is no $GET["firstname"].
Thanks a lot.

What about this :
echo (''.$row["firstname"]. " ".$row["lastname"]).'';

Maybe You have to change this :
$row["firsname"]
to
$row["firstname"]

Related

Updating PHP shows double data where it shouldn't

So me and my group of students are making a web application for a company. On this page we're trying to show different startup companies with different group numbers. We'd also want to have the option to update this number. The update part works fine and it shows the right group number behind someone his/her name. The real problem here is, if I update one of the different group numbers and reload the page, all of the groups have the same number as shown on the webpage, but in the database it shows the correct number that i've updated.
So I would like to have the right update number shown right away. This is my php code down below.
<?php
session_start();
require 'header.php'; //
require 'includes/dbh.inc.php';
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$update = mysqli_real_escape_string($conn, $_POST['group']);
$sql = "SELECT user_uid, user_name, group_id, active FROM groups";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
if(!isset($_POST['groupnumber'])) {
$gnum = $row['group_id'];
}
else {
$gnum = $update;
}
echo "<br> Name : " . $row["user_name"] . " - Group number:" . $gnum . "<br> Select new group" .
"<form method='post'>
<input type='text' name='group'>
<button name='groupnumber' value='". $row['user_uid'] ."'> Update </button>
</form>". "<br>";
}
} else {
echo "0 results";
}
if(isset($_POST['groupnumber'])){//if the submit button is clicked
$id = mysqli_real_escape_string($conn, $_POST['groupnumber']);
$update = mysqli_real_escape_string($conn, $_POST['group']);
$query="UPDATE groups SET group_id='$update' where user_uid = $id";
$conn->query($query) or die("Cannot update");//update or error
}
$conn->close();
?>
Sorry if it's kinda messy, I'm still a student and i'm just a basic PHP guy :)
Of course it does, that's what you have in your while-loop, here:
if(!isset($_POST['groupnumber'])) {
$gnum = $row['group_id'];
}
else {
// On a POST always show the posted value for all groups
$gnum = $update;
}

Creating An Array from MYSQLI query

I am updating all my code to Mysqli, before I did I had this code and it worked:
while($row = mysql_fetch_assoc($WildHorses)){
$InWild[] = $row['id'];
}
$RandomWild = array_rand($InWild);
$RandomHorse = $InWild[$RandomWild];
This is my SELECT statement:
$sql = "SELECT Horse.id, Horse.Name, Horse.Age, Horse.Image_name, Horse.Owner, Horse.Barn, Images.Image_path, Images.Image_name FROM Horse, Images WHERE Horse.Owner = '$colname_WildHorseList' AND Images.Image_name = Horse.Image_name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["Name"]. " ImageName: " . $row["Image_name"]. "<br>";
}
} else {
echo "0 results";
}
The SELECT statement ends up echoing all of the correct information, but I want to make an array of only the Id's so that I can pick one at random each time a button is clicked.
I have tried multiple different copies and pastes of code to try and get what I want, but nothing seems to come out right.
Can someone point me in the right direction or explain what I'm doing wrong?
In your while loop you can simply do this :-
$i=0;
$animals=array();
$animals[$i]=$row["id"]; //puts id in array
And then you can create a random number by "rand()" between the length of 0-$i
and can get the job done.

How can I make a session of a certain result?

I am stuck. I want to make a session of the ID that belongs to a certain link. However, the code results into multiple ID's. And multiple links with different ID's coming from one code.
This is what I have:
$sql = "SELECT * FROM `lijsten` ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<a href='lijst.php'> Naam: " . $row["naamlijst"]. " - taal: " . $row["taal"]. "</a> <br>";
}
} else {
echo "0 results";
}
$conn->close();
?
The statement gives links for all the results of the rows naamlijst and taal. But I only want to make a session of ID of the link that I click.
This is my first question on Stackoverflow. I hope someone can help me! :)
Picture of webpage
Here is the webpage, I want to store the ID of the link that I click into sessions

Adding PHP table to mySQL files

hey guys this maybe a stupid question for you but I have a database containing footballers names (please see screen shot) What i need to do is display maybe 5 names side by side and then 5 below, if that make sense. is there an easy way to do this?
the code i currently have after the established connect is
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
echo "<div class='caption'><h3><img src='http://placehold.it/180x180' alt=''>" . $row['NAME'] . "</h3><p>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
is it CSS i should be focusing on to make this look the way i want it?
sorry i am a new at this and cant get my head around it lol
try this:
I have added a counter called i which will change the div after it has listed 5 names. Then I have styled the div to 50% width and float to left which allows 2 div side by side.
<style>
.caption
{
width:50%;
float:left;
}
<?php
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='caption'>";
while($row = $result->fetch_assoc()) {
$i = 0;
if($i>=4)
{
$i = 0;
echo "</div><div class='caption'>";
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
<h3><img src='http://placehold.it/180x180' alt=''><h3>" . $row['NAME'] . "<h3>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
</div>
yes, you are doing it right way. You just need to do some css. You can take some help form here :- https://designshack.net/articles/css/5-simple-and-practical-css-list-styles-you-can-copy-and-paste/

How to give a unique url/id to a question posted by the user?

There's a form called discussion.php, in which a user will fill out his/her question/discussion and post it to savedisc.php. Some of the savedisc.php looks like this:
$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];
//connect to database
//save the content of discussion/question into the database for future use
$sql="INSERT INTO Discussion (Message, Title, Type)
VALUES
('$message','$title','$represents')";
//Display user's question/discussion again
echo $message . "<br />";
echo $title . "<br />";
echo $represents . "<br />";
It is not shown above, but I am saving the id field manually, i.e. via phpmyadmin as a auto increment and primary key of course. Therefore, all of the values in the table Discussion will have their own unique id. Once the question/discussion is saved, I want to be able to display $title of each question on wb.php as a link, which as of now looks like this(some code from wb.php):
$result = mysql_query("SELECT * FROM Discussion ORDER BY id DESC");
//When user clicks the question/discussion Title, he/she will be directed to wbcomm.php
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php' >{$row['Title']}</a><br />";
}
Until here, everything is working smooth. However, from here on, what I'm trying to do is, when the user clicks the question/discussion title via above code, I want him/her to be directed to wbcomm.php?id=1, where id=1 represents the unique id of the question/discussion. Some of the code from wbcomm.php is below:
if (isset($_GET['id']))
{
//connect to db
$wbid = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM Discussion WHERE id = '$wbid' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows() > 0) {
$discussion = mysql_fetch_object($res);
//display member's question here:
echo $discussion['id'] . "<br />";
echo $discussion['Title'] . "<br />";
echo $discussion['Type'] . "<br />";
echo $discussion['Message'] . "<br />";
}
else {
// discussion does not exist with ID
}
}
However, for some reason, the result is blank. I.e. the question/discussion doesn't even show up. What am I doing wrong? Is my procedure even correct?
Thank you.
In your wb.php, you create a link to wbcomm.php but you are not passing the ID of the discussion, so your $wbid will be empty. You need to pass the ID along with the link, like this:
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php?id={$row['id']}' >{$row['Title']}</a><br />";
}
Your ID column is an autoincrement int type so you do not need to put it in quotes or escape it. You should definitely test it to see if it's numeric, though.
Use this SQL mysql_num_rows($res) > 0

Categories