Issue with PHP Updating MySQL Database - php

I'm having issues with the form below, whenever i click on an item it catches the item and updates on the mySQL table, it only displays 1 item(how do i make it display all the items that i have checked?).
Also when i put in a quantity in it doesn't update on the mySQL table and it just says 0 when i've put multiple quantities of each item.
Are you guys able to help if that's ok?
<tr>
<th>Shirts</th>
<th>Quantity</th>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SH01" /><label for="rd1">Obey T-Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH02" /><label for="rd1">Obey Professor: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH03" /><label for="rd1">Hustle T-Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH04" /><label for="rd1">Hip-Hop Support: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH05" /><label for="rd1">90's Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH06" /><label for="rd1">DOPE Shirt: $9.99</label></div> <br />
<br />
</td>
<td>
<br />
<input type="text" name="qty" size ="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SO1" /><label for="rd1">Shoe - Red Lace: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO2" /><label for="rd1">Shoe - Red High Top: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO3" /><label for="rd1">Shoe - White: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO4" /><label for="rd1">Shoe - Black: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO5" /><label for="rd1">Shoe - Black High Top: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO6" /> <label for="rd1">Red Basketball: $19.99</label></div><br />
<br />
</td>
<td>
<br />
<input type="text" name="qty[]" size ="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SN1" /> <label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN2" /><label for="rd1">Snapback: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN3" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN4" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN5" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN6" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<br />
</td>
<td>
<br />
<input type="text" name="qty" size ="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<br />
</td>
</tr>
</tr>
</table>
<br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$con = mysql_connect('$localhost','$url','$pass');
if (!$con)
{
die("Could Not Connect: " . mysql_error());
}
mysql_select_db("$username",$con);
$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>

The first thing you need to do is update in a foreach loop so that you are inserting one row for each checked item and secondly you need to relate the quantity fields to those items by using an array.
Change your Quantity field names to an array including the code for the items:
<input type="text" name="qty['SO1']" size ="2"/><br/>
<input type="text" name="qty['SO2']" size="2"/><br/>
etc ...
Then in your PHP:
foreach ($_POST['items'] as $item) {
foreach($_POST['qty'] as $key => $value) {
$quantity = 0;
if ($key == $item) {
$quantity = $value;
}
}
$sql = "INSERT INTO Order_Information(Order_Items,Order_Quantity) VALUES ('$item', '$quantity')";
.
.
}

HTML
<input type="checkbox" name="items" value="SH01" />
<input type="text" name="qty_SH01" ..../>
PHP
foreach ($_POST as $key => $val) {
if (!preg_match("/^SH/", $key)) continue;
$qty = $_POST['qty_' . $key];
$sql = "INSERT INTO Order_Information(Order_Items,Order_Quantity) VALUES ('$key', '$qty')";
}

When using the $_POST[items], you forgot about the apostrophes - $_POST['items'].
To get all checked checkboxes, you need a loop:
if(!empty($_POST['check_list']))
{
foreach($_POST['items'] as $item)
{
....
}
}

Related

PHP multiple checkbox array with different values - Get total price of checked checkboxes

