I displayed a set of questions.
On selecting one of those questions for which I required the answers to be posted on the same page, but the problem is I am not getting id parameter from url which I am passing on selectiong the question.
Now when I am trying to answer the question which is selected, I will need the id of question to post the answer of that perticular question which I already have in url as a parameter for Example: id=1.
Here is the body section of html page:
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>
answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
After submit the page "submit_answer.php", Code is:
<?php
include'config.php';
if($conn){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$answers = $_REQUEST['answers'];
$id= $_GET ['id'];
}
$sqli= "INSERT INTO answers (answers)
VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}else{
}
mysqli_close($conn);
?>
Basically I am very much fresher in Php I just want to know how should I get the id of question and submit it to the "submit_answer.php" with the answer content.
just take a hidden field below the answer field and get the url parameter to that hidden field on page load, as said by user buivankim2020 and submit submit_answer.php,
after submit get the value of that field in variable like what you do for getting the answer..
You should change it (add hidden input for id in markup html)
<?php
include'config.php';
//session_start();
$id= $_GET ['id'];
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="tinymce/js/jquery.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/tinymce.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/init-tinymce.js"></script>
</head>
<body>
<div id="container">
<div id="main">
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
submit_answer.php
<?php
include'config.php';
if($conn){
if (isset($_POST['answers']) && isset($_POST['id'])) {
$answers = $_POST['answers'];
$id= $_POST['id'];
$sqli= "INSERT INTO answers (answers) VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}
mysqli_close($conn);
}
?>
Related
I have this code
<html>
<head>
<meta charset="UTF-8">
<title> Game Library</title>
<link href="css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="search">
<img id="del" src="img/delete.jpg" alt="Error">
<a class="A" href="index.php"><img class="AR" src="img/src.jpg" title="Search library"></a>
<a class="A" href="insert.php"><img class="AR" src="img/add.png" title="Add game"></a>
<div id="results">
<?php
require("inc/connection.php");
$query = "SELECT * FROM Library";
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?><div id="results">
<form action="inc/delete.php" method="GET">
<input type="checkbox" name="checkbox[]" value="<?php echo $row['ID'] ?>">
<p id="p1">Name: <?php echo $row['Name']?>;</p>
<p>Genre: <?php echo $row['Genre']?></p>
<p>Release date: <?php echo $row['Release_date']?></p>
<p>Publisher: <?php echo $row['Publisher']?>:</p>
<p>Platforms: <?php echo $row['Platforms']?></p>
</div>
<?php
}?>
<input type="submit" name = "submit" value = "Submit"></form>
<?php
}else{
echo "No results!";
}
</div>
</div>
</div>
</body>
</html>
And I am supposed to create action="inc/delete.php" script which will allow me to delete multiple rows using checkbox and submit button. Can someone help me out with this please ? I honestly have no clue on how to complete this ....
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $key => $check)
{
// delete query
}
}
}
?>
NOTE: You failed to close PHP at line no. 40. Please make sure your PHP tag is closed or not.
Need to first set form method to `POST'
So, modify
<form action="inc/delete.php" method="GET">
To:
<form action="inc/delete.php" method="post">
And in inc/delete.php,
if (isset($_POST['submit'])){ // Check if form is submitted.
if (! empty($_POST['checkbox'])){ // Check if Checkbox is checked.
// Loop over the checkbox and get selected ids.
foreach($_POST['checkbox'] as $checkbox_id){
echo $checkbox_id."<br/>";
// Add your delete query here.
}
}
}
So the resolution is that you get at your delete script array of ids. You can parse that array and execute DELETE query for each id:
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $val)
{
$stmt = $conn->prepare("DELETE FROM Library WHERE id = ?");
$stmt->bind_param('i', $val);
$stmt->execute();
}
}
inc/delete.php
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['checkbox'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $row_id){
echo $row_id."</br>";
// DELETE QUERY HERE
}
}
}
?>
I am currently working on a school project and I need a little help. I am writing PHP/SQL code for a page where, when the user submits a form, a query runs that loops through and displays the user text input and also the value associated with the <select> dropdown.
(For a visual idea of what I mean, visit http://themanaclub.com/themarketplace2/themarketplace2.php)
Here is my code:
<?php
include_once ('connection2.php');
if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_POST['card_catalog_form']))) {
$card_name = mysqli_real_escape_string($conn, $_POST['card_name']);
$card_label = mysqli_real_escape_string($conn, $_POST['card_genre']);
$insert_card_genre_query = sprintf("INSERT into card_catalog (card_name, label_id) VALUES ('%s', %u)",
$card_name,
$card_label);
$insert_card_genre = mysqli_query($conn, $insert_card_genre_query) or die (mysqli_error($conn));
$last_record = mysqli_insert_id($conn);
}
$card_genre_query = "SELECT card_genre.genre_id, card_label from `card_genre` order by genre_id desc";
$card_genre = mysqli_query($conn, $card_genre_query) or die(mysqli_error($conn));
$get_card_genre_query = "SELECT card_catalog.id, card_name, label_id, card_label from card_catalog left join card_genre on label_id = genre_id";
$get_card_genre = mysqli_query($conn, $get_card_genre_query) or die(mysqli_error($conn));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The Marketplace By The Mana Club</title>
<link rel="stylesheet" type="text/css" href="stylesheets2/tmp2.css">
</head>
<body>
<?php include('templatestuff2/top_of_tmp2.php'); ?>
<main>
<h1>Card Input Form:</h1>
<form id="card_name_entry" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<h4>What Card Are You Looking For?</h4>
<textarea name="card_label" rows="5" cols="30" placeholder="Write the name of the card here"></textarea>
<p>
<select name="card_genre">
<?php while ($row_card_genre = mysqli_fetch_assoc($card_genre)) { ?>
<option value="<?php echo $row_card_genre['genre_id'];?>"><?php echo $row_card_genre['card_label'];?><?php echo $row_card_genre['card_name'];?></option>
<?php } ?>
</select>
</p>
<?php $query = "SELECT label_id from `card_catalog` right join `card_genre` on genre_id"; ?>
<p id="textareasubmit"><input type="submit"></p>
<input type="hidden" name="card_catalog_form">
</form>
<section id="all_questions">
<ul>
<?php while ($row_card_genre = mysqli_fetch_assoc($get_card_genre)) { ?>
<li><?php echo $row_card_genre['card_label'];?></li>
<?php }
$row_card_genre = mysqli_data_seek($get_card_genre, 0);
?>
</ul>
</section>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<p id="deletethissubmit"><input type="submit" value="Delete This"></p>
<input type="hidden" name="card_catalog_delete">
<input type="hidden" name="genre_id" value="<?php echo $row_card_catalog['label_id'];?>"
</form>
<p>You're asking this at
<?php
date_default_timezone_set('America/New_York');
echo date('g:i a T \o\n l, F j, Y');
?>
</p>
<p id="backtothemarketplace">Back To The Marketplace</p>
<?php
/*if (isset($_POST['card_genre'])) {
$query = "SELECT card_catalog.card_name, card_catalog.label_id, card_genre.genre_id, card_genre.card_label FROM card_catalog, card_genre WHERE card_genre.genre_id = ?";
$stmt = mysqli_prepare($conn, $query);
$stmt->bind_param('s', $_POST['card_genre']);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['card_name']." - ".$row['label_id'];
echo "<br />";
}
}*/
?>
</main>
<?php include('templatestuff2/bottom_of_tmp2.php'); ?>
</body>
</html>
If anybody has any help or constructive criticism, it would be greatly appreciated (I'm a PHP/SQL newbie so I'll take all the help that I can get).
Thanks
Messed around with the code and I fixed it. The id is now being displayed and it links to the correct page.
All that was wrong with my code was that I mixed up some of the variables and ids.
Thank you all for your help.
update
Can anyone explain to me why I am getting duplicate messages instead of one?
how can I change my code so that when I type a comment and press "Comment" button, it will only display one message instead of duplicates! When I have one comment boxes it doesn't show duplicate comments, but if I have more than one then it starts duplicating!
COMMENT.INC.PHP
include 'cdbh.inc.php';
function setComments($con)
{
if (isset($_POST['commentSubmit'])) {
$uid = mysqli_real_escape_string($con,$_POST['uid']);
$date = mysqli_real_escape_string($con,$_POST['date']);
$message = mysqli_real_escape_string($con,$_POST['message']);
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid','$date','$message')";
$result = mysqli_query($con,$sql);
}
}
function getComments($con)
{
$sql = "SELECT * FROM comments";
$result = mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc($result)) {
echo $row['uid'];
echo ":";
echo $row['message']."<br><br>";
}
}
page code
<?php
date_default_timezone_set('America/Los_Angeles');
include 'comment.inc.php';
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="comment.css" rel ="stylesheet">
</head>
<body>
<?php
$sql="Select * from tbl_images";
$result=mysqli_query($connection,$sql);
while ($row=mysqli_fetch_array($result)) {
?>
<img src="images/<?php echo $row['images_name'] ?>" width="200px" height="200px">
<?php
echo "<form method ='POST' action ='".setComments($con)."'>
<input type ='hidden' name ='uid' value='unknown'>
<input type ='hidden' name ='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea>
<button type ='submit' name='commentSubmit'>Comment</button>
</form>";
}
getComments($con);
?>
</body>
</html>
Maybe you are submiting all your forms instead of one..
check your database in order to know from what img comes each message.
If you have other code like javascript, you should post it.
My aim to is to Update Value in Database By using Update Query . On my first page i have just displayed database table in webpage. Then by using hyperlink i have to click on Edit to second page "edit.php".While on first page i have to get the value of id and send it to second page. Where a input form is displayed which gets Value casually but Id through hidden tag. On third page getting the values query is implented but the value of id is missing.
First Page
<html>
<head>
<title>Assignment</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$result = mysql_query("SELECT * FROM teacher ",$con);
?><table cellpadding="2px" border="2px"><?php
while($row = mysql_fetch_array($result)) {
?> <tr>
<td><a href="edit.php?id=<?php
echo $row['id']; ?>">Edit</a > Delete
</td><td>
<?php
echo $row['id']; ?></td><td> <?php echo $row['name'];?></td><td><?php echo $row['program']; ?></td>
<?php }
?></table><?php
mysql_close($con);
?>
</body>
</html>
Secnod Page edit.php
<html>
<head>
<title>Assignment Edit</title>
</head>
<body>
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="get">
Address <input type="text" name="program"><br>
<input type="hidden" name="id" value='<?php $id?>'>
<input type="submit" name="submit">
</form>
</body>
</html>
Third Page update.php
<html>
<head>
<title>Update Page</title>
</head>
<body>
<?php
$add=$_GET['program'];
$id=$_GET['id'];
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$query = "UPDATE teacher SET program='$add' WHERE id =".$id;
echo $query;
$result = mysql_query($query,$con);
/* while($row = mysql_fetch_array($result)) {
echo $row['id'] ." " . $row['name']." ". $row['address']."<br>";
}
mysql_close($con);
*/
?>
</body>
</html>
output
UPDATE teacher SET program='openSource' WHERE id =
you need to change this
<input type="hidden" name="id" value='<?php $id?>'>
to
<input type="hidden" name="id" value='<?php echo $id?>'>
(or)
<input type="hidden" name="id" value='<?=$id?>'>
<!doctype html>
<html>
<head>
<title>Main Page</title>
</head>
<?php
session_start();
?>
<form action="new_question.php" method="post">
<input type="hidden" name="sid" value="<?php echo $_SESSION['username']?>">
<input type="submit" value="New Question">
</form>
<?php
include ("connection.php");
$result = mysqli_query($con,"SELECT * FROM question_table");
while($row = mysqli_fetch_array($result))
{
echo "" . $row['question'] . $row['q_id'] . "";
echo "<br>";
}
?>
<body>
</body>
</html>
I have 5 question in my database each with a id. this page prints them as a link in loop. upon clicking any of the link it goes to "question.php" file. there i want to echo the question from the database that was clicked previously. the problem is in "question.php" file how do i find out which link was clicked among thus 5. should i send a parameter along with the link? how the parameter will change in each loop? how do i do it in this page? if i do send a parameter with the link how do i receive it in the "question.php" file?
Echo the id as a parameter on the anchor. We can also remove the id from the anchor text since it's not needed there anymore.
while($row = mysqli_fetch_array($result))
{
echo '' . $row['question'] . '<br>';
}
And then in question.php do $_GET['id']