How can i use count function with autoincrement? - php

I am working on a php situation that i have. What it should do is like. each product has a unique id value. if the product id is posted throught submit button, A count action should be activited to count the number of time that product id is posted, so increment.
I hope i exposed the situaation clearly. This is the way i thought doing it, but can't get it work:
<?php
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do;
if ($do > 1)
{
$i= $do+1;
echo $i;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/orange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="2" />
<input type="hidden" id="price" name="price" value="25" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="3" />
<input type="hidden" id="prrice" name="price" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>

If I assume the value in the hidden input #id is the times users vote the product:
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do++;
}
If I assume it incorrectly I don't understand what you want to do, sincerly.

Related

How can I update the right data by using id?

I can display first name, last name and the instrument they borrowed. There is a drop down if they want to change the instrument they borrowed. Why is my code not changing the instrument that the person is changing? What could be wrong in this code? Please help. I know my code is not agreeable to sql securement, this is just an activity. database
<html>
<head>
</head>
<body>
<form method="post">
<?php
$con=mysqli_connect("localhost","root","","borrow");
$q="SELECT students.studentid,students.studfname,students.studlname,instruments.instrumentname, instruments.instrumentid from students INNER JOIN student_instrument ON students.studentid=student_instrument.id INNER JOIN instruments ON student_instrument.checkoutdate=instruments.dateacquired";
//SELECT students.studentid,instruments.instrumentid,students.studfname,students.studlname,instruments.instrumentname from
//students INNER JOIN instruments ON students.studentid=instruments.instrumentid
$t="SELECT instrumentname from instruments where dateacquired='avail'";
$r=mysqli_query($con,$q);
$e=mysqli_query($con,$t);
while($result=mysqli_fetch_array($r)){
$id=$result['instrumentid'];
$studfname=$result['studfname'];
$studlname=$result['studlname'];
$instrumentname=$result['instrumentname'];
echo $studfname." ".$studlname." ".$instrumentname." ";
echo '<input type="hidden" name="id" value='.$id.'>';
echo '<select name="inst">';
while($f=mysqli_fetch_array($e)){
$avail=$f['instrumentname'];
echo '<option>'.$avail.'</option>';
}
echo '</select>';
echo '<input type="submit" value="submit" name="submit"><br>';
}
?>
</form>
<?php
if(isset($_POST['submit'])){
$id=$_POST['id'];
$inst=$_POST['inst'];
$p="UPDATE instruments SET instrumentname='$inst' WHERE id='$id'";
$q=mysqli_query($con,$p);
if($q){
header('location:student.php');
}
}
?>
</body>
</html>
Check the generated HTML code and look for the <form> and </form> tag. Currently you have a form similar like this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
<input type="hidden" name="id" value="8" />
<input type="submit" />
<input type="hidden" name="id" value="9" />
<input type="submit" />
<input type="hidden" name="id" value="11" />
<input type="submit" />
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
You generate only one form. When you submit that form you will send all the ids on the same field name "id". This means only the last id will be stored in $_POST['id']. This explains the "the last row is updated, not the first one".
To fix your problem you have to create individual forms so they will look similar to this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="8" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="9" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="11" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
So opening and closing the form must be happening inside the while loop.

I would use $ _POST twice in one page

I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>

How to increment a value when submit button click

I would like to know how to increase a element, each time that element is posted.
I have to use for loop for the auto increment, but i am not getting right. So any advise or guidance will be great.
Here is the way i have tried to do:
Thanks
<?php
$id=0;
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
$di = count($do);
while ($di > $id) {
$id++;
echo $id;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Try below code, counts are stored in session, but for real life app you should use database and also you should get your products from database:
<?php
// initialize counts for f102 and W122 products
if (!isset($_SESSION['count_f102']) {
$_SESSION['count_f102'] = 0;
}
if (!isset($_SESSION['count_W122']) {
$_SESSION['count_f102'] = 0;
}
if (isset($_POST['submit'])) {
$do = $_POST['prodCode'];
// increment count for product which was submitted
$_SESSION['count_'.$do] = 1+ (int) $_SESSION['count_'.$do];
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Session test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/bestorange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="f102" />
<input type="hidden" id="prodPrice" name="prodPrice" value="25" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_f102'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="prodCode" name="prodCode" value="W122" />
<input type="hidden" id="prodPrice" name="prodPrice" value="1" />
<input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_W122'] ?>" size="1" readonly="readonly" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
Are your $_POST['prodCode'] always in the form one letter + id ? If yes maybe this could help :
if (isset($_POST['prodCode'])) {
$value = $_POST['prodCode'];
// save the letter
$letter = substr($value, 0, 1);
// get id
$id = (int) substr($value, 1, strlen($value) - 1);
// get value with letter and incremented id
$valueIncremented = $letter . ++$id;
}
// with $_POST['prodCode'] = 'f102' you will get $valueIncremented = 'f103'
Hope it helps.

PHP Sticky form

When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"

Problem with displaying correct hidden field

This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>

Categories