Why isn't my PHP form sending the information? - php

I'm trying to make a form in PHP that then passes the variables to the next page. My problem is that the drop down elements in my form aren't passing their values at all. When I do print_r($_GET) on the next page to see what values were passed through the form, only the inputs with type "text" or "radio" appear. I'm not sure what I'm doing wrong, as I've gotten this to work in the past. Can someone help me? I know that the variable names I'm pulling out of my MySQL queries are correct, as are the ids.
//begin form
print "<form action = 'restaurant_confirm.php' method = 'GET'>";
print "Restaurant Name: <input type = 'text' size = 50 name = restaurant_name></br>";
// drop down for cuisine
print "Cuisine: <select>";
while ($row = mysql_fetch_array($cuisine_result) ){
print "<option name = 'cuisine' value = '".$row['cuisine_id']."'>". $row['cuisine_name']. "</option>";
}
print "</select></br>";
// drop down form restaurant type
print "Restaurant Type: <select>";
while ($row = mysql_fetch_array($type_result) ){
print "<option name = 'restaurant_type' value = '".$row['type_id']."'>";
print $row['restaurant_type'];
print "</option>";
}
print "</select></br>";
//Check boxes for price point
//table for alignment
print "Select one price point </br>";
print "<table>";
//header row
print "<tr>";
print "<td>Price Point</td>";
print "<td>Value</td>";
print "<td>Select</td>";
print "</tr>";
while ($row = mysql_fetch_array($price_result)) {
print "<tr>";
//prints the price point
print "<td>";
print $row['price_point'];
print "</td>";
//prints the value
print "<td>";
print $row['price_value'];
print "</td>";
print "<td>";
// radio button to choose
print "<input type = 'radio' name = 'price_selection' value = ".$row['price_id'].">";
print "</tr>";
}
print"</table></br>";
//Check boxes for gluten type
//table for alignment
print "Select one gluten type</br>";
print "<table>";
//header row
print "<tr>";
print "<td>Gluten Type</td>";
print "<td>Select</td>";
print "</tr>";
while ($row = mysql_fetch_array($gluten_result)) {
print "<tr>";
//prints the gluten type
print "<td>";
print $row['gluten_type'];
print "</td>";
print "<td>";
// radio button to choose
print "<input type = 'radio' name = 'gluten_selection' value = ".$row['gluten_type_id'].">";
print"</td>";
print "</tr>";
}
print "</table></br>";
//submit button
print "<input type = 'submit' value = 'Add'>";
print "";

Your select has no name, which is needed.
Change the selects to <select name="whatever"> and you will be able to get them via $_GET["whatever"] on the next page.

Your input and select fields do not have name parameter specified. name defines the variable name.
So <input name="blah" /> will be $_GET['blah']

Related

Display selected category on the top of drop down list

I have a updateProducts page where I want to show a category drop down and within the drop down I want that category pertaining to a particular product must be shown on the top of the list. Which means, the drop down must show all the categories but the selected category should be shown on the top of the list.
Code:
$pid=$_GET['id']; //getting the id from another page
$data=mysqli_query($con,"SELECT * FROM products WHERE pid='$pid'");
while($row=mysqli_fetch_array($data))
{
echo "<td><img src='../product_images/".$row['image']."' height='66px' width='66px' ></td>";
echo "<td><input type='text' value='".$row['title']."'></td>";
echo "<td><input type='text' value='".$row['body']."'></td>";
echo "<td><input type='text' value='".$row['cost']."'></td>";
}
$data2=mysqli_query($con,"SELECT * FROM category");
echo "<td><select>";
echo "<option>Category</option>";
while($row2=mysqli_fetch_array($data2))
{
echo "<option>".$row2['category']."</option>";
}
echo "</select></td>";
First loop the results and seperate the one that goes on top, then output the select.
$cid=??;
$display_top;
$display_rest=array();
while($row2=mysqli_fetch_array($data2))
{
//if id == on_top_id set the variable on_top
if($row2['id'] === $cid){
$display_top= "<option>".$row2['category']."</option>";
}
//the rest
else{
$display_rest[] = "<option>".$row2['category']."</option>";
}
}
//output the select
echo "<td><select>";
echo "<option>Category</option>";
//first echo the id_on_top
echo $display_top;
//echo the rest
echo implode('',$display_rest);
echo "</select></td>";

PHP & SQL - Update query fails to update value

I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.

Is it possible to pass a variable of value from associative array, using hidden input, to another page?

