Session Value always equals last instance - php

I have a page that is running an SQL query. I am displaying information for each row that the query results in. I am now trying to implement a way to update the information for the things being displayed.
My understanding is that in order to get information from one page to another you need to use sessions.
My code is displaying the information from the MySQL tables, then underneath it is giving the user the choice to edit the information in a form then send it to another file

One way of easily doing this is to use <input type="hidden"> so that you can include $row['Toy_ID'] in your form.
Something like this:
$row = $result->fetch_assoc();
while ($row){
echo "Toy Name: " . $row['Toy_Name'] . "<br>" .
"Store Name: . $row['Store_Name'] . "<br>" .
"Cost: " . $row['Cost'] . "";
echo "<form action='update.php' method='post'>" .
"<input type='hidden' name='toyid' value='".$row['Toy_ID']."'>" . // here's the hidden input, which you can call by using `$_POST['toyid']`
"<label>Toy Name: </label><input name='tname'; value='" . $row['Toy_Name'] . "'><br>" .
"<label>Store Name: </label><input name='storename'; value='" . $row['Store_Name'] . "'><br>" .
"<label>Cost: </label><input name='cost'; value='" . $row['Cost'] . "'><br>" .
"<input type='submit' value='Submit'>" .
"</form></div><br><br>";
$row = $result->fetch_assoc();
}
Then change your query to make use of $_POST['toyid'] instead of $_SESSION['toyid']

Related

Save multiple values from checkboxes in php

I am very new to php and am teaching it to myself, so please keep that in mind.
I am working on a project that presents users with a list of items, from a database, with checkboxes, and allows the user to check them. I want to save the values of the checked fields. This is the line of code that prints all of the options. It prints a course code and course title, with a checkbox.
echo "<input type='checkbox' name ='boxes'>" . $row['course'] . ' ' . $row['title'] . "<br>";
However, when I try to print the values selected, it doesn't work. I get an error that says invalid argument supplied foreach()
if(isset($_POST['submit_courses'])){
if(!empty($_POST['boxes'])){
foreach($_POST['boxes'] as $selected ){
echo $selected."</br>";
}
}
}
Please help!
Try:
echo "<input type='checkbox' name ='boxes[]'>" . $row['course'] . ' ' . $row['title'] . "<br>";
EDIT:
To better answer the question:
You need to add the value="" attribute to your input fields. So if you have an ID in your $row array then it would be like this...
echo "<input type='checkbox' name ='boxes[]' value=' . $row['id'] . '>" . $row['course'] . ' ' . $row['title'] . "<br>";
Now you should be able to get the selected id's from the populated values inside of the $_POST['boxes'] array. Just loop through them and do something with them like echo them out.
foreach ($_POST['boxes'] as $box_value) {
echo $box_value . "<br>";
}
Only the boxes that are selected will be in the array and you will have the identifier for whatever courses were selected.

mysqli_fetch_assoc while loop not working

* if(!empty) retrieves first entry in array and shouldn't be used *
This script searches a database for a url that matches the one submitted by a user in a form. Currently the $query printed by the echo statement works fine in mysql and returns one entry. This script executes and prints the table header, but doesn't enter the while($row =.. ) loop.
Any thoughts or suggestions would be really appreciated. I've used this method before with no trouble so I'm sort of stumped right now.
//1. Query DB for results
$query = "SELECT * FROM projects WHERE projectsurl='".$url."';";
echo "<br>".$query;
$projects = mysqli_query($conn, $query);
// 2. Print Project Results
if(!empty(mysqli_fetch_assoc($projects))){
//Print Project Results
echo "Is this your project?<br>";
echo "<table style=width:'100%'>";
while ($row = mysqli_fetch_assoc($projects)){
echo $tabler . $thh . "Title: <br>" .$row['title'] . $thsp . "No. Rewards: <br>" . $row['rewards'] . $thf . $xtabler;
echo $tabler . $thh . "ID: " .$row['id'] . $thf . $xtabler;
// Echo two rows for the URL Strings because they are longer.
echo $tabler . $thh . "<a href='" . $row['projectsurl'] . "'>Projects</a>" . $thsp . "<a href='" . $row['rewardsurl'] . "'> Rewards " . $thf . $xtabler;
echo "<form id='confirmation' action='../index.php' method='POST'>
<input type='hidden' value='2' name = 'stage'>
<input type='hidden' value='".$row['id']."' name='id'>
<input type='hidden' value='".$row['title']."' name='title'>
<input type='submit' value='Confirm'></form>";
}
echo "</table>";
}else{
//trigger ruby script to search for lost file
echo "Project Not Found. <br>";
}
Oh and the random table stuff is defined elsewhere as
$tabler = "<tr>";
$xtabler = "</tr>";
$thh = "<th><b>";
$thsp = "</th><th>";
$thf = "</b></th>";
$csp = "</td><td>";
$ch = "<td>";
$cf = "</td>";
If there's only one row, then if(!empty(mysqli_fetch_assoc($projects))){ will fetch it and your while loop will not be entered. Instead, to check if a row exists, use mysqli_num_rows($projects).
Also, Reminder, escape your user submitted data before using it in your MySQL query if you haven't already. This can be done by: $query = "SELECT * FROM projects WHERE projectsurl='".mysqli_real_escape_string($conn, $url)."';";
EDIT: Alternatively, prepare your MySQL statements before executing them. This is the preferred method, although both protect you from injections.

How do I correctly echo checkboxes in a PHP table?

I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)
$sql = "SELECT * FROM products";
$answer = mysqli_query($connection, $sql);
if ($answer->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Stock</th><th>Delete Product</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
echo "0 results";
}
Insert the checkbox in an additional table cell (td).
A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
. "</td></tr>";

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\"

Passing parameter to php to get specific information

i have this quick issue please.
I have this code here which permits me to extract a user name and a photo, and when the name is clicked it takes me to this hostess.php file, well, i need to pass the id variable to the hostess.php and save it, in order to get information only for that id..
Here is the code:
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
}
How can i just get the id and then how can i save it to the $id variable
Thanks
The table structure is like this:
The table name is called hostess and the field i need to retrieve from hostess is the [id]
Change the line:
echo '<td><a href="hostess.php">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
to this:
echo '<td><a href="hostess.php?id={$row[id]}">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
and in hostess.php, use this to extract the value:
$id = $_GET['id'];
EDIT
Here is another method to pass variable as POST. Again, change the aforementioned line to:
echo '<td><form method="POST" action="hostess.php">' . "<input type='hidden'
name='id' value='{$row[id]}' />" . "<input type='submit' value='" .
$row['first_name_en'] . " " . $row['family_name_en'] .
" /></form></td>";
You can use CSS to style that button as simple text too. And the value can be retrieved in hostess.php as follows:
$id = $_POST['id'];
Add/change this in your code:
$personID = $row['id']; #or whatever the field name is
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
The addition of "?personID='.$personID will send the value in the GET statement, or within the URL.
On the receiving page get the value that way:
$personID = $_GET['personID']+0; #add zero to force a numeric value--be sure to scrub your data!
#be sure to do other validation if needed!

Categories