I have a php form that gets dumped into a MySQL table. I create an array that is populated from a Product table using php.
Queries the table to get the values for the array - works fine.
<?php
function getProducts ($id)
{
$sql =("SELECT * FROM products where products.CAT_ID = $id AND DISABLED ='NO'");
$result = mysql_query($sql);
if ($result > 0)
{
$products = array();
$x=0;
while($row = mysql_fetch_array($result))
{
$products[$x]= $row;
$x += 1;
}
return $products;
}
else return false;
}
function newProd(){
global $products;
}
?>
Then I use the above array to create labels in a javascript accordion. Next to the labels -are select boxes. (Removed unnecessary page elements.. ) Html/php form below that displays the above array.
<?php include('includes/query.php');?>
Inventory
<body onload="toggleField()">
<div id="container" class="container_16">
<form name="form1" id="form1" action="insert.php" method="post" enctype="multipart/form-data" style="height: inherit">
<div id="Info" class="grid_16">
<fieldset>
<label id="Label1">
<span class="Labels">Store Information</span> <br>
</label>
<div>
<label for="name">
<span class="Labels"> Name :</span> </label>
<input id="name" name="name" type="text">
</div>
</fieldset>
</div>
<hr id="SpacePadding">
<hr class="SpacePadding">
<div class="clear"></div>
<!-- Start of the Accordion Container-->
<!-- Start array, make sure Category Array isn't false -->
<?php if($categoryArray !=false):?>
<!-- create the variables for modulus -->
<!-- Start the foreach loop for the category Array -->
<?php $cataCounter = 0; ?>
<?php $closeSection = false; ?>
<?php foreach($categoryArray as $row):?>
<?php if($cataCounter % 2 == 0): ?>
<div class="push_1 grid_8">
<?php $closeSection = false; ?>
<?php else: ?>
<div class="grid_8">
<?php $closeSection = true; ?>
<?php endif; ?><!-- insert modulus-->
<div class="AccordionTitle"><?php echo $row['CATEGORY_NAME'];?></div>
<div class="AccordionContent">
<table>
<?php foreach(getProducts($row['CAT_ID']) as $row):?>
<tr>
<th class="boldLabels"><?php echo $row['PRODUCT_NAME'];?>
<span class="label"></span></th>
<th><span class="Labels">Count:</span></th>
<td>
<select name="selCount[]" onChange="toggleField(this.value);" class="Listbox">
<option value="Select">Select One</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="Other">Other(Specify)</option>
</select>
<input type="text" name="otherCount[]" class ="Listbox" style="display: none;">
</td>
<td><span class="Labels">Need:</span></td>
<td>
<select name="selNeed[]" onChange="toggleField2(this.value);" class="Listbox">
<option value="Select">Select One</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="Other">Other(Specify)</option>
</select>
<input type="text" name="otherNeed[]" class ="Listbox" style="display: none;">
</td>
</tr>
<?php endforeach;?>
</table>
</div>
</div>
<?php if($closeSection): ?>
<div class="clear"></div>
<?php endif; ?>
<?php $cataCounter += 1; ?>
<?php endforeach;?>
<?php endif;?>
<input name="Submit" type="submit" value="submit">
</div>
</form>
<br>
</div>
<footer>
<div align="center">
<ul>
<li>Home</li>
<li>Daily Inventory Counts</li>
<li>Contact System Admin</li>
</ul>
</div>
</footer>
</body>
When submitted, I want the Product_Name to go inside a new table with the correlating select option.
(i took out the database connection info out.)
<?php
//Assign array
$SelCount = $_POST['selCount'];
$SelNeed = $_POST['selNeed'];
$otherNeed = $_POST['otherNeed'];
$otherCount = $_POST['otherCount'];
$limit = count($SelCount);
for($i=0;$i<$limit;$i++) {
$SelCount[$i] = mysql_real_escape_string($SelCount[$i]);
$SelNeed[$i] = mysql_real_escape_string($SelNeed[$i]);
$otherNeed[$i] = mysql_real_escape_string($otherNeed[$i]);
$otherCount[$i] = mysql_real_escape_string($otherCount[$i]);
if (($SelCount[$i]) !="Select" ) {
$sql = ("INSERT INTO inventory ( STORE_ID, REQUESTOR, ITEM_COUNT, ITEM_NEED, NEED_COUNT, OTHER_COUNT)
VALUES ('$_POST[store]', '$_POST[name]', '".$SelCount[$i]."', '".$SelNeed[$i]."', '".$otherNeed[$i]."', '".$otherCount[$i]."')");
if(mysql_query($sql ,$db))
echo "$i successfully inserted.<br/>";
else
echo "$i encountered an error. <br/>";
}
}
?>
Here's what I would do:
function getProducts($id)
{
$sql = 'SELECT * FROM products where products.CAT_ID = ' . $id . ' AND DISABLED = "NO"';
$result = mysql_query($sql);
if($result !== false) {
while($row = mysql_fetch_array($result)) {
$products[]= $row;
}
} else {
$products = false;
}
}
Then you can print it out via:
if($products = getData(id)) {
foreach($products as $product) {
echo $product['database_field_name'];
}
}
To see them all for debugging purposes, you can use this:
echo '<pre>' . print_r($products, true) . '</pre>';
Also, you have to sanitize $id somewhere using:
http://php.net/manual/en/function.mysql-real-escape-string.php and perhaps use intval() if it's an integer.
Store data in session:
session_start();
$_SESSION['name'] = $product['database_field_name'];
And then on the insert.php page you can read the data via:
echo $_SESSION['name'];
Then after you don't need it anymore, you can unset it:
unset($_SESSION['name']);
Related
So, I have $dp->isi = "1,2,3,4"
<select name="isi[]" class="form-control select2" multiple="multiple" data-placeholder="Choose"
style="width: 100%;">
<?php foreach ($data->result() as $db) : ?>
<option value="<?php echo $db->id;?>"
<?php if ($db->id == explode(',',$dp->isi)) { ?>
selected="selected"<?php } ?>>
<?php echo $db->name; ?>
</option>
<?php endforeach; ?>
</select>
Using codeIgniter, I want $dp->isi value to be selected on multipleselect.
I have created a form which allows users to edit existing data within a database, I pull information from one page to the next to populate text boxes and select boxes. I have managed to populate the select box with the correct value but when the update statement goes through it deletes or doesn't recognize the pre-existing value. Can anyone help?
if (isset($_POST['submit'])) {
// Process the form
if (empty($errors)) {
$id = $brand["brandId"];
$brandName = mysql_prep($_POST["brandName"]);
$brandCategory = mysql_prep($_POST["brandCategory"]);
$brandKeyword = mysql_prep($_POST["brandKeyword"]);
$addedBy = mysql_prep($_SESSION['username']);
$query = "UPDATE brands SET ";
$query .= "brandName = '{$brandName}', ";
$query .= "brandCategory = '{$brandCategory}', ";
$query .= "brandKeyword = '{$brandKeyword}', ";
$query .= "addedBy = '{$addedBy}', ";
$query .= "dateTime = CURRENT_TIMESTAMP ";
$query .= "WHERE brandId = '{$id}' ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "Brand updated.";
redirect_to("search.php");
} else {
// Failure
$_SESSION["message"] = "Brand update failed.";
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
<?php $layout_context = "user"; ?>
<?php include("../includes/layouts/header.php"); ?>
<?php include("../includes/layouts/navigation.php"); ?>
<div class="section">
<div id="message">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
</div>
<form id="edit_brands" action="edit_brands.php?id=<?php echo urlencode($brand["brandId"]); ?>" method="post">
<h2>Edit Brand Information: <?php echo htmlentities($brand["brandName"]);?></h2>
<p>
<label for="bname">Brand Name:</label>
<input class="textbox" id="bname" type="text" name="brandName" value="<?php echo htmlentities($brand["brandName"]); ?>" autofocus/>
</p>
<p>
<label for="bcategory">Brand Category:</label>
<select class="textbox" id="bcategory" type="text" name="brandCategory">
<option value=""><?php echo htmlentities($brand["brandCategory"]); ?></option>
<option value="Animation">Animation</option>
<option value="Automotive">Automotive</option>
<option value="Beauty and Fashion">Beauty & Fashion</option>
<option value="Comedy">Comedy</option>
<option value="Cooking and Health">Cooking & Health</option>
<option value="DIY">DIY</option>
<option value="Fashion">Fashion</option>
<option value="Film and Entertainment">Film & Entertainment</option>
<option value="Food and Drink">Food & Drink</option>
<option value="Gaming">Gaming</option>
<option value="Lifestyle">Lifestyle</option>
<option value="Music">Music</option>
<option value="News and Politics">News & Politics</option>
<option value="Science&Education">Science & Education</option>
<option value="Sports">Sports</option>
<option value="Technology">Technology</option>
<option value="Television">Television</option>
</select>
</p>
<p>
<label for="bkeyword">Brand Keyword:</label>
<textarea class="FormElement" id="bkeyword" name="brandKeyword" id="brandKeyword" placeholder=""><?php echo htmlentities($brand["brandKeyword"]); ?></textarea>
</p>
<p>
<input type="submit" class="button" name="submit" value="Edit Brand" onclick="return confirm('Do you wish to edit brand?');"/>
</p>
<p>
Cancel
</p>
</form>
</div>
</div>
The best way is to build the select from an array.
For instance:
<?php
$array = array('Animation', 'Automotive', 'Beauty and Fashion ', ...);
echo '<select class="textbox" id="bcategory" type="text" name="brandCategory">';
foreach ($array as $value){
if($value == htmlentities($brand["brandCategory"]){
echo '<option value='.$value.' selected>'.$value.'</option>';
}else{
echo '<option value='.$value.'>'.$value.'</option>';
}
}
echo '</select>;
This way you can check if the value in the array is the same that the one recieved by post and then add the selected attribute to the option tag.
I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.
Thanks a lot.
Current Code:
<?php
include 'database/connectdatabase.php';
if(isset($_POST['select_db']))
{
$select_db = $_POST['select_db'];
$selectdb_query = 'SHOW TABLES FROM $select_db';
$query_select = mysql_query($selectdb_query,$connectDatabase);
if(!$query_select)
{
echo 'NO table selected!';
}
?>
<form method="POST" action="selecttable.php" autocomplete="off">
<select name="select_db">
<option selected="selected">Select Database</option>
<option>section_masterfile</option>
</select>
</form>
<form method="POST" action="#" autocomplete="off">
<?php while ($row = mysql_fetch_row($query_select)) {
$num_row = mysql_num_rows($row);?>
<select name="select_table">
<option selected="selected">Select Table</option>
<?php for($i=0;$i>=$num_row;i++){?>
<option><?php echo $row[0];?></option>
<?php}?>
</select>
<?php}?>
</form>
The problem is that you're already fetching rows yet you haven't even submitted the form yet.
I suggest restructure you logic this way:
$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
$select_db = $con->real_escape_string($_POST['select_db']); // escape the string
$query = $con->query("SHOW TABLES FROM $select_db");
while($row = $query->fetch_assoc()) {
$tables[] = $row['Tables_in_' . $select_db]; // use associative instead
}
}
?>
<form method="POST" autocomplete="off">
<select name="select_db" onchange="this.form.submit();">
<option disabled selected>Select Database</option>
<option>test</option>
</select>
<br/><br/>
<select name="select_table">
<?php foreach($tables as $table): ?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php endforeach; ?>
</select>
</form>
Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.
error_reporting(E_ALL);
ini_set('display_errors', '1');
<select name="select_table">
<option selected="selected">Select Table</option>
<?php while ($row = mysql_fetch_row($query_select)) { ?>
<option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php } ?>
</select>
Try This
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select))
{
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Consider this code:
<form method="POST" action="#" autocomplete="off">
<select name="select_table">
<option selected="selected">Select Table</option>
<?php
while ($row = mysql_fetch_row($query_select)) {
?>
<option><?php echo $row[0];?></option>
<?php
}
?>
</select>
</form>
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>
When we change the name="quantity" and the $product['price'] value will changing too. Here will have dynamic quantity and price. How to do that using jquery/javascript.
<?php
$select = "SELECT * FROM `products` ORDER BY `id` DESC LIMIT 10";
$query = mysql_query($select) or die(mysql_error());
?>
<ul>
<?php while($product = mysql_fetch_array($query)) { ?>
<li>
<p><?php echo $product['name'];?></p>
<p> Quantity
<select name="quantity">
<?php for($i=1;$i<=$product['quantity'];$i++) { ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
</p>
<p><?php echo $product['price'];?></p>
</li>
<?php } ?>
</ul>
Let me know :)
Using jQuery:
$(document).ready(function() {
$("#quantity_select").bind("change", function() {
selected_option = $("option:selected").val();
$("#counter").html(selected_option);
});
});
...
<select id="quantity_select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="counter"></div>
You can use text() instead of val() if you want to see actual text instead of value attribute .