How to store and get two values in PHP? - php

This is process_upcategory.php
I want to update the category name or the category id with another category name/id by its category id or by its category name.
I'm new to php
<?php
require('includes/config.php');
if(!empty($_POST))
{
$msg=array();
if(empty($_POST['cat']))
{
$msg[]="Please full fill all requirement";
}
if(!empty($msg))
{
echo '<b>Error:-</b><br>';
foreach($msg as $k)
{
echo '<li>'.$k;
}
}
else
{
$cat_nm=$_POST['cat[0]'];
$cat_id=$_POST['cat[1]'];
$query= "UPDATE `category` SET cat_nm='$cat_nm' WHERE cat_id='$cat_id'";
mysqli_query($conn,$query) or die("can't Execute...");
mysql_close($link);
header("location:category.php");
}
}
else
{
header("location:index.php");
}
?>
Now this is category.php, just a snippet of code. Not whole code
<form action='process_upcategory.php' method='POST'>
<b style="color:darkgreen">UPDATE CATEGORY </b> <br>
<b style="color:darkgreen">Old Category</b>
<br>
<select name="cat[]" multiple>
<?php
$query="select * from category ";
$res=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($res))
{
echo "<option>".$row['cat_nm'];
echo "<option>".$row['cat_id'];
}
?>
</select>
<br>
<b style="color:darkgreen">New Category</b><br>
<input type='text' name='cat[0]'></input><br>
<input type='text' name='cat[1]'></input>
<input type='submit' value=' UPDATE '>
</form>
I want to update the category name with another category name by its category id or by its category name. I get undefined index cat[0] and cat[1]

When you end an input name with [] it wil be converted to an array by php. The correct way to get the values in this case would be something like this:
$cat=$_POST['cat'];
$cat_nm=$cat[0];
$cat_id=$cat[1];

I combined the two routines into one script.
I added 'sub' to the form to distinguish from when the form was submitted or not.
I used list() in the query result loop.Used mysqli_fetch_array($result, MYSQLI_NUM) rather than mysqli_fetch_assoc($res)
used foreach() to loop through the $_POST['cat']
Added 'value' to the <option value=""> to hold the id
Eliminated the switching from HTML mode to PHP mode by using HEREDOC.
<?php
if (intval($_POST['sub']) == 1){
$newcat = $_POST['new'];
foreach($_POST['cat'] as $key=>$value){
if(strlen($newcat[$key]) > 0){
mysqli_query($conn,"UPDATE `category` SET `cat_nm`='$newcat[$key]' WHERE `cat_id`='$value'");
}
}
}
echo <<<EOT
<html><head><style>h4,h3{color:darkgreen;margin:.2em;}</style></head><body>
<form action="#" method='POST'>
<h3>UPDATE CATEGORY</h3>
<h4>Old Category</h3>
<select name="cat[]" multiple>
EOT;
$sql="SELECT `cat_nm`, `cat_id` FROM `category` ";
$result=mysqli_query($conn,$sql);
while(list($cat_nm,$cat_id) = mysqli_fetch_array($result, MYSQLI_NUM)){
echo " <option value=\"$cat_id\">$cat_nm</option>\n";
}
echo <<<EOT
</select>
<h3>New Category</h3>
<input type="text" name="new[0]" /><br/>
<input type="text" name="new[1]" /><br/>
<input type="hidden" name="sub" value="1" /><br/>
<input type="submit" value=" UPDATE />
</form>
</body></html>
EOT;
?>

Related

how do I retain search form results while submitting another form on the page?

