How to make radio button with mysql value? - php

I want to make radiobutton with mysql
MY CODE
But it isn't make sense (SERVER ERROR:500)
Beacause of html form tag!
So I want to ask you!
"How can I make radiobutton with mysql value?"

I don't know what you are trying to achieve but I think I know what you mean. You want to create a radio button giving it a value from your database.
You don't create a form for each radio button. Instead, create the radio buttons within a single form tag.
Something like this:
echo "<form name='' action='index.php' method='post'>";
while($row=mysqli_fetch_array($res)){
echo "<input type='radio' name='" .$q . "' value='".$q ."'>" .$q . "<br />";
echo "<input type='radio' name='" .$a1 . "' value='".$a1 ."'>" .$a1 . "<br />";
echo "<input type='radio' name='" .$a2 . "' value='".$a2 ."'>" .$a2 . "<br />";
echo "<input type='radio' name='" .$a3 . "' value='".$a3 ."'>" .$a3 . "<br />";
}
echo "</form>";

In your echo statement change the parameter " to '.
Try this it will be defiantly works.

Try like this,
while ($row = mysqli_fetch_array($res))
{
$q = $row['q'];
echo "<tr>
<td>
<form name='' action='index.php' method='post'>
<input type='radio' name='q' value='".$q."'
</form>
</td>
</tr>";
}

Are you properly able to establish a connection to the server? Just var_dump or print_r to see what you are getting from the database.

Related

Delete individual lines/rows with PHP button

What I am trying to do is have a PHP page display the contents of a MySQL database and and each line it displays, it give me a delete button so I can delete individual rows. I have the code kind of working. The code delete the lines of code, but not the one I select, instead it deletes the last line and not the one I tell it to delete.
Here is a screenshot, I know it does not look pretty, I was going to clean it up after I get the code working.
HTML:
<?php
// Delete php code
if(isset($_POST['delete_series']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
$delete = $_POST['delete_series'];
$delete_sql = "DELETE FROM `Dropdown_Series` where `id` = '$delete'";
if(mysql_query($delete_sql, $conn))
{
echo "Row Deleted </br>";
echo "$delete";
};
}
//Insert Code
if(isset($_POST['add_series']))
{
$insert_sql = "INSERT INTO dropdown_series (series) VALUES ('$_POST[add_series]' )";
if(!mysql_query($insert_sql, $conn))
{
die ('Error: ' . mysql_error());
}
echo "Series Added <br><br>";
}
?>
<br />
<h1>Add New Series Title</h1>
<h3>Add New: </h3><br />
<form name = action="add_series.php" method="POST">
<input type = "text" name = "add_series" required/><br /><br />
<input type ="submit" name="submit" value="Add">
</form>
<h3>Current Series: </h3><br />
<?php
//Delete button on each result of the rows
$query = mysql_query("SELECT id, series FROM Dropdown_Series"); // Run your query
// Loop through the query results, outputing the options one by one
echo "<form action='' method='POST'>";
while ($row = mysql_fetch_array($query))
{
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
}
echo "</form>";
?>
Your loop will result in multiple delete_series inputs - within the same form.
You could create separat forms for each option:
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
// ...
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
// ...
echo "</form>";
}
I believe that a very quick answer would be
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
echo "</form>";
}
If I were you I would prefer injecting some javascript in the logic, and perform the Delete request using an ajax call.
you can refer to this tutorial to get more understanding of what I am refering to https://scotch.io/tutorials/submitting-ajax-forms-with-jquery

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

Issues with PHP INSERT form