I am trying to get the $w['id'] value for a mysql row and pass it to another page using a hidden input field and variable $blog_id. Here is the code for the first page:
$query = "SELECT * FROM blog WHERE username = '$username' ORDER BY created_date DESC;";
$result = $mysqli->query($query);
$rows = resultToArray($result);
// var_dump($rows);
foreach($rows as $r => $w) {
echo "<tr>\n";
echo "<td>{$w['title']}\n";
echo "<td>{$w['id']}\n";
echo "<td>{$w['created_date']}</td>\n";
echo "<td>{$w['updated_date']}</td>\n";
echo "<td style=\"border:none\">{$w['template']}</td>\n";
$blog_id = $w['id'];
echo "<input type=\"hidden\" name=\"id\" value=\"$blog_id\" />\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"view\">View</button></td>\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"edit\">Edit</button></td>\n";
echo "<td style=\"border:none\"><button type=\"submit\" name=\"delete\"><font color=\"red\">Delete</font></button></td>";
echo "</tr>\n";
}
?>
In my html table the values echo correctly but when I try to grab the $w['id'] value and pass it using, say, the edit button to the next page the value is always the lowest id value in the mysql table.
The code for the critical part in the second page is:
sec_session_start();
$username = $_SESSION['username'];
// $blog_id = $_SESSION['blog_id'];
if(isset ($_POST['edit'])) {
$blog_id = $_POST['id'];
$result = $mysqli->query("SELECT * FROM blog WHERE id = '$blog_id'");
if($result->num_rows > 0) {
$rows = resultToArray($result);
foreach($rows as $r => $w) {
?>
<body id="blog_editor">
<?php var_dump($_POST); ?>
The value of var_dump($_POST) is always ["id"]=>string(2) "43" whereas the foreach loop on the first page produces lots of different IDs.
Does anybody know what I am doing wrong or have an alternative way of doing the same kind of thing which might work?
You use a foreach and echo many different id.
I don't see any SUBMIT button or <form> tag.
What you can do is, inside the foreach loop, create a form with is own button:
<?php
foreach($rows as $r => $w) {
echo '<form action="page.php" ...>
echo ....
echo '<input type="submit">
}
?>
OR if you have multiple results inside one only form then you have to change the name of your element (which will be later give the value to `$_POST['id']) every time the loop loops.
To do this change this line:
echo "<input type=\"hidden\" name=\"id\" value=\"$blog_id\" />\n";
1) to this:
echo "<input type='hidden' name='id".$blog_id."' value='$blog_id' />\n";
and then when you call $_POST you'll have $_POST['idXX'] where XX = number of the ID
2) or TO this to create an array with all IDs on it:
echo "<input type='hidden' name='id[]' value='$blog_id' />\n";
and then $_POST['id'] will be an array

How do I update the Table for entering marks of students foreach student Id each mark in php mysql

Hi I have senario where i need to update the MySQL table which has student ID and Student Marks. Now the Student ID is unique here. How do I use only one form to update all the students marks.
$result= mysql_query("SELECT fname,usn FROM student where branch='$branch' and section='$section' and semester='$semester'") or die(mysql_error());
echo "<form action=\"marks.php\" method=\"POST\">";
echo "<table border='6' width='500' cellspacing='10' cellpadding='10' style='font-size:14px'>";
echo "<caption>";
echo "<b style='font-size:18px'>Internal</b> ";
echo "<b style='font-size:18px'>";
echo $internal;
echo "</b>";
echo " <b style='font-size:18px'>marks of</b> ";
echo "<b style='font-size:18px'>";
echo $subject;
echo "</b>";
echo "</caption>";
echo "<tr><th>USN</th><th>FNAME</th><th>MARKS</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['fname'];
echo "</td><td>";
echo "<input name=\"internal\" type=\"text\" value=\"\" >";
echo "</td></tr>";
echo "</table>";
echo "<input name=\"update\" id=\"update\"type=\"submit\" value=\"submit \"align=\"middle\" >";
Each time you print out the input field for the student mark, you could print a name attribute for the input field that uniquely identifies it - using an integer counter, for example, from 0 to N-1, where N is the number of students. You could also pass a hidden input field with the total number of students. The PHP code that receives the data then uses the hidden input field to loop over the input fields in the form data.
For example, if the input fields end up being named FIELD0 ... FIELD20, then the hidden input field has value 21, so the PHP code simply says this:
$marks = array();
for ($i = 0; $i < $NUMOFMARKS; $i++) {
$marks[] = $POST['FIELD' . $i];
}
Then build your SQL query from the array of marks.

populate php form fields with mysql array (on click)

I'm a php/mysql newbie, working on an invoicing application. I have a summary.php page, in which I'm using php to query a db and render a table with the retrieved data:
< ?php
mysql_connect ... [ snip ] ...
// retrieve all data in the 'Invoices' table ...
$result = mysql_query("SELECT * FROM Invoices") or die(mysql_error());
// ... and store each record in a variable:
$row = mysql_fetch_array($result);
and my table is:
<table class="record-summary">
<form action="/edit.php" method="post">
<tr>
<th ...
[ snip: more table headers ] ...
<?php
$controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">". $row['ID']. "</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
[ snip: more data cells ] ...
echo "<td class=\"controls\">". $controls . "</td>";
The last cell (td.controls) in each row contains two submits, one linking to a detail view (details.php), the other to a form page (edit.php). My objective is to populate either page with the values of the record/row. Where I need help is how exactly to use the $row variable to carry this array to e.g., edit.php, and populate the fields in the form (actually I'm fairly certain I have the syntax for the form inputs' value attributes). Both details.php and edit.php have all the data fields in the record represented, though in summary.php only a portion of the fields are displayed (ergo, 'summary.php'). So my questions are:
How do I ensure that when either button is clicked, the $row contains the values of the given record, and how to push the array into either details.php or edit.php?
Many thanks in advance,
src
You can pass the ID to edit.php and details.php but without the form just with two links to edit.php?id=and details.php?id=:
<?php
// $controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />"; <-- is not necessary
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">details.php | edit.php</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
// [ snip: more data cells ] ...
// echo "<td class=\"controls\">". $controls . "</td>"; <-- is not necessary
You can grab the data using the id:
//....
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die("There is no id.");
}
$sql = sprintf("SELECT * FROM Invoices WHERE id = %s", $id);
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<input type=\"hidden\" name=\"row\" value=\"" . $row . "\"/>";
Include this in your loop and your form's "action" script will be able to access the corresponding $row variable via $_POST['row'].

Categories