PHP changing from Single answer Option field to Hidden field - php

I have a working code where the result of a query is passed to another page. The query comes with only a single answer which is what I want. What I want however is for the field I am passing to be hidden. There is also an option field with multiple answers being passed at the same time, which has to remain. So what I'm asking is for a proper way to pas the hidden field so I don't get all the errors when I do it the way I was trying (ie The wrong way)
Here's the working code that I have. I want the first option field to be a hidden field. Any help is appreciated
$studentquery="SELECT * from student ORDER BY ssurname";<br>
//Do Query
$studentresult = mysql_query ($studentquery);
$options="";
echo "<form action='addstudenttofamily1c.php' method='POST'>";
$familychoice = "SELECT * from hostfamily WHERE hid= '".mysql_real_escape_string($hostfamily). "'";
$resultfamilychoice = mysql_query ($familychoice);
$options2="";
echo "<select name='element_1' id='element_1'>";
while ($rowfam=mysql_fetch_array($resultfamilychoice))
{
echo "<option value='$rowfam[hid]'>$rowfam[hfsurname] $rowfam[hfmname]</option><br/>";
}
echo "</select>";
echo "<select name='element_3' id='element_3'>";
while($row=mysql_fetch_array($studentresult))
{
echo "<option value='$row[sid]'>$row[sname] $row[ssurname]</option>";
}
echo "</select><input type='submit' name='submit3' value='Replace Student'>";
echo "</form>";
echo"</td>";
echo" </tr>";

This is how you can store a hidden value for a form:
<input type="hidden" name="SomeHiddenField" val="ValueOfThisField" />
To which you could code it in PHP to make it dynamic also.

Related

Using a option to hold a value in php

