Adding to a session array - php

<?php
session_start();
include("#nav.php");
include("dbconnectie.php");
echo "Plaatsingen: ";
$query = $db->prepare("SELECT * FROM shop");
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
$img = $data['img_url'];
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td> Condition: " . $data["cond"] . "/100 </td>";
echo "<td> Prijs: &dollar; " . number_format($data["price"],2,",",".") . "</td>";
echo "<td> <img src='$img' width='400' height='300' ></img> </td>";
echo "<td> Plaatsing nummer: " . $data['id_img'] . "</td>";
echo "</tr>";
echo "<br>";
}
echo "</table>";
if(isset($_POST['atc']))
{
if($_SESSION['on']){
$myarray = array('0');
$addtoarray = $_GET['id'];
array_push($myarray, $addtoarray);
$_SESSION['cart'] = $myarray;
echo "Toegevoogd aan uw winkelmandje.";
var_dump($_SESSION['cart']);
}else
{
echo "Log eerst in!";
}
}
?>
<html>
<title>Just for kicks</title>
<header>
</header>
<body>
<form method='post' action=''>
Plaatsing nummer invoeren:
<input type='number' name ='id' value ='id'><br>
<button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button><br><br>
</form>
</body>
</html>
Between line 25 and 31 I'm trying to add numbers to the session array but I'm not sure how, because this is clearly not working. It doesn't add the number you fill in, at the form part. But it appears it doesn't do anything.

I trimmed your code down to just the bare minimum and added some inline comments. I made the assumption that the contents of $_SESSION['cart'] is an array.
Please notice:
POST was used consistently
The value of the input was removed (value ='id')
The check for $_SESSION['on'] was removed
Code
<?php
// Start your session.
session_start();
// The form was submitted.
if (isset($_POST['atc'])) {
// Get the cart so we can append to it.
// Assuming that the cart is an array.
$cart = (array)$_SESSION['cart'];
// Append the user's input to the end of the cart.
$cart[] = $_POST['id'];
// Store it in the session.
$_SESSION['cart'] = $cart;
// Dump out the session.
var_dump($_SESSION);
}
?>
<html>
<title>Just for kicks</title>
<header>
</header>
<body>
<form method='post'>
<label> Plaatsing nummer invoeren:
<input type='number' name='id'/>
</label><br>
<button type="submit" class="submit" name="atc" value="atc">Winkelmadje</button>
<br><br>
</form>
</body>
</html>

Related

In a php session

}
put returns between paragraphs
for linebreak add 2 spaces at end
italic or bold
indent code by 4 spaces
backtick escapes like _so_
quote by placing > at start of line
to make links (use https whenever possible)
You could have another session that stores the answers as an array, and add to it after each successful post
Something like this could work:
<?php
$totalQuestions = count($ques);
$_SESSION['answers'] = $_SESSION['answers'] ?? [];
// Get current question, default to 1
$currentQuestion = count($_SESSION['answers']) == $totalQuestions ?
$totalQuestions :
$_SESSION['answers'] + 1;
?>
<div class='questionHeader'>
<label>Question <?php echo $currentQuestion ?> of <?php echo $totalQuestions ?></label>
</div>
<br>
<div class='question'>
<?php echo $ques[$currentQuestion-1] ?>
</div>
Answer: <input type='text' id='answerOneSub' name='answerOneSub'>
<button type='submit' value='submit' name='submit'>Submit!</button>
<?php
if (isset($_POST['submit'])) {
$_SESSION['answers'][] = $_POST['answerOneSub'];
echo "<br>" . $_SESSION['answerOneSub'];
}
?>
You can use query param like this:
<?php
$page = 1;
if (isset($_GET["page"])) {
$page = (int)$_GET["page"];
}
$url = strtok($_SERVER["REQUEST_URI"], '?');
echo "<form method='post' action='" . $url . "?page=" . ($page + 1) . "'>";
echo "<div class='questionHeader'><label>Question [$page] of 6</label></div>";
echo "<br>";
echo "<div class='question'>" . $ques[$page - 1] . "</div>";
echo "<br>";
echo "Answer: ";
echo "<input type='text' id='answerOneSub' name='answerOneSub'>";
echo "<button type='submit' value='submit' name='submit'>Submit!</button>";
echo "</form>";
if (isset($_POST['submit'])) {
$_SESSION['answerOneSub'] = $_POST['answerOneSub'];
echo "<br>" . $_SESSION['answerOneSub'];
}

I have 3 users with different id in the database and it keeps getting the last user id that been loop when I accept or reject?

