PHP for function get POSTED values - php

I have this PHP/HTML Code:
<form method="post" action="create_quote2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Select</strong></td>
<td><strong>Image</strong></td>
<td><strong>Name</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Quantity</strong></td>
</tr>
<?php
$sql="SELECT * from products ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
$counter++;
echo '<input type="hidden" name="product'.$counter.'" value="'.$_POST["checkbox$i"].'" />';
echo '<tr>
<td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td><input type="text" name="qty" id="qty" size="20" /></td>
</tr>';
}
echo '<input type="hidden" name="counter" value="'.$counter.'" />';
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>
so when boxes are checked you go to the next page with this code:
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Trade Price</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Total Cost</strong></td>
</tr>
<?php
for($i=1; $i<=$_POST["counter"]; $i++)
{
if($_POST["checkbox$i"])
{
$counter++;
$sql="SELECT * from products where sequence = '".$i."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
echo '<tr>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td>£'.$result["tradeprice"].'</td>
<td> </td>
<td> </td>
</tr>';
}
}
?>
</table>
it works fine and selects all the correct products from the products table but i need a way to get the posted quantity values for each row.
how can i display the posted quantity values on the second page?
P.S. im not worried about SQL injection on this code...

Use <input type="text" name="qty'.$counter.'" id="qty'.$counter.'" size="20" /> on the first page
Then $_POST["qty{$counter}"] or $_POST['qty'.$i] in the relevant cell
As a side note, you may find it easier to use a HEREDOC structure so that you don't have to keep on adding quote marks to echo stuff:
echo <<<BLOCK
<tr>
<td>Image</td>
<td>{$result["title"]}</td>
<td>£{$result["saleprice"]}</td>
<td>£{$result["tradeprice"]}</td>
<td>Quantity - {$_POST['qty'.$i]}</td>
<td> </td>
</tr>
BLOCK;
I found HEREDOC a great help as the quote marks don't blend into one another so much

To get your quantities just change:
<input type="text" name="qty" id="qty" size="20" />
to
<input type="text" name="qty'.$counter.'" id="qty" size="20" />
and then reference the same way you do for other inputs.

Related

display a drop down menu based on the first selection in php

