Retrieve checkbox options after submit - php

I am building this function which echo-s all table fields and has a checkbox along with it.
I need to print this fields after submission, i have this code, and no i haven't started something, not because i don't want but because i don't have any idea how to.. so i'm asking your help.
I need to print information in report.php please help.
<form action='report.php' method='post'>
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field); // instead of $i
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . '][]" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
<input type='submit' value='Submit' />
</form>
The report.php
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
foreach($_POST['checkbox'] as $key => $value)
?>

Try this one:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}

Related

PHP Split Search Bar Keywords By Spaces

OK let me be as specific as possible.... I would like to change the way my search bar behaves. Right now the string has to match the MySQL query exactly!!! or it won't show my products.
Here is what I got:
A search for "case iphone"
searchitems.php?tosearch=case+iphone&Search=Search
Now a search for "iphone case"
searchitems.php?tosearch=iphone+case&Search=Search
Now based on how the query would work...if I type in the URL manually to this... it works how I want it to:
searchitems.php?tosearch=%iphone%&%case%&Search=Search
So how do I go about changing the URL from
searchitems.php?tosearch=case+iphone&Search=Search
to
searchitems.php?tosearch=%iphone%&%case%&Search=Search
Here is the code that I have so far:
Search Form In index.php
<form method="GET" action="searchitems.php">
<input size="50" type="text" name="tosearch" value="Search" name="keyword" id="keyword" title="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field">
<input type="submit" name="Search" value="Search" alt="Search" id="searchbutton" title="Search" class="sub_btn">
</form>
searchitems.php (Sorry if it isn't tabbed correctly just copied and pasted)
<?php
include('core/header2.php');
include ('core/connectdb.php');
if(isset($_GET['tosearch'])) {
$tosearch=$_GET['tosearch'];
$tosearch=urldecode($tosearch);
$tosearch = preg_replace('!\s+!', ' ', trim($tosearch));
$search_terms = explode(' ',$tosearch);
$search_terms[] = $tosearch;
$search_terms=array_unique($search_terms);
$query = "select * from products where ";
$query_fields = Array();
$sql = "SHOW COLUMNS FROM products";
$columnlist = $connect->query($sql);
while($arr = $columnlist->fetch_assoc()){
extract($arr);
$query_fields[] = $Field . " LIKE ('%". $tosearch . "%')";
}
$query .= implode(" OR ", $query_fields);
$results = mysqli_query($connect, $query) or die(mysql_error());
$rows = $results->num_rows;
if ($rows > 0) {
$cols = 5;
$counter = 1;
$nbsp = $cols - ($rows % $cols);
echo '<div id="content" class="float_r">';
echo "<table border=\"0\">";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if(($counter % $cols) == 1) { // Check if it's new row
echo '<tr align="center">';
}
extract($row);
echo '<td valign="top" style="padding-right:15px;">';
echo "<a href=itemdetails.php?itemcode=$item_code>";
echo '<img src=' . $imagename . ' style="max-width:120px;max-height:140px;
width:auto;height:auto;"></img><br/>';
echo $item_name .'<br/>';
echo "</a>";
echo '<div class="product_price">$'. $price .'</div>';
echo "<form method=\"POST\" action=\"cart.php?action=add&icode=$item_code&iname=$item_name&iprice=$price&ilocation=$location\">";
echo "<input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\"></form>";
echo "</td>";
if(($counter % $cols) == 0 ){
echo "</tr>";
}
$counter++;
}
if($nbsp > 0) { // Add unused column in last row
for ($i = 0; $i < $nbsp; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
}
}
echo '</table></div><div class="cleaner"></div>';
include('core/footer.php');
?>
well, brother, it's a bad approach how you doing it. But for learning, its fine. For + sign issue, you can use php urldecode, example:
<?php
$tosearch = 'a+b';
echo urldecode($tosearch);
it has its own pro/con thing but on high-level it will work for you, you can dig more into it if you like.

PHP Loop with dropdown selection and submit button?

I am trying to make a loop that gets the name some other info about a product from a sql table - MySQL table
Then Creates a page that looks like that - Webpage
So Far I have this code that does show it but I cant figure out a way how to update the name of the dropdown menu so when I press submit It writes into another SQL table the name of the product and then how many of those products did the customer selected .
<?php
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id=$value>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
echo "</tr>\n";
}
}
}
$conn->close();
?>
<html>
<body>
<form method="POST" action="#" >
<input type="submit" name="Submit" value="Submit" /><br>
</form> </body>
</html>
I know I will most likely need a second php script for the capture of the post so help with that will be greatly appreciated too .
////////////////////////////////////////////////
So up to here I got it somehow - It loops and shows all the product . When I press submit it adds only the last product in the loop and it doesn't care of the drop down menu - Just adds a "2" .
The table where the script writes is simple - 4 columns OrderID1,productid1,ProductName1 orderedqnt1
Thanks in advance .
Index.php
<html>
<body>
<form method="POST" action="insert.php" >
<?php
session_start(); // session start for Variables to add to the sql in Insert.php
include("global.php"); // Stores the session Variables
## Conection part
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
/* echo " ##dropdown## "; */
echo "<select id=" . $value . " name='dropdown'>\n";
echo "<option value=''>-</option>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
/* echo "</td>\n"; */
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n" . "<br>";
print_r ($value);
$_SESSION['GrabIDses']=mysqli_real_escape_string($conn,$row['ProductID']); //Grabs the ID of the product in Session Variable
$_SESSION['GrabNameses']=mysqli_real_escape_string($conn,$row['ProductName']); //Grabs the Name of the product in Session Variable
$_SESSION['GrabSKUses']=mysqli_real_escape_string($conn,$row['SKU']); //Grabs the SKU of the product in Session Variable
$_SESSION['Ordered']=mysqli_real_escape_string($conn,$value); //Grabs the Ordered Quantity for the product in Session Variable ????????????????
/* $GrabID = mysqli_real_escape_string($conn,$row['ProductID']);
$GrabName = mysqli_real_escape_string($conn,$row['ProductName']);
$GrabSKU = mysqli_real_escape_string($conn,$row['SKU']);
echo "----------------------"."$_SESSION['GrabSKUses']"."<br>"."$_SESSION['GrabIDses']"."<br>"."----------------------"; */
}
}
}
$conn->close();
echo "<br>";
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
Insert.php
<?php
session_start(); // session start
$getvalue = $_SESSION['GrabIDses']; // session get
$getvalue1 = $_SESSION['GrabNameses']; // session get
$getvalue2 = $_SESSION['GrabSKUses']; // session get
$ordered11 = $_SESSION['Ordered']; // session get
echo $getvalue;
echo "||";
echo $getvalue1;
echo "||";
echo $getvalue2;
echo "||"."<br>";
print_r($_SESSION);
## Connection Part
if(isset($_POST['dropdown'])) {
echo("You order was completed" . "<br>");
$sql = "INSERT INTO testorder (productid1,ProductName1,orderedqnt1) VALUES ('$getvalue', '$getvalue1','$ordered11')";
if (mysqli_query($conn, $sql))
{ echo "New record created successfully"; }
else
{ echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
mysqli_close($conn);
}
else {
echo" dhur";
}
?>
The < select>-Boxes have to be inside the form and inside the web page at all.
Enter a name-attribute in the < select>-Tag, to make the data available in the saving script vie $_POST
I am not sure, what you want to do, so I don't know if $row['ProductID'] is a reasonable name.
<html>
<body>
<form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id='$value' name='{$row['ProductID']}'>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n";
}
}
}
$conn->close();
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
</html>
<body>
<form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id='$value' name='{$row['ProductID']}'>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n";
}
}
}
$conn->close();
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
</html>
echo "<select id=$value>\n";
needs a name. So change to
echo "<select id=" . $value . " name='dropdown'>\n";
Then you need to make a second page, use
if(isset($_POST['dropdown'])) {
Then insert or update table with the info. I think you can get this part down quite easily :).
Edit:
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
You're using 2 while loops. That's 1 to many.

