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.
Related
So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances.
Here is my script:
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$count = mysqli_num_rows($query);
if($count != "1"){
$output = '<h2>No result found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s = $row['name'];
$output .= '<h2>Found: '.$s.'</h2><br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php echo $output; ?>
</body>
</html>
Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?
<?php
if ($count==0) {
echo '<input type="submit" name="return" value="Return">';
}
?>
If you want a much cleaner html code, do this:
<form method="POST" action="index.html">
<?php if ($count!= "1") : ?>
<input type="submit" name="return" value="Return">
<?php else : ?>
<!-- put your other button here -->
<?php endif; ?>
</form>
You can read more about escaping from HTML here.
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$results = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php if(0 < count($results)) ?>
<?php foreach($results AS $row) : ?>
<h2><?= $row['name'] ?></h2>
<?php endforeach; ?>
<?php else : ?>
<H2> No results found!</h2>
<?php endif; ?>
</body>
</html>
I am working on a school assignment and I have run into some issues. I have PHP code for a form that, when selected, sends the selected result to a MySQL database and then loops through and displays the results. The only problem is that, instead of showing the selected <option>, it shows all four of the options.
Here is my code:
<?php
include_once (connection.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_label']);
$insert_card_genre_query = sprintf("INSERT into card_catalog (card_name, card_label) VALUES ('%s', '%s')",
$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 card_label asc";
$card_genre = mysqli_query($conn, $card_genre_query) or die(mysqli_error($conn));
$get_card_genre_query = "SELECT card_catalog.id, card_catalog.card_name, card_catalog.card_label, card_genre.genre_id from card_catalog right join card_genre on card_catalog.card_label = card_genre.card_label";
$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="stylesheets/tmp.css">
</head>
<body>
<?php include(templatestuff/top_of_tmp.php); ?>
<main>
<h1>Products:</h1>
<section>
<ul id="products_list">
<li><b>Product 1: "Jack-In-The-Mox"</b></li>
<li><b>Product Description: "Roll a six-sided die for Jack-in-the-Mox. On a 1, sacrifice Jack-in-the-Mox and lose 5 life. Otherwise, Jack-in-the-Mox has one of the following effects. Treat this ability as a mana source..."</b></li>
<img src="productimages/jackinthemox.jpeg" alt="Jack In The Mox"/>
</ul>
</section>
<div>
<h2>What Card Are You Looking For?</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<fieldset>
<p><b>What's The Card Name? <input type="text" name="card_name"></b></p>
<p>
<p><b>Card Type:</b></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'];?></option>
<?php } ?>
</select>
</p>
<p><input type="submit"></p>
<input type="hidden" name="card_catalog_form">
</fieldset>
</form>
<?php
if ($last_record) {
echo "<p><b>You just created form query #" . $last_record ."</b><p>";
}
?>
<p>You are submitting your form at
<?php
date_default_timezone_set('America/New_York');
echo date('g:i a \o\n l, F j, Y');
?>
</p>
</div>
<?php
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
while($row = mysqli_fetch_array($result)){
echo $row['card_name']. " - ". $row['genre_id'];
echo "<br />";
}
?>
</main>
<?php include('templatestuff/bottom_of_tmp.php'); ?>
</body>
</html>`
(If you want to see the website that contains the problem, you can go here)
Any help, or constructive criticism, would be greatly appreciated.
Thanks
This might point you in the right direction:
if (isset($_POST['card_genre'])) {
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id 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['genre_id'];
echo "<br />";
}
} else {
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($result)) {
echo $row['card_name']." - ".$row['genre_id'];
echo "<br />";
}
}
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);
}
?>
There are 02 tables called item and customer.
item(item_id, item_name)
customer(cus_id, iid, cus_name)
I just tried to store item_id from item to the iid in the customer.
but it always showing null values.
My database is item_sales.
Here is my PHP code
<html>
<title></title>
<head></head>
<body>
<?php
$hostname = "localhost";
$database = "item_sales";
$username = "root";
$password = "";
$con = mysql_pconnect($hostname, $username, $password);
error_reporting(0);
?>
<form action="index.php" method="post" enctype="multipart/form-data">
<p>Customer Name : <input type="text" name="cus_name" /><br/><br/> </p>
<p>Select an Item:
<select name="iid">
<?php
$sql = mysql_query("SELECT * FROM item");
mysql_select_db($database,$con);
while($sqlv = mysql_fetch_array($sql))
{ ?>
<option id="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
<?php } ?>
</select>
</p>
<?php
if(isset($_POST['submit']))
{
$sql2 = "SELECT * FROM item WHERE iid='%item_id%'";
mysql_select_db($database,$con);
$mydata = mysql_query($sql2);
$cus_name = $_POST['cus_name'];
$sql3 = "INSERT INTO customer (cus_id, iid, cus_name) VALUES ('', '$_POST[iid]', '$cus_name')";
mysql_query($sql3);
}
?>
<input type="submit" name="submit" value="Add Sale" />
</form>
</body>
</html>
The reason it is not working is that you are attempting to save the iid select into the iid field, and I'm guessing the iid field in customer is a numeric type field, like INT - using the POST variable like this, you are going to be saving the text of the SELECT rather than the val.
What you need to do to fix this particular problem is set a "value" on each of the select options. You've set an ID but thats no real help here.
<select name="iid">
<?php
$sql = mysql_query("SELECT * FROM item");
mysql_select_db($database,$con);
while($sqlv = mysql_fetch_array($sql))
{ ?>
<option value="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
<?php } ?>
</select>
This is besides the point your code is very dangerous. I would recommend you do not use the original mysql functions as, 1) they don't offer any real protection from malicious users, and 2) they will be removed from PHP support very soon.
See this SO article on how to replace the mysql functionality from your PHP code : How can I prevent SQL injection in PHP?
That article also might help you understand the dangers your code offers.
The correct code is following :
<html>
<title></title>
<head></head>
<body>
<?php
$hostname = "localhost";
$database = "item_sales";
$username = "root";
$password = "";
$con = mysql_pconnect($hostname, $username, $password);
error_reporting(0);
?>
<form action="index.php" method="post" enctype="multipart/form-data">
<p>Customer Name : <input type="text" name="cus_name" /><br/><br/> </p>
<p>Select an Item:
<select name="iid">
<?php
$sql = mysql_query("SELECT * FROM item");
mysql_select_db($database,$con);
while($sqlv = mysql_fetch_array($sql))
{ ?>
<option value="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
<?php } ?>
</select>
</p>
<?php
if(isset($_POST['submit']))
{
$sql2 = "SELECT * FROM item";
mysql_select_db($database,$con);
$mydata = mysql_query($sql2);
$cus_name = $_POST['cus_name'];
$iid = $_GET['item_id'];
$sql3 = "INSERT INTO customer (cus_id, iid, cus_name) VALUES ('', '$_POST[iid]', '$cus_name')";
mysql_query($sql3);
}
?>
<input type="submit" name="submit" value="Add Sale" />
</form>
</body>
</html>
My goal to my code is to show the count query for the number of batchcode in table batchcodes to my textbox and if i click the button it will save to the batchcode table...my batchcode field is
'id','batchcode'
my current code:
<?php
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('include/connect.php');
$query = "SELECT DISTINCT count(batchcode) FROM batchcodes";
while( $rows = mysql_fetch_array($query)) {
}
?>
<?php
if(isset($_POST['save'])){
$var = $query+1;
$sql = "INSERT INTO batchcodes(batchcode) VALUES ('$var')";
}
?>
<form method="post" action="index.php" >
<input type="text" value="batch<?php echo $query; ?>" />
<input type="submit" name"save" />
</form>
</body>
</html>
In my code im suffering from error like Undefined variable query and warning mysql_fetch_array expects parameter 1...I need your help guys.
Use mysql_query.
$query = mysql_query("SELECT DISTINCT count(batchcode) AS nb_batchcode FROM batchcode");
while($row= mysql_fetch_array($query)) {
$batchcode=$row['nb_batchcode'];
}
<input type="text" name="save" value="batch<?php echo $batchcode; ?>" />
You should use mysql_query function to execute the query
$query = mysql_query("SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes");
$sql = mysql_query("INSERT INTO batchcodes(batchcode) VALUES (". $var .")");
Here's how I did it on PHP based on my SQL data
<li>
<label> OR #: </label>
<?php
include('php/connect-db.php');
$sql = mysql_query("SELECT MAX(or_num)+1 AS inc_or FROM tbl_admission");
while($row = mysql_fetch_array($sql)){
$nextOR=$row['inc_or'];
}
?>
<input type="text" name="asID" value="<?php echo $nextOR; ?>" disabled>
OR_num is my integer and auto-incremented key in my table "tbl_admission" :)
Use $row instead of $query to get result from query.Try below code can help you.
<?php
ob_start();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
include('include/connect.php');
$query = "SELECT DISTINCT count(batchcode) as batchcode FROM batchcodes";
while( $rows = mysql_fetch_array($query)) {
}
?>
<?php
if(isset($_POST['save'])){
$var = $row[0] + 1;
$sql = "INSERT INTO batchcodes(batchcode) VALUES (". $var .")";
}
?>
<form method="post" action="index.php" >
<input type="text" name="save" value="batch<?php echo $query; ?>" />
<input type="submit"
</form>
</body>
</html>