Text Box overrides Radio Button in MySQL - php

I am creating a web survey to collect data for a grad project/paper and am collecting the data into a MySQL database. What the webpage is supposed to do is the following: Respondents will see a word and then have to make one of four choices. Three choices are radio buttons and the fourth choice is "other" where they will have an opportunity to enter something into a text box. This seems to be a fairly typical set-up, but I am having trouble on the sql end of things.
The problem is that, although the web page seems to function properly, when I look at the database, none of the radio button values are present-- the only values that are passed to the sql server are those entered into a text box (including blank values where a radio button was checked and nothing was entered into the text box.
The code for creating the survey page is:
echo "<form action='insert2.php' method='post'>";
echo " <table border='1' cellpadding = '10' align = 'center'>
<tr>
<th>Num</th>
<th>Français</th>
<th>Choix 1</th>
<th>Choix 2</th>
<th>Choix 3</th>
<th>Choix 4</th>
</tr>";
for ($i=1; $i<=$total; $i++)
{
$query = "SELECT * FROM MyDataTable WHERE id = '$i'";
$word = $db->query($query);
$word = $word->fetch();
echo "<tr>";
echo "<td>" . "$i " . "</td>";
echo "<td width = '300'>" . $word[1] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='one' id='wa_$i' /> " . $word[2] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='two' id='wb_$i' /> " . $word[3] . "</td>";
echo "<td width = '300' align = 'center'>" .
" <input type='radio' name='word$i' value='three' id='wc_$i' /> " . $word[4] . "</td>";
echo "<td width = '300' align = 'center'>" .
" autre: <input type='text' name='word$i' id='wd_$i' /> </td>";
echo "</tr>";
}
echo "<br /><br />";
echo "</table>";
Everything works fine when I don't have the final form field (the textbox). Is it not possible to EITHER a radio button value OR a textbox value?
The 'insert2.php' code is as follows:
mysql_query("INSERT INTO `MyResultsTable` (w1) VALUES ('$_POST[word1]')");
$last_id = mysql_insert_id();
for ($i=2; $i<=$total; $i++)// This loop inserts the rest of the data into the DB
{
$temp = "word$i";
if (isset($_POST[$temp]))
{
mysql_query("UPDATE `MyResultsTable` SET w$i='$_POST[$temp]' WHERE id = $last_id");
}
Any help would be greatly appreciated. I'm new at this and have searched for a long time, but to no avail.
Regards,
Matt

Change your radios to
<input type='radio' name='word$i[]' value='one' id='wa_$i' />
<input type='radio' name='word$i[]' value='two' id='wb_$i' />
<input type='radio' name='word$i[]' value='three' id='wc_$i' />
(Notice the array brackets after the value in the "name" property)
And see if that fixes it.

Related

checkbox array with corresponding input value

I fetch data from mysql in php. List all items with checkbox and quantity (rqty) as input value where user can enter quantity. But I'm not able to relate checkbox with corresponding input value (rqty) in an array. If I select first checkbox and enter quantity it works fine but if select any other checkbox and enter quantity, it's not getting updated in database. My code is as below:
<?php
If (isset($_REQUEST['submit1'])!='') {
foreach($_POST['itm'] as $key=>$itm) {
$odate=date("Y-m-d H:i:s");
$rqty=$_POST['rqty'][$key];
$itm=$_POST['itm'][$key];
$sql1="insert into table (itemtype,itemname,qty ,retdate,reason) select itemtype, itemname, '$rqty', '$odate', 'Return' from stock where itemname='$itm';
mysql_query($sql1);
}
}
?>
<p>
<form action="" method="post" name="form1">
<?php
$query3 = "SELECT id, category, itemtype, itemname, qty, status FROM stock where location='godown'";
$comments3 = mysql_query($query3);
$qty = mysql_num_rows($comments3);
echo "Consumable Items: " . $qty;
print "<table class='blue'>
<thead>
<tr>
<td width=100>Return</td>
<td width=100>Category</td>
<td width=100>Item Type</td>
<td width=100>Item Name</td>
<td width=100>Available Qty.</td>
<td width=100>Return Qty.</td>
<td width=100>Status</td>
</tr>
</thead>";
while($row1 = mysql_fetch_assoc($comments3))
{
$it=$row1['itemname'];
print "<tbody>";
print "<tr>";
print "<td><input type='checkbox' name='itm[]' value='$it'></td>";
print "<td>" . $row1['category'] . "</td>";
print "<td>" . $row1['itemtype'] . "</td>";
print "<td>" . $row1['itemname'] . "</td>";
print "<td>" . $row1['qty'] . "</td>";
print "<td><input type='text' name='rqty[]' placeholder='Enter Qty' /></td>";
print "<td>" . $row1['status'] . "</td>";
print "</tr>";
print "</tbody>";
}
print "</table>";
?>
<input type='submit' name='submit1' value='Return' />
In inspect I can see that all rqty[] are getting posted with blank data. But in case of itm, only itm which is selected is getting posted which is correct.
enter image description here
Since I did not receive any solution for this, I changed the approach. Now handling each checkbox separately and not using any array. Added submit button in form list and posting required values to other php page where it's processed further.
That's because of your loop:
foreach($_POST['itm'] as $key=>$itm) {...
and input "itm" is checkbox: <input type='checkbox' name='itm[]' value='$it'></td>
For instance, if you have 5 checkboxes in the form-table but only 2 of them was checked - then you'll have only 2 items in array $_POST['itm'], not 5.
To fix this issue you need to loop through $_POST['rqty'] instead of $_POST['itm']

PHP Update always update empty string

let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data
<?php
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';
$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';
$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {
$sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";
mysqli_query($conn, $sql1) or die(mysqli_error($conn));
echo "yess";
}
?>
Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.
Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)
Add the name attribute (and fix type):
<input type='text' name='product_name' id='product_name' ...
Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.
Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).
You defined your data form in one form tag and submit form in the another form tag:
<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
When you submit your first form but your data is on another form
this is what is wrong

Trying to create an editable HTML table using PHP and mySQL but the table won't update

I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.

how to display four radio buttons dynamically inside a dyanamically created html table?

this my table code
$mem=mysql_query("SELECT * FROM tbl_user_profile WHERE comp_id= $cmp");
while($result=mysql_fetch_array($mem))
{
echo"<tr>" ;
echo"<td><font color='red' size=4> ".$result['fname']." </font></td>";
echo"<td><font color='red' size=4 >".$result['emp_id']."</font></td>";
inside this table in the third column i tried while loop to show radio buttons.
$query = "SELECT * FROM tbl_house where event_id = $eid";
$hid = mysql_query($query);
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<td> . <input type=\"checkbox\" name=\"q1\" value=\"$house_id\" />$house_name </td>";
}
echo "<td> <a href='house_member.php?operation=dele&id=".$result['profile_id']."'><input type='button' value='add' id='btn'></a></td>";
echo"</tr>" ;
echo"</table>";
}
its not working properly. somebody please help. i'm just a beginner in php.
waiting for a better solution.
if you want them all to be in the same column, don't put <td> around each of them, do that outside the loop. And the type of the input should be radio, not checkbox.
echo "<td>";
while ($row = mysql_fetch_array($hid)) {
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id'/>$house_name ";
}
echo "</td>";
please change the <Input type="checkbox"> to <input type "radio">
and
echo "<input type='radio' name='q1' value='$house_id'/>$house_name "; this is the right way to display html code into php
Are you trying to add radio buttons in the third column or do you want to create a column for each radio??
my guess is that you remove the from within the while loop and place the starting and ending tags outside the while loop. something like
<td>
while loop
</td>
hope this helps
You have to use the following code at the place of your while loop.....
<td>
while ($row = mysql_fetch_array($hid))
{
$house_id = $row['house_id'];
$house_name = $row['house_name'];
echo "<input type='radio' name='q1' value='$house_id' />$house_name ";
}
</td>

Loop through $_POST

I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel
For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.
Paint Edit Update Cancel
I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids.
Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?
for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{
echo "<tr>";
echo "<td width='1500'>";
echo "<input type='text' name='name' size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Update details' name='submit_update_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Delete' name='submit_delete_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;
echo "</td>";
echo "</tr>";
}
I want to do something like
foreach($_POST as $key => $value)
{
}
so that when i click on a button it performs an action depending on the category_id.
I have tried this as suggested:
echo "<input type='submit' name='submit[add_category]'" .
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";
Now in my class, i have:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
But, i am getting the error : undefined index submit
I would give the name attributes of my submit buttons using following pattern:
name="submit[which_action][which_category]"
For example for your 'Update' button for category 123:
name="submit[update][123]"
When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
Well i would use something like this:
<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>
that way when you get it to the serverside everything will be nice and tidy in nested arrays.

Categories