So I'm trying to get this php code to display a textarea and submit button so a user can submit a bio that gets inserted into a db. I have a few issues with this code:
For some reason, the text area is filled with the actual form HTML instead of just a blank box, so it shows as:
<form action='page-bio.php' method='post'>
<textarea name='author_bio' value=<input type='hidden' name='hidden' value=
<input type='submit' name='update' value=update
</form>.
I'm using wordpress, so this is a php template, but I am wondering if this code will properly get the current user and insert the user's bio into the user's bio field in the database? I have a feeling it won't, but I can't tell because I still can't get the submit button to display.
<?php
$con = mysql_connect("localhost","XXXX","XXXX");
if (!$con){
die("can not connect: " . mysql_error());
}
mysql_select_db("i5412",$con);
// Get the current user's info
$current_user = wp_get_current_user();
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE wp_usermeta SET author_bio='".$_POST['author_bio']."'WHERE
user_id=$current_user and author_bio='".$_POST['hidden']."'";
mysql_query($UpdateQuery, $con);
}
$sql = "SELECT * FROM wp_usermeta";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
echo "<form action='page-bio.php' method='post'>";
echo "<textarea name='author_bio' cols='10' rows='10' value=" . $record['author_bio'];
echo "<input type='hidden' name='hidden' value=" . $record['author_bio'];
echo "<input type='submit' name='update' value=update";
echo "</form>";
}
mysql_close($con);
?>
You need to change the following lines:
echo "<textarea name='author_bio' cols='10' rows='10' value=" . $record['author_bio'];
echo "<input type='hidden' name='hidden' value=" . $record['author_bio'];
echo "<input type='submit' name='update' value=update";
To
echo "<textarea name='author_bio' cols='10' rows='10'>" . $record['author_bio'] . "</textarea>";
echo "<input type='hidden' name='hidden' value='" . $record['author_bio']. "'>";
echo "<input type='submit' name='update' value='update'>" ;
you forgot to close half of the input tags such as echo "<input type='submit' name='update' value=update"; where you missed the ending >

Dynamic naming of radio buttons

I have "n" number of rows being returned by a sql statement. Works great. For every row returned, I would like the user to pick an option (either option 'a' or option 'b')
As standard practice a while loop is used to iterate through each row, and I attempt to name my options by the row number. However, I run into problems when I attempt to get the value of the radio button the user has selected.
I can't seem to dynamically get the value of the radio button named 1 in the first loop (or any loop with it's appropriate name) of the where clause.
Code snippet here:
$counter = 1;
while($row = mysql_fetch_array($result))
{
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $counter . ">";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['d_id'] ."> ";
echo "<br>";
echo "<input type='hidden' name='choice' value=" . $_GET["m" . $counter] . " >";
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
$counter ++;
}
updatepics.php:
Game
<?php
echo $_POST['game'];
echo " " . $_POST['player'] . " picks: ";
echo " " . $_POST['choice'] ;
?>
output of updatepics.php...
Game 1 granny picks:
Notice 'choice' is null
According to the HTML4 specification, names attributes are defined as CDATA and CDATA is defined as follows:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Modify your code to have all name values start with at least one letter.
I bailed out from dynamically creating a radio button name. Now all radio buttons have the same name through each iteration of the loop.
It seems to work passing the correct value at the correct time even if you select all of the buttons are set before hitting the submit button for each iteration.
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='mw' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $row['f_id'] . ">";
echo "<input type='hidden' name='player' value='" . $user . "' >";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='mw' value= ". $row['d_id'] ."> ";
echo " " . $row['line'];
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
updatepics.php is just as simple, and it works:
$_POST['mw']

Get and display the multiple choice that have been selected

So this is my form that let the users select input from a table :
echo"Please select questions to analyze : ";
$qsq = mysql_query("SELECT DISTINCT question_text FROM questions ");
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($qsq))
{
echo"<input type='checkbox' name='choice' value='" . $row['question_id'] . "' /> ". $row['question_text'] . '<br />';
}
echo"<br>";
echo "<input type='submit' value='submit' /></form>";
In the next.php where the form directs to,how can i display the selected one ? should i make an array or any better suggestions ?Please help me i am a beginner in this.Thanks in advance.
echo"<input type='checkbox' name='choice[]' value='" . $row['question_id'] . "' /> ". $row['question_text'] . '<br />';
Add [] to the name of checkbox.
This will post the check box as an array.
$_POST['choice'] = array('questionId1', 'questionId2');

Categories