HTML in PHP - displaying variables from MYSQL table - php

I have a MYSQL table containing tournament data for players. Each player has stats like games_won, games_lost, etc. I am trying to display this information in a table.
My PHP code looks like this:
//Connecting to $db
//...
//Store table data into array
$result = mysqli_query($db, "SELECT * FROM ladder");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " " . <a href=$row['link']$row['username']</a> . " " . $row['tourney_wins'] . " " . $row['game_wins'] . " " . $row['game_losses'] . " " . $row['last_played'];
echo "<br>";
I get an error (Unexpected "<") on
<a href=$row['link']$row['username']</a>
I am trying to display each username in the table as a hyperlink to their respective profile on another site. Can anyone give me a correction? Thanks!

This is how it should be quoted (assuming I interpreted how you wanted the link properly):
//Connecting to $db
//...
//Store table data into array
$result = mysqli_query($db, "SELECT * FROM ladder");
while($row = mysqli_fetch_array($result))
{
echo( $row['id']." <a href='".$row['link']."'>".$row['username']."</a> ".$row['tourney_wins']." ".$row['game_wins']." ".$row['game_losses']." ".$row['last_played'] );
echo("<br>");
}

Change
<a href=$row['link']$row['username']</a>
to
"<a href=$row['link']$row['username']</a>"

You forgot to quote your html:
echo $row['id'] . " " . <a href=$row['link']$row['username']</a> . " " ...snip...
^--here ^--here
Since it's not quoted, PHP is interpreting it <a as concatenate less than undefined constant 'a', which makes no sense.

Related

How Do I Give Each Row In MySQL Delete Button With PHP?

I need each row to have a unique button beside it which will delete the message. I need it to only be able to be deleted by the receiver. My first thought was to echo a HTML form inside the while loop. I don't think that would work though. If it does work what would the SQL statement inside if(isset()) look like? Each message has an ID so could I use that? If you need to see rest of the code let me know.
$Select = "
SELECT * FROM Messages
WHERE Receiver='$Identifier'
ORDER BY ID
DESC
LIMIT 10";
$Result = $Connect->query($Select);
if (mysqli_num_rows($Result) > 0) {
while ($Row = mysqli_fetch_assoc($Result)) {
echo nl2br("Sender = " . $Row["Sender"] . " Message = " . $Row["Message"] . "\n");
}
}
why don't try this:
echo "Sender = " . $Row["Sender"] . " Message = " . $Row["Message"] . "<button id=".$Row["ID"].">Delete</button>";

Some html tags' parameters messup html/php code

When I create/edit post to my website, and when I use html tags (either type it by my self or using WYSIWYG editor (in this case nicEdit, but I've used different one - same problem)) it does not show anything on the page.
to be more specific: I have article container which imports php code, which than iterates through the db table and echo posts. This is the code:
<h3>Wydarzenia</h3>
<?php
//collects data from table "events" and sort it by "id" date in descending oreder
$event_table_data = mysqli_query($db_con, "SELECT * FROM events ORDER BY id DESC")
or die(mysqli_error());
//puts the data from "events" into an array
$event_content_array = mysqli_fetch_array($event_table_data);
//makes variables from table columns
$id = id;
$title = title;
$happeninng_date = happen;
$created_date = created;
$content = content;
$author = author;
//add new post button
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<a class='e_event_addpost_link' href='./index.php?link=add_post'><button class='e_event_addpost_button' type='button'>Dodaj nowy post</button></a>";
}
do{
//echo each event for main event article
echo "<span class='b_events_span' id='" . $event_content_array[$id] . "'>";
echo " <a class='b_events_anchor' id='" . $event_content_array[$id] . "'></a>";
echo " <h2 class='b_events_title'>" . $event_content_array[$title] . "</h2>";
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<form class='b_events_edit_form' name='edit_post' action='./index.php?link=edit_post' method='post'>";
echo " <input type='hidden' name='id' value='" . $event_content_array[$id] . "'> ";
echo " <input class='e_events_edit_submit' type='submit' value='Edytuj wydarzenie'>";
echo "</form>";
}
echo " <div class='fb-share-button' data-href='http://www.zhppgkaberdeen.co.uk/index.php?link=events#" . $event_content_array[$id] . "'></div>";
echo " <p class='b_events_happening_date'><i>Data wydarzenia: " . $event_content_array[$happeninng_date] . "</i></p>";
echo " <p class='b_events_content'>" . $event_content_array[$content] . "</p>";
echo " <p class='b_events_created_date'>Utworzono: " . $event_content_array[$created_date] . "</p>";
echo " <p class='b_events_author'><b>Czuwaj!</b><br/>" . $event_content_array[$author] . "</p>";
echo "</span>";
}while($event_content_array = mysqli_fetch_array( $event_table_data ));
?>
When I add tags like <img> for instance with alt="" parameter it completely ruins web page. What I mean by that is that the article container is not shown at all (I've checked that with firebug) as well as everything after the article tag (like footer for instance). What is even more strange is that this: alt="text" did not break the page but every other random word does, how ever it does not show the "text" at image at all.
Similar thing happen when I tried to create table within the post and title or target parameter, I cant remember which one.
I have tried to use htmlentities($event_content_array[$content]), htmlspecialchar($event_content_array[$content]) and mysqli_real_escape_string($do_con, $event_content_array[$content]) -> non of this helped.
Is there a way to fix that or I just need to give up using this tags/parameters?
Thank you for any help or explanation
if you are using e.g echo "<img alt="text">";try to escape the " such as alt=\"text\"

Deleting an item from a mysql cart with php

I'm creating a cart system and trying to find a way to have a simple button that once pressed deletes the corresponding row. However, I cannot seem to find a way to dynamically do this with while loop I currently have.
<?php
//connect to DB.
$con = mysqli_connect("localhost", "root", "", "books");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySql: ". mysqli_connect_error();
}
$query = "SELECT * FROM cart WHERE customerID = '$_SESSION['id']'";
$result = mysqli_query($con, $query);
//creates a table for dumping the DB to, loops through the DB and posts the contents elegantly.
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td></tr>";
$totalprice += $row['bookPrice'];
}
echo "</table>";
echo "The total present price is: $".$totalprice;
//closes the conncection to the DB
mysqli_close($con);
?>
I've considered trying to put an echo query statement into the database and adding "$row['deletebutton']" to the while loop but I'm not sure that would necessarily work.
The easy way is to create a new page and send the message to this page to delete the item.
So, in the table you add a new column with a link
delete
And in the page you treat this value.
To expand on the comment posted to the question, you could add this to your row:
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td><td><input type='checkbox' name='remove[]' value='" . $row['ROW_ID'] . "' /></td></tr>";
And then with the data submitted in the form you could do something like this:
$query = "DELETE FROM cart WHERE ROW_ID IN (" . implode(',',$_POST['remove']) . ")";
Keep in mind to check the value of remove before using it in queries.