I start by showing a list of businesses I have stored in a db. There is a form to search by industry or state. When showing a list of search results, I also provide the option to move the businesses to different tables. After submitting the form to move a business to a different table, the list refreshes to default result list, and we have to enter search term again.
I've tried assigning $_POST values to dynamic urls in the action url of my forms, I've tried assigning $_POST values to the value="" parameter of my forms.
<?php
if (isset($_POST['update']) && ($_POST['update'] == 'true')){
$sql = 'INSERT INTO table1 (columns,columns,columns)
SELECT columns,columns,columns
FROM table2 WHERE id = 1';
if (mysqli_query($db, $sql)) {}
}
?>
<div>
<form action="./?action=list" method="POST">
<input type="hidden" name="search" value="true" />
<input placeholder=" INDUSTRY" type="text" size="15" name="kw" />
<select name="state" onchange='this.form.submit()'>
<option value=''>BY STATE</option>
<?php require('includes/stateselect.php'); ?>
</select>
<input type="submit" value="SEARCH">
</form>
</div>
<table>
<?php
$sql = 'SELECT * FROM db.table ';
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE \'%'.$_POST['kw'].'%\' ';
}
if ((isset($_POST['state'])) && (!empty($_POST['state']))){
$sql .=' WHERE `state` = \''.$_POST['state'].'\' ';
}
if ($result = mysqli_query($db, $sql)){
while ($row = mysqli_fetch_array($result)) {
echo '<tr><form method="POST" action="./?action=list">
<input type="hidden" name="id" value="'.$row['id'].'" />
<input type="hidden" name="update" value="true" />
<td>'.$row['kw'] .'</td><td>'. $row['state'].'</td>';
echo '<td>
<select name="move">
<option>--Fresh--</option>
<option value="dnc">DNC</option>
</select>';
echo '<input type="submit" value="submit">';
echo '</td></form></tr>';
}
}
?>
</table>
We want to be able to search for lawyers, then disposition them from the list as we call them, but retain our search results.
You can store the search term inside a session so that you can use that term multiple times in your form. Store the search term in the session like this,
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){\
$_SESSION['search_term] = $_POST['kw'];
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE
\'%'.$_POST['kw'].'%\' ';
}
You could try to re-set the kw as a hidden input within the second form to retain the search terms, like this :
<?php
if (isset($_POST['update']) && ($_POST['update'] == 'true')){
$sql = 'INSERT INTO table1 (columns,columns,columns)
SELECT columns,columns,columns
FROM table2 WHERE id = 1';
if (mysqli_query($db, $sql)) {}
}
?>
<div>
<form action="./?action=list" method="POST">
<input type="hidden" name="search" value="true" />
<input placeholder=" INDUSTRY" type="text" size="15" name="kw" />
<select name="state" onchange='this.form.submit()'>
<option value=''>BY STATE</option>
<?php require('includes/stateselect.php'); ?>
</select>
<input type="submit" value="SEARCH">
</form>
</div>
<table>
<?php
$sql = 'SELECT * FROM db.table ';
if ((isset($_POST['kw'])) && (!empty($_POST['kw']))){
$sql .=' WHERE `kw` LIKE \'%'.$_REQUEST['kw'].'%\' OR `biz` LIKE \'%'.$_POST['kw'].'%\' ';
}
if ((isset($_POST['state'])) && (!empty($_POST['state']))){
$sql .=' WHERE `state` = \''.$_POST['state'].'\' ';
}
if ($result = mysqli_query($db, $sql)){
while ($row = mysqli_fetch_array($result)) {
echo '<tr><form method="POST" action="./?action=list">
<input type="hidden" name="id" value="'.$row['id'].'" />
<input type="hidden" name="update" value="true" />
<td>'.$row['kw'] .'</td><td>'. $row['state'].'</td>';
echo '<td>
<select name="move">
<option>--Fresh--</option>
<option value="dnc">DNC</option>
</select>';
// added below blocks
if (!empty($_POST['kw'])){
echo '<input type="hidden" name="kw" value="'.$_POST['kw'].'" />';
// echo '<input type="hidden" name="search" value="true" />';
}
if (!empty($_POST['state'])){
echo '<input type="hidden" name="state" value="'.$_POST['state'].'" />';
}
echo '<input type="submit" value="submit">';
echo '</td></form></tr>';
}
}
?>
</table>

Search multiple Criteria - And/or search in PHP

Not sure what I am doing wrong here. I would like to create a search that allows the user to do an and or search.
However when I use the below code, if I type in Brown as Colour1 it will return all results, same as post code.
The goal is to allow the user to search multiple fields to return a match. So Colour1 and Postcode
<html>
<head>
<title> Logo Search</title>
<style type="text/css">
table {
background-color: #FCF;
}
th {
width: 250px;
text-align: left;
}
</style>
</head>
<body>
<h1> National Logo Search</h1>
<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>
<label>Colour 1: <input type="text" name="criteria" /></label>
<label>Colour 2: <input type="text" name="criteria2" /></label>
<label>PostCode: <input type="text" name="criteria3" /></label>
<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')
or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')
LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
echo "<table>";
echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";
}
echo "</table>";
}// end of main if statment
?>
</body>
</html>
I can get it to work correctly when I use a dropdown list to select the criteria however I would like them to have multiple options to filter results.
<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>
<label>Search Category:
<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>
<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');
In general you run your query with AND condition because it has criteria and you have it means that you have to show value by matching each criteria with receptive column. but it also depends on users or client how they want to show their field.
In short it doesn't have any rule how to show.
Played around with it for a bit and found the answer. I needed to define the criteria. see below code
<?php
if (isset($_POST['submitted'])) {
// connect to the database
include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];
$criteria2 = $_POST['criteria2'];
$criteria3 = $_POST['criteria3'];
$criteria4 = $_POST['criteria4'];
$criteria5 = $_POST['criteria5'];
$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%')
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')

Checkbox in a while loop php

I have a check box inside a while loop like this:
<form method="POST">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` ='$id' ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names" value="<? echo $get ['id'];?>"><?php echo $get ['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
The problem is at this part I am unable to get specific checkbox properties and even if the user selects two check boxes I am unable to echo the id out
<?php
if(isset($_POST['id_names']))
{
$id_names= $_POST['id_names'];
$email = mysql_query("SELECT `email` FROM users WHERE `id` = '$id_names' ");
while ($getemail = mysql_fetch_array($email))
{
echo $getemail['email'];
}
}
?>
I have tried searching for answers but I am unable to understand them. Is there a simple way to do this?
The form name name="id_names" needs to be an array to allow the parameter to carry more than one value: name="id_names[]".
$_POST['id_names'] will now be an array of all the posted values.
Here your input field is multiple so you have to use name attribute as a array:
FYI: You are using mysql that is deprecated you should use mysqli/pdo.
<form method="POST" action="test.php">
<?php $sql= mysql_query("SELECT * FROM names WHERE `id` =$id ");
while ($get = mysql_fetch_array($sql)){ ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<input type="checkbox" name="id_names[]" value="<?php echo $get['id'];?>"><?php echo $get['name']; ?>
<?php } ?>
<input id="submitbtn" type="submit" value="Submit" /><br><br>
</form>
Form action: test.php (If your query is okay.)
<?php
if(isset($_POST['id_names'])){
foreach ($_POST['id_names'] as $id) {
$email = mysql_query("SELECT `email` FROM users WHERE `id` = $id");
$getemail = mysql_fetch_array($email); //Here always data will single so no need while loop
print_r($getemail);
}
}
?>

Display records from database

I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>

php script with multiple buttons

The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>

Categories