i want to display the name based on previous selection of group name commerce.
i hard code the first option and want to select the data based on previous one
iam still confused how to do this... kindly guide me
`<form action="" method="post">
<table width="39%" height="67" border="0" align="center">
<tr>
<td width="22%" height="27">Group</td>
<td width="33%"><select name="option">
<option>Commerce</option>
<option>Computer</option>
<option>Pre-engineering</option>
<option>Pre-Medical</option>
</select>
</td>
<td width="8%">Name</td>
<td width="37%">
<? if ($option=='Pre-engineering')
$sql = mysql_query("SELECT name FROM std_reg where faculty='$option'") or die(mysql_error());
while($row = mysql_fetch_array($sql)){
echo "<option>".$row[name]."</option>";}
?>
</td>
<td width="8%">Class</td>
<td width="37%"><input name="class" /></td>
</tr>
<tr>
<td height="34" colspan="4"><strong><h3>
<input type="submit" name="submit" value="SEARCH" />
</h3></strong></td>
</tr>
<br />
<br />
'
Fix the key here
echo "<option>".$row['name']."</option>";}

No error shown when updating and deleting

I have those codes that help to update and delete a row from a table:
html:
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<form action="update_del.php" method="post">
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</form>
</td>
</tr>
<tr>
PHP:
<?php
require_once('../include/global.php');
$id=$_POST['id'];
if(isset($_POST['submit1']))
{
$name=$_POST['med_name'];
$info=$_POST['info'];
$sql = "UPDATE med SET med_name='$name',
info='$info'
WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if($result){
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
if(isset($_POST['submit2']))
{
$sql = "DELETE FROM med WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if(mysqli_affected_rows($con) == 1)
{
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
?>
Nothing is updated nor deleted. And no errors are shown. If someone could help me, I think the problem is too easy but can't see it where.
You are placing <form> tag at wrong place.
Therefore, your other elements: id, med_name and info are not children of form.
And they are not getting posted.
You should start <form> before table and end after table.
Corrected Code:
<form action="update_del.php" method="post">
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
</tr>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="Update" />
<input class="imgClass_dell" type="submit" name="submit2" value="Delete"/>
</td>
</tr>
</table>
</form>
just do this -
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<form action="update_del.php" method="post">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</td>
</tr>
</form>

Display keywords from DB as Checkbox Options

I've created a database using mysqli and I have keywords stored in a table called "Keywords". I need to use the keywords stored in this table to display as options on a form in the form of checkboxes.
So, if the keyword has not been added to the db, then the checkbox on the form will not display for users to choose from. What's the best way to achieve this? My sql knowledge is basic but I can have an idea on how the query might look. Just not sure about how to display them as checkboxes.
UPDATE UPDATE UPDATE!
I was able to get my array to display with checkboxes: Here is what I did:
<?php
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
print <<<HERE
HERE;
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
}
?>
NOW MY QUESTION IS: How do I pass the checked value off to my new table (profileTable) a completely separate table containing the form results?
MY FORM CODE LOOKS LIKE THIS (Fri 8/9/13):
<div id="form1profile">
<form method="post" enctype="multipart/form-data" name="addProfile" id="addProfile" >
<fieldset>
<legend>Add/Create a Media Source Profile</legend>
<br>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th ><label>First Name</label></th>
<td><input name="FName" type="text" id="FName" size="40"></td>
</tr>
<tr>
<th >Middle Name</th>
<td><input name="MName" type="text" id="MName" size="40"></td>
</tr>
<tr>
<th ><label>Last<br>
Name</label></th>
<td><input name="LName" type="text" id="LName" size="40"></td>
</tr>
<tr>
<th><label>Prefix</label></th>
<td><input name="Prefix" type="text" id="Prefix" size="40"></td>
</tr>
<tr>
<th><label>Title
</label></th>
<td><input name="Title" type="text" id="Title" size="40"></td>
</tr>
<tr>
<th><label>Dept</label></th>
<td><input name="Dept" type="text" id="Dept" size="40"></td>
</tr>
<tr>
<th><label>Phone</label></th>
<td><input name="PH1" type="text" id="PH1" size="40"></td>
</tr>
<tr>
<th><label>Other Phone</label></th>
<td><input name="PH2" type="text" id="PH2" size="40"></td>
</tr>
<tr>
<th><label>Email</label></th>
<td><input name="Email" type="text" id="Email" size="40"></td>
</tr>
<tr>
<th><label for="ProfileImg">Photo</label></th>
<td><input name="ProfileImg" type="file" multiple id="ProfileImg" form="addProfile">
</td>
</tr>
<tr>
<th valign="top"><label class="biotextTitle">Bio/Info</label></th>
<td><textarea name="bioLinks" cols="55" rows="25" id="bioLinks" placeholder="Paste Bio/Links here..."></textarea></td>
</tr>
<tr>
<th><label class="Keywords">Keyword</label></th>
<td>
<?php include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" name="keyword[$key]" id="$key">$key<br />
HERE;
}
?>
</td>
</tr>
<tr>
<th><label class="tags">Tags</label></th>
<td><input name="Tags" type="text" id="Tags" size="40"></td>
</tr>
<tr>
<th><input name="SaveBtn" type="submit" id="SaveBtn" formaction="insertProfile.php" formmethod="POST" value="Save"></th>
<td></td>
</tr>
</table>
<br />
</fieldset>
</form>
</div>
You may add a name to your checkboxes:
<input type="checkbox" name="keyword[$key]" value="1">$key<br />
Doing so, when the page is submit you just need to iterate through the selected keywords:
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
After that you can get all the selected keywords, then you shall just insert them into your table.
change this --
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
to this
echo "<input type='checkbox' id='tag_$id'>$key<br />";
reason for doing this is so you eliminate all character combinations by just using numeric only. character combo for EX. id="Ava/Flight", id="Ava Flight"

I want to submit this form into DB php

i want to insert this below given form data to database, but i dont know how to do this, it is about inserting product to multiple warehouse with different quantities, when checkbox against the warehouse name is checked than the quantity textbox appears.
and this is my PHP code
<form name="form1" method="post" action="product_insert.php" enctype="multipart/form-data">
<table>
<tr>
<td width="274" align="right" height="25"><strong>Product Name :</strong></td>
<td><input type="text" name="wproname" value="" /></td>
</tr>
<tr>
<td width="274" align="right" height="25"><strong>Select Warehouse :</strong></td>
<td width="500"><table style="border:none">
<tr >
<td> Select </td>
<td> Name </td>
<td> Quantity </td>
</tr>
<?php
$sql="select * from tbl_warehouse where w_flag='1'";
$result=ExecuteGetRows($sql);
$num_rows=count($result); ?>
<?php for($i=0;$i<$num_rows;$i++){ ?>
<tr>
<td><input type="checkbox" name="chk<?php echo $result[$i]['w_id'];?>" value="<?php echo $result[$i]['w_id'];?>" id="chk<?php echo $result[$i]['w_id'];?>" onChange="display<?php echo $result[$i]['w_id'];?>();" />
</td>
<td><?php echo $result[$i]['w_name'];?></td>
<td><input type="text" name="qty<?php echo $result[$i]['w_id'];?>" id="qty<?php echo $result[$i]['w_id'];?>" style="display:none" />
</td>
</tr>
<script>
function display<?php echo $result[$i]['w_id'];?>()
{
if(document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="block";
}
if(!document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="none";
}
}
</script>
<?php } ?>
</table></td>
</tr>
<tr>
<td align="right"></td>
<td width="296"><input type="submit" name="Submit" value="Add New" align="middle" class="button login_btn"/></td>
</tr>
</table>
</form>
And I am using this php code for inserting data to the database
$sqls=mysql_query("select * from tbl_warehouse");
while($result=mysql_fetch_array($sqls)) {
$w = $result['w_id'];
echo $_POST['chk'.$w];
foreach($_POST['chk'.$w] as $key=>$val) {
echo $_POST['chk'.$w];
$sql = "INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
mysql_query($sql);
}
}
You don't need the leading comma in your values list in your insert statement. So change this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
To this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('".$wid."','".$wproname."','".$qty."')";

Data not loading from MySql into textboxes on button click

i have a little problem here. What i am trying to achieve is User types in unique id into textbox then presses load button, script is looking for this parameter in MySql database. When found match data should be loaded into textbox below.
UPDATED VERSION
<?php
mysql_connect ("localhost", "root","84946dff6e1") or die (mysql_error());
mysql_select_db ("employees");
if(isset($_POST["loadbtn"]))
{
$load = $_POST["loadbtn"];
$sql = mysql_query("SELECT * FROM titles WHERE emp_no = '$load' ");
$details = mysql_fetch_array($sql);
$savedTitle = $details["title"];
}
?>
<form method="post" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
<td width="150">Employee number</td>
<td><input type="text" name="load" /></td>
<td><input type="submit" name="loadbtn" value="Load" /></td>
</tr>
</table>
</form>
<br />
<br />
<form method="get" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<td width="150">Employee Title</td>
<td><input name="salary" type="text" value="<?php echo $savedTitle; ?>"></td>
</tr>
<td width="150"> </td>
<td>
<input name="add" type="submit" id="add" value="Update">
</td>
</tr>
</table>
</form>
thank you for looking and help :)
Try this
<?php
error_reporting(-1);// show all errors when debugging
// don't use these database commands
//mysql_connect ("localhost", "root","84946dff6e1") or die (mysql_error());
//mysql_select_db ("employees");
// do it this way and don't show us your database credentials.
$conn = new mysqli('localhost', "root", "84946dff6e1", 'employees');
//declare your variables so if POST isn't true you don't have errors later
$load = "some Id";
$savedTitle = "no value yet!"; // use something interesting when testing
//you want the value of the textbox which name is load
if(isset($_POST["load"]))
{
//never trust the user directly
//$load = $_POST["load"];
// do this
$load = $conn->real_escape_string($_POST["load"]);
$result = $conn->query("SELECT * FROM titles WHERE emp_no = '$load' ");
$details = $result->fetch_assoc();
$savedTitle = $details["title"];
// show me error when testing to see if something is wrong with query
echo $conn->error;
}
?>
<form method="post" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
<td width="150">Employee number</td>
<td><input type="text" name="load" value="<?php echo $load; ?>" /></td>
<td><input type="submit" name="loadbtn" value="Load" /></td>
</tr>
</table>
</form>
<br />
<br />
<form method="get" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<td width="150">Employee Title</td>
<td><input name="salary" type="text" value="<?php echo $savedTitle; ?>"></td>
</tr>
<td width="150"> </td>
<td>
<input name="add" type="submit" id="add" value="Update">
</td>
</tr>
</table>
</form>
You will want to add another if branch to handle the second form which updates the title
And remember that form is GET instead of POST like the first form.
<td><input name="salary" type="text" value="<? echo $empTitle; ?>"></td>
try change that line to this:
<td><input name="salary" type="text" value="<?php echo $empTitle; ?>"></td>

Categories