Using php I have manage to search and display the data i need. Now I wish for the user to be able to select a option from a drop down menu and click update. Now when I try this it doesn't update the data for some reason. Are you able to notice any errors in the code below? I have included small bits of relevant code from the php file. I'm using the 'value=1' so that when I click update the query updates using the number rather than the text as i want to update a different field than the output field. Any ideas?
if (isset($_POST['update'])) { //once the update is click this updates the gametable with the adjusted information
$updatequery = "
UPDATE
GameTable
SET
GameID='$_POST[gameid]',
GameName='$_POST[gamename]',
PubID='$_POST[Publisher]',
TimePeriodID='$_POST[TimePeriod]',
SettingID='$_POST[Setting]', //the field i want to update using the value of the named select option
MoodID='$_POST[Mood]',
GameWeaponID='$_POST[Weapon]',
GameCameraAngleID='$_POST[CameraAngle]',
GamePlayerTypeID='$_POST[PlayerType]',
GameDescription='$_POST[Description]'
WHERE
GameID='$_POST[gameid]'";
mysqli_query($dbcon, $updatequery);
echo "Record successfully updated";
};
//query that fetches data from a database and outputs.
while ($row5 = mysqli_fetch_array($result5)) {
echo "<tr> <th> Setting ID</th> </tr>";
echo "<td><select class='text-black input-button-rounded' name='Setting'>";
//the output is a different field to the one I want to update so that's why I want to use the value.
echo "<option disabled selected>" . $row5['SettingName'] . "</option>";
echo "<option class='text-black' type='text' value=1>Western</option> ";
echo "<option class='text-black' type='text' value=2>Space</option>";
echo "<option class='text-black' type='text' value=3>City</option>";
echo "<option class='text-black' type='text' value=4>Sea</option>";
echo "<option class='text-black' type='text' value=5>Apocalypse</option>";
echo "</select></td><br>";
//update button
echo "<td>" . "<input class=text-black input-button-rounded type=submit name=update value=Update" . " </td>";
I think your problem is that your select and submit button need to be wrapped in a <form> element, so that when the "Update" button is clicked, it POSTs the Setting data as well.
A few other things to note:
All the quotes for the values of the HTML attributes are missing from the line which has your button code. Also the quotes for the array keys of $_POST in your SQL statement e.g. you wrote$_POST[Mood] rather than $_POST['Mood'] Why is that?
You don't need type="text" for the option tags.
If you really want to build a secure website/app you shouldn't be using raw SQL statements. Rather, make use of either PDO or MySQLi prepared statements.

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.

PHP Variable Variables Confusion

I'm using a while loop and mysql_fetch_array to display info on a page from a database. How can I give a variable name to certain radio button elements that are echoed on the page, and then use those variable names for the radio buttons as $_POST variables when the form is submitted?
Here is the code:
session_start();
include 'acdb.php';
$prodid = $_SESSION['prodid'];
$e = "SELECT * FROM Images WHERE product_id=$prodid AND active='yes'";
$e_result = mysql_query($e,$connection) or die ('Could not get the list of all Images for this Product and Service');
$amount = mysql_num_rows($e_result);
while ($e_data = mysql_fetch_array($e_result)) {
echo "<img src='images/thumbnails/" .$e_data[1]. "' border='0'>";
echo "<label class='sel-photo'>DISPLAY PICTURE:</label> <input type='radio' name='{radio" .$e_data[0]. "}' value='1'>";
echo "<br>";
echo "<label class='sel-photo'>DO NOT DISPLAY PICTURE:</label> <input type='radio' name='{radio" .$e_data[0]. "}' value='0' checked>";
}
You will get the result you are after by doing this
echo "<label class='sel-photo'>DISPLAY PICTURE:</label> <input type='radio' name='radio" .$e_data[0]. "' value='1'>";
So assuming $e_data[0] = 'xxx' you would get html of
name='radioxxx'
PS. Its best, especially when using SELECT * to use the form $e_data['fieldname']. This is because the columns will be returned in the order they were created on the table when using *. If you/someone else decides to reorganise the table columns at a later date, they will be returned in a different order and $e_data[0] may now point to a different column.

Submit of selected on checkbox items?

I print this form in a web page with php:
<?php
include("connectDB.php");
$mySQL=new MySQL();
$queryResult=$mySQL->query("SELECT nombre, precio, id_producto FROM productos");
echo "<form action= 'checkout.php'> method=''POST'";
while($datos=$mySQL->fetch_array($queryResult))
{
$n = 1;
$nombre=$datos['nombre'];
$id_producto=$datos['id_producto'];
$precio=$datos['precio'];
echo "<h1>$nombre</h1>";
echo "<input type=\"checkbox\" name=\"$id_producto\" value=\"$nombre\"> Cantidad: <input type=\"number\" name=\"points\" min=\"1\" max=\"20\" step=\"1\" value=\"1\"><br>";
echo "<h3> Precio: $precio<br>";
}
echo "<br>";
echo "<input type=\"submit\" class=\"button\" value=\"Comprar\">";
echo "</form>";
?>
So it displays a list (which is a form within) of items which can be selected or checked, on submit, I want to do the $_POST[''] of only the checked items, how could I solve this?
When such checkboxes are printed, only those values which were checked are submitted.
If i understood you correctly you wanted to retrieve those that got posted, which you can follow with this simple method
foreach($_POST as $post_key => $post_value){
//Check that the particular input is int and numeric, since i believe the name is the id
if(is_numeric($post_key) && is_int($post_key){
//Here goes your code, $post_key is the id and $post_value is the $nombre
//Although i admit that i have no idea what nombre is since it is in another language. Forgive me if id_producto is not numeric and unique.
}
}

PHP MySQL Dropdown box

I have a dropdown box which is populated through MySQL:
echo "<form>";<br>
echo "Please Select Your Event<br />";
echo "<select>";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo "<option>";
echo $row['eventname'];
echo "</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
How do i make it that if one clicks submit it will display a value from a MySQL db
Thanks for the help
Just change your query like SELECT result FROM somedb WHERE eventname = '".$eventname."'
Then you just do: (remember to check before while has user already requested info)
The value was: <?php print $row["result"]; ?>
Remember to check $_POST["eventname"] with htmlspecialchars before inserting it to query.
1) Give a name to your <select>, i.e. <select name='event'>.
2) Redirect your form to the display page (and set method POST): <form method='POST' action='display.php'>
3) just display the selected value: <?php echo $_POST['event']; ?>
If you want to use the same page, give a name to your submit button and then do this:
<?php
if (isset($_POST['submit']))
echo $_POST['event'];
?>
Hope it helps.

Categories