HTML / PHP / SQL How to display a button under certain circumstances? - php

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>

Related

Deleting multiple rows using checkbox

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
}
}
}
?>

MySQL/PHP incorrect id displaying

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.

JSON header in inner php file affecting outer php file

Let say I have file1.php containing:
<!DOCTYPE html>
<html>
<head>
<title>My page</title>
</head>
<body>
<form action="file1.php" method="post">
<input type="radio" name="radio" value="1">1
<input type="radio" name="radio" value="2">2
<?php include 'file2.php'; ?>
<input type="submit" name="submit" value="Submit">
</form>
//some codes omitted here
</body>
</html>
and file2.php:
<?php
header('Content-Type: application/json');
$value_selected = 0;
if (isset($_POST['submit'])) {
if(isset($_POST['radio'])) {
$value_selected = $_POST['radio'];
}
}
include("connect_to_database.php");
$sql =
"SELECT field1, field2 FROM some_table WHERE field1 = ".$value_selected;
$query = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
mysql_free_result($result);
print json_encode($data);
?>
The problem is when I run file1.php, it displays in text (JSON I believe) form because of the header in file2.php. Is there any way to make only the file2.php in JSON format but not file1.php?
If you are not quite ready for coding an AJAX call to ask for this data from your javascript then this may get you moving in the right direction.
It will load the data into a javascript variable that can be used from your other javascript
file2.php
<?php
$value_selected = 0;
if (isset($_POST['submit'])) {
if(isset($_POST['radio'])) {
$value_selected = $_POST['radio'];
include("connect_to_database.php");
$sql = "SELECT field1, field2
FROM some_table
WHERE field1 = ".$value_selected;
$query = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
echo '<script type="text/javascript">';
echo 'var theData = ' . json_encode($data) . ';';
echo '<script type="text/javascript">';
mysql_free_result($result);
}
}
?>
Now call this from inside your <head> tag and you will be a little closer to what you want.
file1.php
<!DOCTYPE html>
<html>
<head>
<title>My page</title>
<?php include 'file2.php'; ?>
</head>
<body>
<form action="file1.php" method="post">
<input type="radio" name="radio" value="1">1
<input type="radio" name="radio" value="2">2
<input type="submit" name="submit" value="Submit">
</form>
//some codes omitted here
</body>
</html>
However I feel I must add
Please dont use the mysql_ database extension it is deprecated and has been for years and is gone for ever in PHP7.
If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements.
Start here

Making html page appear on click of submit button

I am trying to make another page to appear when I click certain button, I basically know how to do this, but this time I am in trouble.I have following code:
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
<?php
if(isset($_POST['saada'])){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/saada.html'>";
}
?>
Everything works just fine but when I click button 'saada' nothing happens. What should I do to make saada.html appear on this click?
replace this:
<form action='Minu_kysitlused_1.php' method="post">
Missing:
method="post"
why you try with meta tag
You just put header("location:yourpagename");
just like below
if(isset($_POST['saada'])){
header("Location:http://localhost/Praks/saada.html");
}
try like this
header function must be First thing to sent as html otherwise it will not work
<?php
if(isset($_POST['saada'])){
header( "Location : localhost/Praks/saada.html");
die();
}
?>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
second approach
<form action="your-url">
<input type="submit" name="Submit" value="saada"/>
</form>
in first time, I don't understand why do you use the form tag to call a page, if there aren't any data passed via POST?
Anyway, in your foreach cicle you can use a very simple collection of link:
foreach($titles as $title) {
echo $title;
echo 'Link 1';
echo 'Link 2';
echo 'Link 3';
echo 'Link 4';
}
This solution is too clean.
Bye
Marco

Display count in the textbox

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>

Categories