php echo no output to display very puzzled, why?

I am getting no where with this, I am not getting any output from my echo ,can someone help, thanks in advance...singhy
code below...
$strSQL = "SELECT * FROM <tablename> WHERE id='" . $_GET["serviceName"] . "'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)){
echo "<dt>Name:</dt><dd>" . $row["serviceType"] . " " . $row["serviceName"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>";
}
// Close the database connection
mysql_close();
?>
<p>Return to the list</p>
</body>
</html>
can someone tell me where i am going wrong, ive tried various options, thanks in advance, singhy
Try this debugging code:
$serviceName = mysql_real_escape_string($_GET['serviceName']); // Read PS note at the end
$strSQL = "SELECT * FROM `tablename` WHERE id='$serviceName'";
$rs = mysql_query($strSQL) or die(mysql_error()); // Display any query error
echo "Total number of rows: ". mysql_num_rows($rs); // Echo number of rows
while($row = mysql_fetch_assoc($rs)){
echo "<dt>Name:</dt><dd>" . $row["serviceType"] . " " . $row["serviceName"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>";
}
Please note
You should escape the $_GET request and never use it directly in a query statement. Use mysql_real_escape_string() for that. (This method will be deprecated, read next bullet)
many of the functions you are using will be deprecated starting php 5.5.0 Alternatively you can use PDO prepared statements
replace
$_GET["serviceName"]
with this
$_GET['serviceName']
use single quotes in $_GET in your case.

simple calculation of field for php mysql field

i have simple db that it is filled by a form from a page, all is ok in the entry.
i have 2 fields in the database are: all_students and current_students filled by the user in the form that i want to calculate
the trick is that i am echoing only the latest db record in the output page.. to give me only the latest data inserted in the form...
now, i want to create a new field that give me the absent students automatically (all - current)
what i have tried, i read that i can NOT create a new calculated field in the db, it is not an excel, so i am trying to echo the calculation results of these 2 fields, to a new value that is the "absent students"
what you suggest? please help, here is my code
<?php
$con=mysqli_connect("localhost","root","PasswordHere","DBnameHere");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT id, class, all_students, current_students FROM students ORDER BY id DESC LIMIT 1");
while($row = mysqli_fetch_array($result))
{
echo "Class: " . $row['class'] . "<br>";
echo "All students: " . $row['all_studnets'] . "<br>";
echo "Current students: " . $row['current_studnets'] . "<br>";
echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . " <br>";
}
mysqli_close($con);
?>
You only put dot (.) if you want to concatenate a string. Please remove the dot since you are subtracting.
echo "Absent students: ";
echo floatval($row['all_studnets']) - floatval($row['current_studnets']);
echo "<br>";
Try this line
echo "Absent students: " . $row['all_studnets'] - $row['current_studnets'] . " <br>";
Insted of this
echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . " <br>";

Categories