How to calculate sum of PHP multiple checkboxes values array - Total Price of checked checkboxes?
For example, result should be displayed like:
Total Price of Selected Programming Languages : C++,Java = 1200$
<form method="post" action="#">
0<input type="checkbox" name="count[]" id="count[]" value="0"/>
<input type="hidden" name="language[]" id="language" value="C"/>C [$800]
<input type="hidden" name="price[]" id="price" value="800"> <br/>
1<input type="checkbox" name="count[]" id="count[]" value="1"/>
<input type="hidden" name="language[]" id="language" value="C++"/>C++ [$700]
<input type="hidden" name="price[]" id="price" value="700"> <br/>
2<input type="checkbox" name="count[]" id="count[]" value="2"/>
<input type="hidden" name="language[]" id="language" value="Assembler"/>Assembler [$600]
<input type="hidden" name="price[]" id="price" value="600"><br/>
3<input type="checkbox" name="count[]" id="count[]" value="3"/>
<input type="hidden" name="language[]" id="language" value="Java"/>Java [$500]
<input type="hidden" name="price[]" id="price" value="500"> <br/>
4<input type="checkbox" name="count[]" id="count[]" value="4"/>
<input type="hidden" name="language[]" id="language" value="PHP"/>PHP [$400]
<input type="hidden" name="price[]" id="price" value="400"> <br/>
<input type="submit" name="sbt" id="sbt" value="SUBMIT">
</form>
This is the PHP code:
<?php
if(isset($_POST['sbt'])){
$count = $_POST['count'];
$sub_menu = $_POST['sub_menu'];
$sub_price = $_POST['sub_price'];
$sub_price1 = $_POST['sub_price'];
//$total_price = array($sub_menu => $sub_price);
foreach($count as $j)
echo $sub_menu[$j] . '['.$sub_price[$j]. ']' ;
}
?>
$items = array();
$total = 0;
foreach($_POST['price'] as $k => $price) {
if(in_array($k, $_POST['count'])) {
$items[] = $_POST['language'][$k];
$total += intval($price);
}
}
$items = implode(", ", $items);
echo $items . " = $" . $total;
You need to make sure the array indexes are the same for each group:
0<input type="checkbox" name="count[0]" value="0"/>
<input type="hidden" name="language[0]" value="C"/>C [$800]
<input type="hidden" name="price[0]" value="800"> <br/>
1<input type="checkbox" name="count[1]" value="1"/>
<input type="hidden" name="language[1]" value="C++"/>C++ [$700]
<input type="hidden" name="price[1]" value="700"> <br/>
Then you can get the price and language for each count:
$price = array_intersect_key($_POST['price'], $_POST['count']);
$language = array_intersect_key($_POST['language'], $_POST['count']);
Then implode the text and sum the price:
echo "Total Price of Selected Programming Languages: "
. implode(',', $language) . ' = $'
. array_sum(price);

Multiple checkbox values in php

I have the following code
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value"type1" /><br>
Type 2:<input type="checkbox" name="product[]" value"type2" /><br>
Type 3:<input type="checkbox" name="product[]" value"type3" /><br>
<input type="submit" value="Submit">
</form>
foreach($_POST["product"] as $value)
{
echo $value ;
}
it should return the values user have selected. But it gives only 'on' as output.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
An example code:
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="checkbox" name="product[]" value="type 1">
<input type="checkbox" name="product[]" value="type 2">
<input type="checkbox" name="product[]" value="type 3">
<input type="checkbox" name="product[]" value="type 4">
<input type="checkbox" name="product[]" value="type 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['product'])) {
foreach($_POST['product'] as $check)
{
echo $check;
}
}
?>
your value is wrong,you forgot =
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
It must be value="type1" not value"type1".
Try this
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
foreach($_POST["product"] as $value)
{
echo $value ;
}
?>
try this
<form id="myForm" action="" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST)) {
foreach($_POST["product"] as $value)
{
echo $value ;
}
}
First you forgot the = after value
Remodifying your script becomes this
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
$check = $_POST["product"]
foreach($check as $value)
{
echo $value ;
}

Display a checked checkbox in a form to form POST submission

I have this wizard that needs to display the previous posted value
index.html
<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
in the posted.php i have a similar form only this time i know the value from $_POST
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.
How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.
This would be a way to go:
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
<input type="submit" value="Submit" />
</form>

Update query not working in php