There are 3 id that been view from this table
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo "<table>";
echo "<tr>";
echo "<td colspan='2'> <strong>Name: </strong>" . $row['first_name'] . " " . $row['middle_name'] . " " . $row['last_name'] . "</td>";
echo "<td><strong>You're request is: </strong>" . $row['event'] . "</td>";
echo "</tr>";
echo "<tr><td colspan='3'> <strong>Address: </strong>" . $row['address'] . " </td></tr>";
echo "<tr><td colspan='3'> <strong>Office to go: </strong>" . $row['office'] . " </td></tr>";
echo "<tr>";
echo "<td> <strong>Contact#: </strong>" . $row['phone'] . "</td>";
echo "<td> <strong>Request made from: </strong>" . $row['curdate'] . "</td>";
echo "<td> <strong>Time request: </strong>" . $row['time'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong><i>Message: </i></strong><br>". $row['message'] . "</td>";
echo "</tr>";
echo "<tr> <td colspan='3'>";
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>";
echo "</form> </center>";
echo "</td></tr>";
echo "</table>";
echo 'Either I choose one of the users, it still getting the user id that been loop last';
if(isset($_GET['approveSubmit'])){
isset($_GET['userDate']);
$date = $_GET['userDate'];
header("location: ../approve_insert.php?id=$id&date=$date");
}
if(isset($_GET['rejectSubmit'])){
header("location: ../reject_insert.php?id=$id");
}
}
You are not passing the correct $id to your header: Location(...
To solve this you would need to pass the id of the user to the form as well, so this value become available when an user is clicked.
You can do this by adding an extra hidden input to the form you are creating
<input type='hidden' name='id' value='".$id."' />
Also there is no need to place the code that controls the action you want to do inside the loop that creates the table. Just place it above (or below) the code that generates the table
<?php
if(isset($_GET['approveSubmit'])){
$date = $_GET['userDate'];
header('location: ../approve_insert.php?id='.$_GET['id'].'&date='.$date);
exit;
}
if(isset($_GET['rejectSubmit'])){
header('location: ../reject_insert.php?id='.$_GET['id']);
exit;
}
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo '... table start ...';
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>
<input type='hidden' name='id' value='".$id."' />
";
echo "</form> </center>";
echo '... table end ...';
}
Keep in my mind you would still need to sanitize the input of $_GET['id'] and $_GET['userDate'] before using it in your code/queries
My assumption at this point is that multiple users meet the conditions at the end of your loop. If your goal is to redirect to the location specified, from the first header location call, you'd have to prevent the loop from continuing. Typically this would be done with exit().
header("location: ../reject_insert.php?id=$id");
exit();
Also, you're going to get an error that you can't set headers because you've already output body content. The header("location...") can only be called before your echo ... statements.

Cant seem to EDIT/MODIFY my php table by id