how to update an array from input box in php mysql

This is my code:
$uid=$_POST['uid'];
$fields = '';
if($uid==1){
//having some error in this line
foreach($query as $key => $value) {
if ($i++ != 0) $fields .= ', ';
$key = mysql_real_escape_string($key);
$value = mysql_real_escape_string($value);
$fields .= "$key = $value";
}
$query = mysql_query("UPDATE 2mcom SET $fields");
}
echo "<table>" ;
$result = mysql_query("select * from 2mcom where roll_no='138218600004'");
$i=1;
while (false != ($data = mysql_fetch_array($result, MYSQL_ASSOC)))
foreach ($data as $key => $value)
//echo "$key: $value <br />";
{
echo "<tr><td>";
echo "$key: ";
echo "</td>";
echo '<form method="post" action="">';
echo "<td>";
echo '<input type="text" name="fld'.$i.'" value="'.$value.'"/>';
echo '<input type="hidden" name="uid" value="1"/>';
$i++;
echo "</td></tr>";
}
echo "<tr><td>";
echo'<input type="submit" name="submit" value="submit"/>';
echo "</tr></td>";
echo'</form></table>';
I want to update this table 2mcom from the input text-box. Here i am fetching the field-name along with their values in input field from table "2mcom" using array.. now i want to update the field after editing.it is for result correction .
This should give your text field a name, then you can do separate UPDATE queries to update each field individually.
UPDATE, consolidated version of previous code:
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
foreach($row as $key => $value) {
$name = 'field';
$id = $row['id']; //rename this with your row id, this will give you identifiable input names
echo '<tr><td>';
echo $key . ': ';
echo '</td>';
echo '<form method="POST">';
echo '<td>';
echo '<input type="hidden" name="id[]" value="'. $row['id'] .'"/>';
echo '<input type="hidden" name="column[]" value="'. $key .'"/>';
echo '<input type="text" name="field[]" value="'. $value .'"/>';
echo '</td></tr>';
}
$i++;
}
UPDATE QUERY:
function update_table($table, $col, $value, $id) {
global $link; //mysqli object
//escape values
$value = mysqli_real_escape_string($link, $value);
$col = mysqli_real_escape_string($link, $col);
//update the database field
$query = "UPDATE `$table` SET `$col` = '$value' WHERE `roll_id` = '$id'";
mysqli_query($link, $query) or die(mysqli_error($link));
return true;
}
CONTROLLER:
if(isset($_POST['submit'])) {
$col_count = count($_POST['column']);
$field_count = count($_POST['field']);
if($field_count == $col_count) {
} else {
trigger_error("Col - Field mismatch", E_USER_ERROR);
}
for($i=0; $i<count($_POST['column']); $i++) {
if(!update_table($table, $_POST['column'][$i], $_POST['field'][$i], $_POST['id'][$i])) {
trigger_error("Error updating field", E_USER_ERROR);
}
}
}
Try that.