there are already values inside the texboxes which i filtered inside the table(score) but i wanna update it by changing the values inside on it and click update.
Can anyone help with my code..i wanna update the values inside the (texboxes) but my code wont work can anyone help me locate the code that messing with the program?
php code:
<?php
if(isset($_POST['update'])){
//1
$u1id = $_POST['id1'];
$u1name = $_POST['name1'];
$u1score1 = $_POST['optA1'];
$u1score2 = $_POST['optB1'];
$u1other_qual = $_POST['other_qual1'];
$u1interview = $_POST['interview1'];
$u1total = $_POST['total1'];
//2
$u2id = $_POST['id2'];
$u2name = $_POST['name2'];
$u2score1 = $_POST['optA2'];
$u2score2 = $_POST['optB2'];
$u2other_qual = $_POST['other_qual2'];
$u2interview = $_POST['interview2'];
$u2total = $_POST['total2'];
//1
mysql_query("UPDATE score SET score1='$u1score1', score2='$u1score2', total='$u1total' WHERE id='$u2id'");
//2
mysql_query("UPDATE score SET score1='$u2score1', score2='$u2score2', total='$u2total' WHERE id='$u2id'");
}
?>
html code:
<form method="post" id="frm" name="frm" action="" />
<table>
<tr>
<td>
ID: <br />
<input type="text" name="id1" value="<?php if(empty($id[0])){$id[0] = array(NULL);}else{echo $id[0];} ?>" readonly /> <br />
<input type="text" name="id2" value="<?php if(empty($id[1])){$id[1] = array(NULL);}else{echo $id[1];} ?>" readonly /> <br />
</td>
<td>
Name: <br />
<input type="text" name="name1" value="<?php if(empty($name[0])){$name[0] = array(NULL);}else{echo $name[0];} ?>" readonly /> <br />
<input type="text" name="name2" value="<?php if(empty($name[1])){$name[1] = array(NULL);}else{echo $name[1];} ?>" readonly /> <br />
</td>
<td>
Score 1: <br />
<input type="text" name="optA1" value="<?php if(empty($score1[0])){$score1[0] = array(NULL);}else{echo $score1[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optA2" value="<?php if(empty($score1[1])){$score1[1] = array(NULL);}else{echo $score1[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Score 2: <br />
<input type="text" name="optB1" value="<?php if(empty($score2[0])){$score2[0] = array(NULL);}else{echo $score2[0];} ?>" onchange="optTotal1()" /> <br />
<input type="text" name="optB2" value="<?php if(empty($score2[1])){$score2[1] = array(NULL);}else{echo $score2[1];} ?>" onchange="optTotal2()" /> <br />
</td>
<td>
Other Qualification: <br />
<input type="text" name="other_qual1" value="<?php if(empty($other_qual[0])){$other_qual[0] = array(NULL);}else{echo $other_qual[0];} ?>" readonly /> <br />
<input type="text" name="other_qual2" value="<?php if(empty($other_qual[1])){$other_qual[1] = array(NULL);}else{echo $other_qual[1];} ?>" readonly /> <br />
</td>
<td>
Interview: <br />
<input type="text" name="interview1" value="<?php if(empty($interview[0])){$interview[0] = array(NULL);}else{echo $interview[0];} ?>" readonly /> <br />
<input type="text" name="interview2" value="<?php if(empty($interview[1])){$interview[1] = array(NULL);}else{echo $interview[1];} ?>" readonly /> <br />
</td>
<td>
Total: <br />
<input type="text" name="total1" value="<?php if(empty($total[0])){$total[0] = array(NULL);}else{echo $total[0];} ?>" readonly onKeyUp="optTotal1()" /> <br />
<input type="text" name="total2" value="<?php if(empty($total[1])){$total[1] = array(NULL);}else{echo $total[1];} ?>" readonly onKeyUp="optTotal2()" /> <br />
</td>
</tr>
</table>
<input type="submit" value="update" />
</form>
It's in your submit button:
<input type="submit" value="update" />
You have given it a value, but not a name. If you change it to:
<input type="submit" name="update" value="yespleasedososir" />
it will end up in your post var
A few thoughts: 1. Have you echoed out your sql statements to see what you are getting? 2. Try add the tilde symbol before and after each column name...like score1 = 'whatever'. 3. You should really be using the mysqli statemnts.

PHP MSQL FORM Query Won't Connect