<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if(!isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM pleasework ORDER BY ID");
$row = mysql_fetch_array($result);
}
?>
<form action="?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
<img id="close1" src="X.png" width="25" height="25" onclick ="div_hide1()">
<h2><font size="6">Please change existing data</font></h2>
<hr>
<br>
<font color="yellow">Change Name to: </font><input type="text" name="New" value="<?php echo $row['Name'];?>"/><br><br>
<font color="yellow"> Change Cause to: </font> <input type="text" name="New1" value="<?php echo $row['Cause'];?>"/><br><br>
<font color="yellow">Change Symptom to: </font><input type="text" name="New2" value="<?php echo $row['Symptom'];?>"/><br><br>
<font color="yellow"> Change Gene_affected to: </font><input type="text" name="New3"value="<?php echo $row['Gene_affected'];?>" /><br><br>
<input type="hidden" name="id" value="<?php echo $_GET['ID'];?>"/>
<input type="submit" onclick="clicked(event)" />
</form>
<?php
if(isset($_POST['submit'])){
mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[id]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
}
else {}
?>
This is my php file.
while($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . " </TD>";
echo "<TD>" . $row['Cause'] . " </TD>";
echo "<TD>" . $row['Symptom']. " </TD>";
echo "<TD>" . $row['Gene_affected'] . " </TD>";
echo "<TD><font color='red'>Delete row</font> </TD>";
echo "<TD><font color='red'>modify</font> </TD>";
echo "</TR>";
}
And this is the section which has a modify button that links to the edit.php file. The error here is that is doesnt bring over the values in the table to the editing page and then submitting the form doesnt work too. help please
Your code appears a bit confused.
First of all, why to put the modify routine after output the form? Especially since after modify you send the header function, that fails if previously there are some output.
Note also a typo: you forgot to properly open the php tag in the form declaration. Change-it in this way:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
The main problem is that you check if the $_POST[submit] if set, but this is not set, due to the absence of attribute name.
Change it in this way:
<input type="submit" name="submit" onclick="clicked(event)" />
Now your script should work (I don't have tested the sql).
Please also note that your UPDATE routine is redundant: you can reduce the 4 statement to only one in this way:
$result = mysql_query
(
"UPDATE pleasework SET Name='{$_POST[New]}', Cause='{$_POST[New1]}', Symptom='{$_POST[New2]}', Gene_affected='{$_POST[New3]}' WHERE ID={$_POST[id]}"
);
About PHP Original MySQL API:
This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
NOTE: mysql_* deprecated, so try to use PDO or mysqli_*.
Simple way:
<?php
if(isset($_POST['submit'])){
$result = mysql_query("UPDATE pleasework
SET Name='".$_POST['New']."',
Cause='".$_POST['New1']."',
Symptom='".$_POST['New2']."',
Gene_affected='".$_POST['New3']."'
WHERE ID=".$_POST['id'].");
if($result ){
echo "Change Successful<br>" ;
header("Location: databse.php");
}
mysql_close($con);
}
YOUR PHP:
while($row = mysql_fetch_array($result))
{ $spaces = " ";
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . $spaces."</TD>";
echo "<TD>" . $row['Cause'] . $spaces."</TD>";
echo "<TD>" . $row['Symptom']. $spaces."</TD>";
echo "<TD>" . $row['Gene_affected'] . $spaces."</TD>";
echo "<TD><a href='delete.php?id=".$row['ID'] ."'>";
echo "<font color='red'>Delete row</font></a>".$spaces."</TD>";
echo "<TD><a href='edit.php?id=" . $row['ID'] ."'>";
echo "<font color='red'>modify</font></a>".$spaces."</TD>";
echo "</TR>";
}

Tried to replace $_GET with $_POST, but now it won't display or update

I tried to retype a code of my groupmate, because the 'textarea' in here is going to be bombarded by a whole lot of string and the problem is that the request URI is too long. So I tried to change of all the $_GET to $_POST, because it won't be posted in the URL.
But the problem is that it won't display the input 'text' and 'textarea' in the isset. I don't know whether it's the isset that is the problem or the or the $_POST, but when I return it back to $_GET it works.
PHP Code for displaying the chapters to be updated and the update function.
<?php
if (isset($_POST['submit'])) {
$id = $_POST['cid'];
$title = $_POST['ctitle'];
$body = $_POST['cbody'];
$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id'");
}
$result = $db->query("select * from chapter");
while($row = mysqli_fetch_assoc($result)) {
$update = 'chapterid';
echo "<li id='button' class='btn' ><b id='shadow'><a href='chapter1.php?update={$row['chapter_id']}'>{$row['chapter_title']}</b></a></li></button>";
}
?>
PHP code for displaying the input 'text' and 'textarea'.
<?php
if (isset($_POST['update'])) {
$update = $_POST['update'];
$result1 = $db->query("select * from chapter where chapter_id=$update");
while($row1 = mysqli_fetch_assoc($result1)) {
echo "<center>";
echo "<form class='form' method='POST'>";
echo "<h2>Update Form</h2>";
echo "<hr/>";
echo"<input class='input' type='hidden' name='cid' value='{$row1['chapter_id']}'/>";
echo "<br />";
echo "<label>" . "Chapter Title:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='ctitle' value='{$row1['chapter_title']}' />";
echo "<br />";
echo "<label>" . "Chapter Body:" . "</label>" . "<br />";
echo "<textarea rows='15' cols='95' name='cbody'>{$row1['chapter_body']}";
echo "</textarea>";
echo "<br />";
echo "<input class='submit' type='submit' name='submit' value='update' />";
echo "</form>";
echo "</center>";
}
}
if (isset($_POST['submit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
?>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div><?php
mysqli_close($db);
?>
if (isset($_POST['update'])) {
This line should stay $_GET['update'], because "update" is not part of you form, but part of the url:
<a href='chapter1.php?update={$row['chapter_id']}'

get a variable row from a while mysql_fetch array that isnt the last result

Hey guys I'm having a hard time sending a session variable of the $row['projectnaam']
that belongs to the one that just has been clicked.
At the moment the Session always takes the last $row['projectnaam']
from the while loop and I'm wondering how I can send the right
variable with a session that belongs to the row that just has been clicked.
Thank you in advance.
Here's my syntax:
<?php
include "config.php"
$bedrijfsnaam = $_SESSION['gebruikerbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam='$bedrijfsnaam' ")or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (isset($_POST['submit'])){
$_SESSION['projectnaam'] = $projectnaam;
header('Location: viewprojectsbedrijf.php');
}
echo "<div class='project'>";
echo "<div class='projectdetails'>";
echo "<p class='projectnaam'>";
echo $row['projectnaam'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='datum'>";
echo $row['datum'];
echo "|";
echo $row['Tijd'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='bedrijfsnaam'>";
echo $row['bedrijfsnaam'];
echo "</p>";
echo "</div>";
echo "<div class='view'>";
echo "<form method=\"POST\" action=\"\">";
echo "<input type='submit' value='View' name='submit' class='viewbutton'></input>";
echo "</form>";
echo "</div>";
}
?>
Mede Nederlander ;)
I think you mean to do something like this:
include("config.php");
if(isset($_POST['submit'])){
$_SESSION['projectnaam'] = $_POST['projectnaam'];
header('Location: viewprojectsbedrijf.php');
}
$bedrijfsnaam = $_SESSION['gebruikersbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam=".$bedrijfsnaam)or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<div class='project'>
<div class='projectdetails'>
<p class='projectnaam'>
<form method=post action=''>
<input type=text value=".$row['projectnaam']." name=projectnaam>
</p>
</div>
</div>
<input type='submit' value='submit' name='submit'>
</form>";
}
In your own script only your submit button is in the form, so that will be the only value posted. In that case you won't get the POST value of 'projectnaam'.
Also, you try to make a SESSION value of a variable which isn't declared in your posted code. In my code it makes a Session value of the POSTED code.

Categories