html table to display php photo gallery

I need to display a photo gallery in a table, I want five pictures on each line, but can't find the way to insert </tr><tr> after each fifth picture.
Here's my code:
<?php
// table name
$tbl_name=gallery1;
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows= mysql_fetch_assoc($result)){
$id = $rows['id'];
$path = $rows['path'];
$image_name = $rows['image_name'];
$title = $rows['title'];
?>
<img src="<?php echo $path."/".$image_name;?>" height="120"/>Name:<?php echo $title;?>
<?php
echo "<form action='pictry.php' enctype='multipart/form-data' method='post'>
<input name='file[]' type='hidden' value='".$image_name."' />
<input name='id' type='hidden' id='id' value='".$id."'/>
<input type='submit' name='button' id='button' value='Delete Picture' /></form>";
}
?>
Replace the line
while($rows= mysql_fetch_assoc($result)){
with
for($i = 0; $rows= mysql_fetch_assoc($result); ++$i) {
Then you place something like this into the for loop.
if($i % 5 == 0) { /* insert stuff */ }
Untested Code
<?php
$perrow = 5;
$i = 0;
echo '<table>';
echo '<tr>';
while($rows = mysql_fetch_assoc($result)) {
echo '<td><img src=[grapresultfromrows] /></td>';
++$i;
if($i == $perrow) {
$i = 0;
echo '</tr>';
echo '<tr>';
}
}
// If not a multiple of $perrow you need to add extra cells
for($i; $i < $perrow; ++$i) {
echo '<td></td>';
}
echo '</tr>';
echo '</table>';
?>

Select particular fields from a database using a checkbox in PHP

I want to select particular fields from a database using a checkbox in PHP.
My code is:
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < $numfields; $i++) // Header
{
echo '<th>'.mysql_field_name($result, $i).'</th>';
echo '<tr><td><input type="hidden" name="checkbox[]" value="false"/></td></tr>';
echo '<tr><td><input type="checkbox" name="checkbox[]" value="true"/></td></tr>';
$checkbox[$i] = isset($_POST['checkbox'][$i]) ? true : false;
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
}
I think something like this will give you better results.
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';

Categories