Getting two variables from one radio button? - php

http://i.stack.imgur.com/Gy3o0.png
That is what the site looks like now. What I want to do is when you click on the approve registration on the table, it will extract the value of the id no and the name of that particular record. I thought i was on the right track. I knew how to get the id no. But it doesn't get the value of the name at the same time.
This is how the code looks like:
while($row = mysql_fetch_array($mayor))
{
$id = $row['identification_no'];
$name = $row['lastname'].", ".$row['firstname'];
echo "<tr>";
echo "<td><form method=post action=approvedmayor.php><input type='radio' name=id value='$id'></td>";
}
approvedmayor.php
$query = mysql_query("insert into tbcandidates VALUES ($id, '$name', 'mayor')");
if ($query)
{
echo "You appproved ";
echo $name;
}
else
echo "error";

you can try like this...
<?php
while($row = mysql_fetch_array($mayor))
{
$id = $row['identification_no'];
$name = $row['lastname'].", ".$row['firstname'];
echo "<tr><td><a href='approvedmayor.php?id=$id&name=$name'>Approve</a></td></tr>";
}
?>
in this type don't use the form, and Approve button... try this alone

Actually it is bad practice to insert that kind of data directly from POST data.
If you have all these candidates already stored in the database, you should run a SELECT query in your approvedmayor.php first, and if the candidate still exists, insert it's data to another table.
$query = mysql_query('SELECT * FROM `candidates` WHERE `id` = '.$id.' LIMIT 1');
if(mysql_num_rows($query)) {
$candidate = mysql_fetch_assoc($query);
$insertQuery = mysql_query("insert into tbcandidates VALUES ($candidate['id'], $candidate['name'], $candidate['mayor'])");
if ($insertQuery) {
echo "You appproved ";
echo $name;
} else echo "error";
} else echo 'This candidate is no longer available';

I understand your question,
But thats not the best way go ahead, Before we move let us understand some little elements functions
Radio Button : Its an input type element, that allows the user to choose only one [ 1 ] of option given list.
Check Boxes : Its an input type element, that allows the user to select n number of options or selections from give list.
Fore info - http://www.w3schools.com/html/html_forms.asp
Now comming to your question..
You need to modify your code to check boxes as below
<input type='checkbox' name=id[] value='$id'>
Notice : elements name is in Array mode.. ie whenever a user one or more than one, the values are stored in array.
Once the values are stored in array, call it / use if however you want.
For your mentioned code
echo "<form method=post action=approvedmayor.php>';
while($row = mysql_fetch_array($mayor))
{
$id = $row['identification_no'];
$name = $row['lastname'].", ".$row['firstname'];
echo "<tr>";
echo "<td><input type='radio' name=id[] value='$id'></td>";
}
echo "</form>";
approvedmayor.php
$temp_app_arr = $_POST['id'];
foreach ($temp_app_arr as $pos => $val) {
$query = mysql_query("insert into tbcandidates VALUES ('$val', '$name', 'mayor')");
if ($query) {
echo "You appproved ";
echo $name;
} else {
echo "error";
}
}
And i believe this should gonna be good code / algorithm for your project.

Related

How to add multiple checkboxes in PHP and mysql with different user id