Hey guys I've been trying to do a code where I submit a form from a PHP Order Page so that it can update the MySQL Database with the required information
I've got the Order PHP Code Here:
<form action="http://zim.cs.uow.edu.au/~ga420/order.php" method="post">
<tr>
<th>Shirts</th>
<th>Quantity</th>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SH01" />
<label for="rd1">Obey T-Shirt: $9.99</label>
</div>
<br />
<input type="checkbox" name="items" value="SH02" />
<label for="rd1">Obey Professor: $9.99</label>
</div>
<br />
<input type="checkbox" name="items" value="SH03" />
<label for="rd1">Hustle T-Shirt: $9.99</label>
</div>
<br />
<input type="checkbox" name="items" value="SH04" />
<label for="rd1">Hip-Hop Support: $9.99</label>
</div>
<br />
<input type="checkbox" name="items" value="SH05" />
<label for="rd1">90's Shirt: $9.99</label>
</div>
<br />
<input type="checkbox" name="items" value="SH06" />
<label for="rd1">DOPE Shirt: $9.99</label>
</div>
<br />
<br />
</td>
<td>
<br />
<input type="text" name="qty" size="2" />
<br/>
<input type="text" name="qty" size="2" />
<br/>
<input type="text" name="qty" size="2" />
<br/>
<input type="text" name="qty" size="2" />
<br/>
<input type="text" name="qty" size="2" />
<br/>
<input type="text" name="qty" size="2" />
<br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items[]" value="SO1" />
<label for="rd1">Shoe - Red Lace: $19.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SO2" />
<label for="rd1">Shoe - Red High Top: $19.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SO3" />
<label for="rd1">Shoe - White: $19.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SO4" />
<label for="rd1">Shoe - Black: $19.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SO5" />
<label for="rd1">Shoe - Black High Top: $19.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SO6" />
<label for="rd1">Red Basketball: $19.99</label>
</div>
<br />
<br />
</td>
<td>
<br />
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items[]" value="SN1" />
<label for="rd1">Snapback Bullets: $29.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SN2" />
<label for="rd1">Snapback: $29.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SN3" />
<label for="rd1">Snapback Bullets: $29.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SN4" />
<label for="rd1">Snapback Bullets: $29.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SN5" />
<label for="rd1">Snapback Bullets: $29.99</label>
</div>
<br />
<input type="checkbox" name="items[]" value="SN6" />
<label for="rd1">Snapback Bullets: $29.99</label>
</div>
<br />
<br />
</td>
<td>
<br />
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<input type="text" name="qty[]" size="2" />
<br/>
<br />
</td>
</tr>
</tr>
</table>
<br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])){
$conn = mysql_connect('url','username','password');
if (!$con){
die("Could Not Connect: " . mysql_error());
}
mysql_select_db("db",$conn);
$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,$_POST[items]','$_POST[qty]')";
mysql_query($sql, $con);
mysql_close($con);
}
?>
Obviously my username and password I won't display but when I hit the submit button it says that it cannot connect.
You can try the form yourself on this website:
http://zim.cs.uow.edu.au/~ga420/order.php
How come it is saying that I can't connect when clearly I have been putting the right details in.
Can anyone help? this has been stressing me out :'(
Your help is appreciated greatly!!
You need to serialize all your inputs/quantities if you will have the same name "qty" in all fields. I will update my answer later if you do t get answer about this.
You had also small bugs on your code, notice this:
VALUES (null,'$_POST[items]','$_POST[qty]')"; - you were missing ' before $_POST[items]
$conn = mysql_connect('zim.cs.uow.edu.au','username','password'); - $conn with 2 "n"
So, a corrected version would be:
<?php
if (isset($_POST['submit'])){
$con = mysql_connect('url','username','password');
if (!$con){
die("Could Not Connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";
mysql_query($sql, $con);
mysql_close($con);
}
?>
Change to this. You did some mistake on $conn and $con. Then, '$_POST[items]','$_POST[qty]' in query.
if (isset($_POST['submit'])){
$conn = mysql_connect('url','username','password');
if (!$conn){
die("Could Not Connect: " . mysql_error());
}
mysql_select_db("db",$conn);
$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";
mysql_query($sql, $conn);
mysql_close($conn);
}
Hope this help

Categories