So i am trying to display the id inside the adressbar. On previous pages it already works but in this one it doesn't seem to work and i dont know why. The arrows shows where the problem lies
My disired result is that this adress bar https://nottherealsiteurl/upload.php?id=
to turn into this https://nottherealsiteurl/upload.php?id= $row['id'](id from database)
<?php
$sql = "SELECT chauffeurs_naam, ritten.id ,chauffeurs.cc, ritten_date, ritten_totaal, ritten_naam, ritten_start, ritten_end, ritten_pauze, Kenteken, km_end, Onderhoudsrit
FROM ritten
JOIN chauffeurs
ON ritten.rit_cc = chauffeurs.cc
JOIN users
ON users.cc = chauffeurs.cc
WHERE users.id =".$_GET['id']. "";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
?>
<form action="upload.php?id=<?php echo $row['id'];?>" method="post" enctype="multipart/form-data">
<h3>Select image to upload:</h3>
<input class="button button2" type="file" name="fileToUpload" id="fileToUpload" required="required">
<input class="button button2" type="submit" value="Upload Image" name="submit">
</form>
On other pages where its does get displayed.
its put like this.
Again i put the arrows where it does work
$sql = "SELECT chauffeurs_naam, c.id, c.cc, c.chauffeurs_foto FROM chauffeurs c JOIN users u ON c.cc=u.cc WHERE u.id =".$_GET['id']."";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"<div class='col-xl-3 col-md-6 card'>";
if (isset($row['chauffeurs_foto'])) {
echo "<div class='caption'><img class='avatar-cards' alt='Generic placeholder thumbnail' src=''/></div>";
}
else {
echo "<img class='avatar-cards' alt='Generic placeholder thumbnail' src='images/Test_Foto_Chauffeur.png'/>";
}
echo "<a href='Update_image.php?id=". $row['id'] ."'>Aanpassen</a>";
echo "<div class='card-body'>";
echo "<h4>". $row['chauffeurs_naam'] ."</h4>";
echo"<p class='card-text'>";
echo "Chauffeurs-nummer: ". -------------------->$row['id']<--------------------------- ."<br/>";
echo "</p>";
echo "</div>";
echo "</div>";
}
on this line upload.php?id= you missing echo and semi colon
upload.php?id=<?php echo $row['id'];?>
try this hope it work.
and i have run your code and cross checked its working now , once just check with simple query and if worked cross check your query that its getting data or not
<?php
$sql = "SELECT * from tab_1";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
?>
<html>
<head>
<link rel="stylesheet" href="styling.css">
<title>Image Upload Tutorial</title>
</head>
<body>
<center>
<h1>Php Photo Upload Tutorial</h1>
<form action="upload.php?id=<?php echo $row['id'];?>" method="post"
enctype="multipart/form-data">
<h3>Select image to upload:</h3>
<input class="button button2" type="file" name="fileToUpload"
id="fileToUpload" required="required">
<input class="button button2" type="submit" value="Upload Image"
name="submit">
</form>
</center>
</body>
</html>
above complete code working fine
Related
I have a form that is being created dynamically
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{ ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<?}?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
</form>
But i a not able to understand how to carry the value of input to the addtable.php page
Can anyone please tell how to submit these values from this form
In your addtable.php, you need to print_r $_POST and see what inputs you're getting.
// addtable.php
echo "<pre>";
print_r($_POST);
print_r($_FILES); // If you're expecting a file input
Also, you should modify your HTML a little:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_assoc($result)) { ?>
<input type="text" placeholder="<? echo $row['heading']?>" name="<? echo $row['heading']?>" />
<? } ?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button> <!--Place submit button outside loop -->
<?php } ?>
I think there is issue in submit button, you looping submit button with same name, try to put submit button out of while loop.
One of the reasons your PHP is not functioning correctly is because your PHP block, is not placed inside of PHP tags.
Your code should look like this:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php // added the PHP tags
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
?> // escaped the php tag
<input type="text" placeholder="<?= $row['heading']; ?>" name="<?= $row['heading']; ?>" />
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
}
?>
Notice how I also escaped PHP when I wanted to display any HTML. Of course, you could simply echo these out, but you can do this too! Personal preference, really.
Another little alteration I made was, rather than using <?php echo ... ?> you can simply use <?= $row['...']; ?>. It is faster and cleaner too!
In your addtable.php:
<?php
$inputValue = $_POST['input_name'];
//the $inputValue is the input value that you posted from form.
echo $inputValue;
?>
To receive data in addtable.php you have to get them by name property of inputs in the form. So you have to define the names of inputs or to retrieve them again from db in addtable.php
try this ,
addtable.php
<?php
foreach ($_POST as $key => $value){
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
?>
i hope it will be helpful.
You can create array of heading and access it in php:
<form action="addtable.php" method="post" enctype="multipart/form-data" >
<?php
$sql="SELECT * FROM `tracker_item` ";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<input type="text" placeholder="<?php echo $row['heading']; ?>" name="heading[]"> />
<?
}
?>
<button type="submit" name="add" alt="Add" value="Add" class="btn blue">Add</button>
<?php
}
?>
</form>
In addtable.php:
$headingArray = $_POST['heading']; // here you will get all the heading in array format
Hello monsters of programming. I just want to ask a question about using $_SESSION and $_GET. When to use $_GET and $_SESSION? what is the best for passing variable? Im just new to php and html and i don't know what is the best practice. Can someone help me to understand both of them?
Here is the example of my code. I used $_SESSION for passing the variable $newsid;
here is the edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type='hidden' name="news_id" value="<?php echo $newsid;?>">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
header('Location: edit2.php');
}
?>
here is the edit2.php
<?php
session_start();
$id = $_SESSION['news_id'];
include_once('connection.php');
$sql = "SELECT * FROM news WHERE news_id = '$id'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $title;?>"/><br>
Date<input type ="text" name="date" value="<?php echo $date;?>" /><br>
Content<textarea name="content"><?php echo $content;?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $newsimage;?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
$_GET is for parameters that are needed during that specific request (or can be easily carried over to other pages), e.g.:
item IDs
current page (pagination)
user's profile name
...
$_SESSION is for data that needs to be persisted across multiple requests, e.g.:
current user's ID
shopping carts
list filters
...
You should use the one that better suits your use case.
That being said, I'd consider storing news_id in the session a bad thing. What if I want to edit multiple items and open multiple browser tabs? I'll end up overwriting my data. Just because you can use sessions doesn't mean you should.
this is what i want to do, i have two pages, first is the "edit.php" and second is the "edit2.php". the page "edit.php" this is the page where all the news is shown and you will select what news you will edit by clicking the "edit button" and the "edit2.php" where will i exactly edit the news. i want to pass the value of the selected news in another page. But here is the problem, when i clicked the "Sample news 1" edit button the data showing in another page is the "Sample news 2". Even when i clicked the "Sample news 2" edit button, the data is also the "Sample news 2". Can someone give me ideas on how to fix this?
here is the picture of edit.php. I click the edit button of "Sample news 1".
here is the picture of edit2.php. and this is the output data. The data should be "Sample news 1" not "Samplel news 2".
here is my php code in edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $newsid;
$_SESSION['n_title'] = $title;
$_SESSION['n_date'] = $date;
$_SESSION['n_content'] = $content;
$_SESSION['n_image'] = $newsimage;
header('Location: edit2.php');
}
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
here is my php code for "edit2.php"
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $_SESSION['n_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $_SESSION['n_date']; ?>" /><br>
Content<textarea name="content"><?php echo $_SESSION['n_content']; ?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $_SESSION['n_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Remove if(isset($_POST['esubmit'])){ code out of while loop and add values in form as I shown in below example
Pass $newsid in edit form as hidden and retrieve the content based on new
<form action="" method="post">
<input type='hidden' value="<?php echo $newsid;?>" name="news_id">
<input type="submit" name="esubmit" value="edit" />
</form>
and in your php add this line.
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
The problem is, in each iteration of while loop you're overwriting $newsid, $title, $date,... variables. So when you submit the form, the last row's data will get stored in the corresponding $_SESSION variables.
So here's the solution to your problem.
You don't need $_SESSION to pass the form values to edit2.php page, instead change the <form> element in the following way,
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
On edit2.php page, first catch the news_id value using $_GET superglobal , like this:
$newsid = $_GET['news_id'];
And then get the appropriate news details using this $newsid, and finally populate the form.
Here's the complete code,
edit.php
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
edit2.php
<?php
if(isset($_POST['esubmit'])){
$newsid = $_GET['news_id'];
include_once('connection.php');
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}
}
if(isset($_POST['submit'])){
// Write code to commit the edit details
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['esubmit'])){
?>
<form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="content"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="submit" value="Update" />
</form>
<?php
}
?>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Here is the issue, You are looping your data to the session, usually single session keeps only a value. Since that final item of your loop will store on the session [in this case it is 'Sample news 2']. I believe, you can place it on a hidden field & post it to the next page or you can use URL Parameter [GET], to pass the Id to the next page.
Ex : <a href ='edit2.php?newsId ='<?php echo $newsid?>> Edit </a>
what I think is happening is when you are clicking edit button this edit.php page is again reloading and that sql query is running again that's why it is taking value of first image in both cases.what I suggest you to try is use $_get[] instead of $_SESSION and $_POST and pass value of image id directly to page edit2.php and query others details from database in that page and then display it.try it
Remove <form> and <input> use just <a> instead like this.
<a href='edit2.php?id=$news_id'>Edit</a>
and then fetch this id in edit2.php like following
$news_id=$_GET['id'];
and atlast fetch other details of news from the database using this id like'
$query='select * from news where news_id=$news_id';
It's all going wrong. I need to output a form onto my website that will do 1 of 2 things:
If the user already has content in the database, provide a form that posts to self to update the existing content.
If the user does not have content in the database, provide a form to let the user add information to the database.
The forms should submit to themselves to keep coding tidy. I'm getting into a right mess. I'll show what I have so far, but I'm getting in a muddle.
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
while($row = mysql_fetch_array($result))
{
$profileText = $row['text'];
}
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
echo
'<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">' .
$profileText .'
</textarea><br />
<input type="submit" value="Submit" />
</form>'
;}
else{
$profileText = $row['text'];
echo
"<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>" .
$profileText
."</textarea><br />
<input type='submit' value='Submit' />
</form>"
;}?>
You've pretty much got the functionality there, just needs tidying up.
Try something like this:
<?php
//look in db to see if content exists, if it does set variable
$profileText="";
if($result = mysql_query("SELECT * from tbl_profiles WHERE user_id = $who")) {
while($row = mysql_fetch_array($result))
{
$profileText .= $row['text'];
}
?>
<form action="../edit/indexUpdate.php" method="post" name="edit">
Comments:<br />
<textarea name="updatedText" id="comments">
<?php echo $profileText; ?>
</textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
} else {
?>
<form action='../edit/index.php' method='post' name='add'>
Comments:<br />
<textarea name='comments' id='comments'>
<?php echo $profileText; ?>
</textarea><br />
<input type='submit' value='Submit' />
</form>
<?php
}
?>
The basic idea is to add a record if new and update if not. What you can do is use an id to represent the record or -1 if it's a new entry
Something along the lines of:
//Defaults
$recordid=-1;
$name='';
$comments='';
//look in db to see if content exists, if it does set variable
$result = mysql_query(
"SELECT * from tbl_profiles
WHERE user_id = $who
");
// Check if user has content in db
$result = mysql_query(
"SELECT * FROM tbl_profiles WHERE user_id='$who'");
if(mysql_fetch_array($result) !== false){
//Yes. Get the id
$recordid = $result->id;
//Get the values
$name= $result->name;
$comments= $result->name;
}
<form action="../edit/index.php" method="post" name="formdata">
<input type="hidden" name="recordid" value="<? echo htmlspecialchars($recordid) ?>">
<input type="hidden" name="name" value="<? echo htmlspecialchars($name) ?>">
<textarea name="comments" id="comments"><? echo htmlspecialchars($comments) ?></textarea>
<input type="submit" value="submit"/>
</form>
This way a new form will have a -1 but an existing will have an id.
As an additional point it is very important to sanitize your inputs for SQL and what you output in HTML to stop SQL Injections. For your reference on this:
SQL
Little Bobby Tables
Cross Site Scripting
I have a page that display products and I would like to get the product ID when I click on the particular item and pass it to another page.
May I know how can I achieve this?
I always get the last PID, my code:
<head>
<title>Toy-All</title>
<!--Wilmos: Using external CSS File to format the page style and fonts.-->
<link href="StyleSheet2.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<form method = "post" action "getpid.php">
<div class="stylediv2-Middle-Toy-all">
<div class="transbox-Toy-all">
<?php
//open connection to MySQL Server
$connection = mysql_connect('localhost','root', '')
or die ('Unable to connect to !');
// select database for use
mysql_select_db('we-toys') or die ('Unable to select database!');
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</h1>
';
}
}
else
{
echo "No rows found!";
}
mysql_free_result($result);
mysql_close($connection);
?>
</div>
</div>
</form>
</body>
</html>
If you retrieve your data from $_SESSION['PID'], then you will always get the last ID because you keep reassign new value to that session.
You can just achieve this with a link to the another PHP page. For example:
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a>
A more completed code as requested
<?php
$query = 'SELECT p.*, price.priceN FROM product p, pricing price
WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
?>
<?php while ($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row[1]; ?></h3>
<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a><br><br>
<?php } ?>
And for anotherPage.php code
<?php
echo "You are trying to add this product ID to cart: " . $_GET['id'];
?>
You can use this form that i also provide in this code.
$query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1';
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
$pid = ($row[0]);
$_SESSION['PID'] = $pid;
echo '<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'" width=200px height=200px; ?> </div>
<h3>'.$row[1].'</h3>
<h1><span style="color:red"> $ '.$row[7].' </span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value= '.$row[0].' >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
$pid = '.$row[0].';
</h1>
';
}
}
Now you should make a new page such as cart.php
echo $_POST['pid'];
If I understand you correctly, the following should work:
<form method="post" action="anotherPage.php">
<input type="hidden" name="id" value="<?php echo "$row[0]"?>"/>
<input id="AddtoCart-Btn" type="Submit" value="<?php echo "$row[0]" ?>" />
</form>
So basically when you click the product button, the id will be accessible in anotherPage.php
EDIT:
I rewrote your code to improve readability:
<div style="float:left;margin-right: 10px; margin-left: 10px;"><img src=<?php echo $row[5]; ?> width=200px height=200px; ?> </div>
<h3><?php echo $row[1]; ?></h3>
<h1>
<span style="color:red"> $ <?php echo $row[7] ?></span>
<form method="post" action="cart.php">
<input type="hidden" name="pid" value=<?php $row[0]; ?> >
<input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >
</form>
<?php $pid = $row[0]; ?>
</h1>
Avoid echo-ing out large chunks of HTML where possible. Try it now, If it fails provide the error message.
The above does, what the simple test below achieves:
<?php
$id = 1;
if (isset($_POST['submit_btn'])){
echo $_POST['id'];
}
?>
<form method="post" action="#">
<input type="hidden" name="id" value= <?php echo $id; ?> >
<input type="submit" name="submit_btn" value="submit">
</form>