I am developing a platform that allows me to enter presences at an event and I am stuck in one place. How can I load a series of checkboxes in order to change the user ID for each item?
A practical example, I have a table containing the name of all the people and next to it a checkbox once pressed submit php must upload the data for each user to the db.
Below the html form and the php script that manages the upload to the db.
'''php
<?php
$query = "SELECT * FROM utenti";
$ris = mysqli_query($conn, $query);
//$dati = mysqli_fetch_array($ris);
while($row = mysqli_fetch_assoc($ris)){
echo "<tr><td>".$row['nome']."</td>";
echo "<td>".$row['cognome']."</td>";
echo "<td>".$row['squadra']."</td>";
echo "<td>
<input type='checkbox' name='presenza' data-toggle='toggle' data-onstyle='success' data-offstyle='danger' data-on='Presente' data-off='Assente' value='1'>
</td></tr>";
}
'''
if (isset($_POST['submit'])){
if(!empty($_POST['presenza'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['presenza'] as $selected){
echo $selected."</br>";
$sql="INSERT INTO presenze(id_utente,settimana,presenza) VALUES ('1','1','$selected')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
}
}
'''
One of the way to get relationship between a user and a checkbox is to use user ID (or whatever unique identifier you have in your tables structure) in checkbox name e.g.:
"<input type='checkbox' name='presenza_" . $row['user_id'] . "'>"
Then on the backend you can iterate over all post params with presenza_ prefix and get user ID from it.
So your code will be a bit more sophisticated:
foreach($_POST as $param_key) {
// working only with params started with presenza_
if (0 !== strpos($param_key, "presenza_")) {
continue;
}
$presenza_parts = explode("_", $param_key);
$user_id = (int)$presenza_parts[1];
}
Keep in mind that checkbox field sends only value if it's switched on and sends nothing if it's switched off.

getting data from $_POST and Showing specific data in php

This may have been asked before but i have not been able to find it.
I have created a dropdown list in a form to show a selection from a database.
I am then sending that information via $_POST to another page.
But i am only getting the one result (eg plantID).
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
$plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
Is there any way i can send all the data via post or
can i somehow get the required PlantID from the database and show all the information for that record in a table on the second page.
Hope this makes sense to someone out there :D
Although technically you can, it is not advised nor used anywhere.
Just send an id like you do right now and then select all the data with another query based on this id.

Storing values from dropdowns to Database using PHP

I am working on an attendance module. Initially, it will show the list of all students along with a dropdown having options Present/Absent. The faculty member will choose Present/Absent accordingly & submit the same.
I am having problem in storing the corresponding values to the DB.
$sql = "select a.student_id, r.student_name, r.section, r.group_name, a.$subject from result.$batch r, attendance.$batch a where a.student_id = r.student_id AND r.section='$section'";
$c = 1;
$result1 = $result->query($sql);
if ($result1->num_rows > 0)
{
while($row = $result1->fetch_assoc())
{
echo '<tr>';
echo "<td>$c </td>";
echo "<td>{$row['student_id']}</td>";
echo "<td>{$row['student_name']}</td>";
echo "<td>{$row['section']}</td>";
echo "<td>{$row['group_name']}</td>";
echo "<td>
<select class='dropdown' id='attend' name='attend[$c]' > <option value='1'>Present</option> <option value='2'>Absent</option>
</td>";
echo '</tr>';
++$c;
}
}
else
{
echo "No Results Found";
}
Can someone please help me with the updation code. Updation is to be made in the table $batch (batch is a variable containing Table Name to use) and column $subject (contains variable name).
well, you can do one thing.when user clicks on the check-box to mark absent or present, save it into an array using Javascript like :
onclick='array.push(this.id);'
this would push the id of current element to an array in Javascript.
When you finally submit the form, just do this,
onsubmit="passValues();"
in script tag, do this
function passValues()
{
var x = array // the array to which elements were pushed
document.getElementById('someBlankElement').innerHTML = "<input type='hidden' value = 'display array here' name='get_this_name_through_php_when_form_submits' >"
}
and done !

insert multiple rows into sql database at once through php input form

I am trying to record the results of a car race, but I want to be able to enter all of the results for the race at once (rather than doing it one by one) but I just cannot seem to get it to work.
Code below:
INPUT FORM:
{
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID' value='$racerID'>";
echo"<td><input type='text' name='rank'>";
echo"<td><input type='text' name='timetaken'>";
}
SQL INSERT FORM:
$rank=$_POST['rank'];
$timetaken=$_POST['timetaken'];
$reID=$_POST['reID'];
$racerID=$_POST['racerID'];
$sql = mysql_query("INSERT INTO Racing (rank, timetaken, reID, racerID) VALUES ('$rank', '$timetaken', '$reID', '$racerID')");
$result = mysql_query($sql);
How this works is, I will select a race, then a specific event within that race, then that will display all the racers and I can enter their rank and time taken. At the same time the hidden inputs (racer no and raceevent will go into the database for each result too).
So I am trying to just enter all the ranks and timetaken for all racerIDs at once, can someone help me complete that please.
Thanks.
EXTRA:
$reID = $_GET['reID'];
$result = mysql_query("SELECT * FROM RaceEventRacer WHERE reID = $reID");
while ($row = mysql_fetch_assoc($result))
First of all u have to fix some issues with ur php form generation...
all of ur input elements are going to have the same name attribute...
correct it first
-use something like this
$cv=0;
while () {
.......................
.......................
echo "<input name='racer_'.$cv>"; // in each repetition new name value, not the same
$cv++;
}
Instead of having name='reID' you should have name='reID[]', the same withthe other fields. And the future code would look like this:
$ranks = $_POST['rank'];
$timetakens = $_POST['timetaken'];
$reIDs = $_POST['reID'];
$racerIDs = $_POST['racerID'];
$sql = "INSERT INTO Racing (rank, timetaken, reID, racerID) VALUES";
$values = array();
foreach($ranks as $key => $rank) {
$values[] = "('$rank', '{$timetakens[$key]}', '{$reIDs[$key]}', '{$racerIDs[$key]}')";
}
$sql .= implode(', ', $values);
$query = mysql_query($sql);

inserting checkbox values to database

I have a php form with some textbox and checkboxes which is connected to a database
I want to enter all the details into the database from the form.All the textbox values are getting entered except the checkbox values.I have a single column in my table for entering the checkbox values and the column name is URLID.I want to enter all the selected checkbox(URLID)values to that single column only ,separated by commas.I have attached the codings used.can anyone find the error and correct me?
NOTE: (The URLID values in the form is brought from the previous php page.those codings are also included.need not care about it)
URLID[]:
<BR><?php
$query = "SELECT URLID FROM webmeasurements";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$URLID = $row[0];
echo "<input type=\"checkbox\" name=\"checkbox_URLID[]\" value=\"$row[0]\" />$row[0]<br />";
$checkbox_values = implode(',', $_POST['checkbox_URLID[]']);
}
?>
$URLID='';
if(isset($_POST['URLID']))
{
foreach ($_POST['URLID'] as $statename)
{
$URLID=implode(',',$statename)
}
}
$q="insert into table (URLID) values ($URLID)";
You really should separate your model from the view. It's 2011, not 2004 ;o
if (isset($_POST['checkbox_URLID']))
{
// Notice the lack of '[]' here.
$checkbox_values = implode(',', $_POST['checkbox_URLID']);
}
$URLIDs = array();
while($row = mysql_fetch_row($result))
{
$URLIDs[] = $row[0];
}
foreach ($URLIDs as $id)
{
?>
<input type="checkbox" name="checkbox_URLID[]" value="<?php echo $id; ?>" /><?php echo $id; ?><br />
<?php
}
?>
<input type="submit"/>

Categories