First time doing this, watch it be a really obvious/easy fix...
Basically I'm trying to insert multiple rows with a single insert query, however, I clearly fail and as such it wont work.
The form is generated by searching a table for the relevant entries it relates to, so if your in department A it will grab your name and form an input line on the form. This all works fine I can generate the form however the issue them comes when I try to insert the form.
Currently it does this thing where rather than inserting the data entered via the form it is entering the letter A across most fields (COUNT, STATUS and TYPE are immune to this as they are restricted to only certain data types via the tables set up itself).
This is currently how I have the form set in the script:
<?php
if ($action == "editrecord") {
?>
<form name="form1" method="post" action="">
<table>
<tr><td colspan="2" align="left">
<?php //Getting Title for Record
$resultTITLE = queryDB("SELECT * FROM merit_subdepts WHERE subdeptID = '$subdeptID'");
while($rowTITLE = mysql_fetch_array($resultTITLE)) {
$recordTITLE = $rowTITLE["subdeptNAME"];
echo "$recordTITLE";
}
?>
</td></tr>
<tr><td>Member Name</td><td>Choose Award</td><td>Reason</td><td></td></tr>
<?php
$resultMERIT = queryDB("SELECT * FROM merit_members WHERE memberPRIMARY = '$subdeptID'");
while($rowMERIT = mysql_fetch_array($resultMERIT)) {
$memberID = $rowMERIT["memberID"];
$memberNAME = $rowMERIT["memberNAME"];
?>
<tr>
<td><?php echo $memberNAME; ?></td>
<td><select name="count[]" class="ui-state-default">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td><input name="reason[]" type="text" id="reason" class="blackInput" value="Monthly Award <?php echo date("F, Y") ?>"></td>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php $recordTITLE; ?>">
</td>
</tr>
<?php } //end of sub-division membership list ?>
<tr><td colspan="4"><center>
<div class="butDiv"><input name="submitRECORDS" type="submit" id="submitRECORDS" value="Submit Records"></div>
</center>
</td>
</tr>
</table>
</form>
<?php
} //End of Edit Merit Records
?>
This is the insert query used:
if ($submitRECORDS) {
for($i = 0; $i<count($_POST['id']); $i++) //Getting total # var submitted and running loop
{ $fromID = $_POST['id'][$i]; //var
$fromTYPE = $_POST['type'][$i]; //var
$fromCOUNT = $_POST['count'][$i]; //var
$fromDATEA = $_POST['datea'][$i]; //var
$fromAWARDER = $_POST['awarder'][$i]; //var
$fromREASON = $_POST['reason'][$i]; //var
$fromDEPT = $_POST['dept'][$i]; //var
$fromSTATUS = $_POST['status'][$i]; //var
if ($fromID != "0") {
queryDB("INSERT INTO merit_precord (NAME,TYPE,COUNT,DATEA,AWARDER,REASON,DEPT,STATUS) VALUES ('$fromID','$fromTYPE','$fromCOUNT','$fromDATEA','$fromAWARDER','$fromREASON','$fromDEPT',$fromSTATUS')"); } }
$action="";
$msg=1;
}
Assistance much appreciated before I rip my hair out. :)
This may not be the only source of your problems (only the first I notice), but when using <?php $varname ?>, nothing will actually be printed to the screen. Instead you need to use <?php echo $varname; ?>
<td><input name="type[]" type="hidden" id="type" value="Civil">
<input name="awarder[]" type="hidden" id="awarder" value="<?php echo $usersname; ?>">
<input name="datea[]" type="hidden" id="datea" value="<?php echo $stamp83; ?>">
<input name="status[]" type="hidden" id="status" value="Check">
<input name="id[]" type="hidden" id="id" value"<?php echo $memberID; ?>">
<input name="dept[]" type="hidden" id="dept" value"<?php echo $recordTITLE; ?>">
</td>
The A you're seeing inserted is the first letter of Array. When the above values are inserted empty and you try to access them in the for loop with [], the string Array is accessed by character index like an array.
Your INSERT statements, as mentioned in comments, are vulnerable to SQL injection. Escape them with mysql_real_escape_string().
$fromID = mysql_real_escape_string($_POST['id'][$i]);
$fromTYPE = mysql_real_escape_string($_POST['type'][$i]);
$fromCOUNT = mysql_real_escape_string($_POST['count'][$i]);
...
...
$fromSTATUS = mysql_real_escape_string($_POST['status'][$i]);
Related
I am generating forms based on the data from the database. so a set of data is basically a form with a button to select the data. Based on another radio button selection, I want to do different operations on the form data. Is it possible to do it?
<form action="">
<p>Please select user gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</form>
<?php
// here I am generating a list of forms based on some information.
// $fetched_data is the returned array of data (data from db)
// how do I change the form action of the following forms based on the radio buttons above ???
if(!empty($fetched_data))
{
foreach ($fetched_data as $row)
{
?>
<form action = '??????' method =get>
<tr>
<td><?php echo $row["job_options"]; ?> </td>
<td><?php echo $row["salary_options"]; ?> </td>
<td><?php echo $row["place_options"]; ?> </td>
// buttons
<td><input type="hidden" name="getID" value=<?php echo $row["id"]; ?> /></td>
<td><input type=submit name=serialSubmit value=select id=button1 class = classSubmit /></td>
</tr>
</form>
<?php
}
}
?>
Ideally, I'll use a select rather than checkboxes
<form action="" id="form">
<p>Please select user gender:</p>
<select class="gender-select">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
</form>
Then listen on changes of that select:
<script>
const selectElement = document.querySelector('.gender-select');
selectElement.addEventListener('change', (event) => {
const selectedGender = event.target.value;
// then we can change to form's action based on the selected value here
if (selectedGender == 'male') {
document.getElementById('form').action = 'http://action/for/male.php';
return;
}
if (selectedGender == 'female') {
document.getElementById('form').action = 'http://action/for/female.php';
return;
}
// if others
document.getElementById('form').action = 'http://action/for/others.php';
});
</script>
This script can be greatly optimized and compressed, I'm just gonna leave it this way so it is much more easier to understand
How would I utilise my code for a select input type to use data within a database? I've got a connection to my datebase that works fine it terms of writing data and also retrieving data to a text box etc. Yet to workout how it works for a select type though.
Im currently using :
<select name="product" width="100" style="width: 245px">
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
How could I change my code so that when a user selects the drop down menu from the select it lists all options from a specific row in my products table?
Updated code:
if(isset($_POST['product'])){
$choice = $_POST['product'];
}
$query="SELECT * from loanproducts WHERE product = '$choice'";
while($row = fetch_assoc($query)){
echo $row[product];
}
endwhile;
if (isset($_POST['addloan'])):
$username=$_SESSION['username'];
$product=$_POST['product'];
$date_collected=$_POST['date_collected'];
$date_return=$_POST['date_return'];
$returned=$_POST['returned'];
$my_query="INSERT INTO loans VALUES ('','$username','$product','$date_collected','$date_return','$returned')";
$result= mysqli_query($connection, $my_query);
if ($result):
header ('location: homepage.php?confirm=New loan successfully created');
else :
echo "<b>This didn`t work, error: </b>";
echo mysqli_error($connection);
endif;
endif;
HTML:
<form method=post action="addloan.php" enctype="multipart/form-data">
<input type="text" name="username" autocomplete="off" size="30" value="<? php echo $_SESSION['username']; ?>" disabled>
<br><br>
<select name="product" width="100" style="width: 245px">
<option value="" disabled selected>Product?</option>
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
<br><br>
<input type="date" name="date_collected" autocomplete="off" placeholder="Collection Date?" size="30">
<br><br>
<input type="date" name="date_return" autocomplete="off" placeholder="Return Date?" size="30">
<br><br>
<select name="returned" width="100" style="width: 245px">
<option value="" disabled selected>Returned?</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<br><br>
<input type="submit" name="addloan" value="Create Loan" >
</form>
Given no code to work with (consult my "footnotes"), this is the logic you can use.
Use a WHERE clause from values pulled from a POST/GET array and name attribute(s).
HTML:
<form method="post" action="handler.php">
<select name="product" width="100" style="width: 245px">
<option value="laptop">laptop</option>
<option value="keyboard">keyboard</option>
</select>
<input type="submit" name="submit">
</form>
PHP:
if(isset($_POST['product'])){
$choice = $_POST['product'];
}
Query: N.B.: query() is an example here. Consult my "footnotes".
SELECT * FROM table where col_x = '$choice'
or actual columns:
$query = query($con, "SELECT col_1, col_2, col_3 FROM table where col_x = '$choice'");
Then you would loop over results; fetch_assoc() is an example here. Consult my "footnotes".
while($row = fetch_assoc($query)){
echo $row['col_1'] . " " . $row['col_2'] . " " . $row['col_3'];
}
Footnotes:
The API used to connect with is unknown, so you will need to escape that value and use the respective functions to connect/query with. As is the db schema being unknown.
You will need to adjust accordingly.
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
<?
$huntNum = $hunts[$i][$fm_hunt_fields['__id']];
$cookRole = 'Cook';
$cookVols = get_volunteersByHunt($huntNum, $cookRole);
foreach ($cookVols as $cVols) {
//create new cook array that only contains ID of person
$exCooks[] = $cVols['_id_acct'];
}
$guideRole = 'Hunt Guide';
$hgVols = get_volunteersByHunt($huntNum, $guideRole);
foreach ($hgVols as $hVols) {
$exHg[] = $hVols['_id_acct'];
}
$supportRole = 'General Support';
$gsVols = get_volunteersByHunt($huntNum, $supportRole);
foreach ($gsVols as $gVols) {
$exGs[] = $gVols['_id_acct'];
}
?>
<script>
var existing_cooks = <?= json_encode($exCooks); ?>
var existing_hgs = <?= json_encode($exHg); ?>
var existing_gss = <?= json_encode($exGs); ?>
</script>
<form action="" name="vol-update" id="vol-update" class="vol- update" method="post">
<input type="hidden" id="id_hunt" name="id_hunt" value="<?= $huntNum; ?>" />
<input type="hidden" name="action2" id="action2" value="1" />
<table class="tyhp-account">
<tr>
<td>
<div class="floatL cont_lft_side">
<label for="cooks"><strong>Cooks</strong></label>
<select name="cook[]" class="form-control vCooks" multiple="multiple">
<?php
foreach ($cooksArray as $cook) {
?>
<option
<?
if (in_array($cook['ID'], $exCooks)) {
echo "selected='selected'";
}
?>
value="<?= $cook['ID']; ?>" >
<?= $cook['name_last'] . $cook['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<td>
<div class="floatL cont_lft_side">
<label for="hunt_guides"><strong>Hunt Guides</strong></label>
<select name="huntGuide[]" class="form-control vHg" multiple="multiple">
<?php
foreach ($guidesArray as $guide) {
?>
<option
<?
if (in_array($guide['ID'], $exHg)) {
echo "selected='selected'";
}
?>
value="<?= $guide['ID']; ?>" >
<?= $guide['name_last'] . $guide['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<?php
$allVols = getAllVolunteers();
?>
<td>
<div class="floatL cont_lft_side">
<label for="supp"><strong>General Support</strong></label>
<select name="gsupport[]" class="form-control vSupp" multiple="multiple">
<?php
foreach ($allVols as $allVol) {
?>
<option
<?
if (in_array($allVol['__id'], $exGs)) {
echo "selected='selected'";
}
?>
value="<?= $allVol['ID']; ?>" >
<?= $allVol['name_last'] . $allVol['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td style="text-align:center">
<input type="submit" name="action" id="upVol" class="btn btn-primary" value="Update Volunteers">
</td>
<td> </td>
</tr>
</table>
</form>
//Volunteer information for hunts
<script>
var cooks = existing_cooks;
var hunt_guides = existing_hgs;
var gen_support = existing_gss;
console.log(cooks);
console.log(hunt_guides);
console.log(gen_support);
//Cooks multiSelect
$(".vCooks").val(cooks);
$('.vCooks').multiselect({
columns: 2,
placeholder: 'Select Cooks'
});
//Hunt Guide multi-select
$(".vHg").val(hunt_guides);
$('.vHg').multiselect({
columns: 2,
placeholder: 'Select Hunt Guides'
});
//General Support multi-select
$(".vSupp").val(gen_support);
$('.vSupp').multiselect({
columns: 2,
placeholder: 'Select General Support'
});
return false;
</script>
Hey guys im trying to make a quiz and currently I have ran into an issue, I have made it increment but not send the data, so I need to fix it but don't really know how to, I need the users to be able to select an option the data get sent to the next page but the page must also increase in increment.
this is what I have got
<form name="form2" method="post" >
<select name="answer">
<option value=$myrow['A']><?php echo $myrow['A']?></option>
<option value=$myrow['B']><?php echo $myrow['B']?></option>
<option value=$myrow['C']><?php echo $myrow['C']?></option>
<option value=$myrow['D']><?php echo $myrow['D']?></option>
<input type="submit" name="submit" value="Continue" onclick = "window.location.href = 'testttts.php?id=<?php echo $count?>'"
any help would be great thank you!!
Is this what you mean?
<?php
if(isset($_POST['i'])) {
$i = $_POST['i'];
} else {
$i = 0;
}
echo "increment: " . $i;
$i++;
?>
<form name="form2" method="post" >
<select name="answer">
<option value=$myrow['A']><?php echo $myrow['A']?></option>
<option value=$myrow['B']><?php echo $myrow['B']?></option>
<option value=$myrow['C']><?php echo $myrow['C']?></option>
<option value=$myrow['D']><?php echo $myrow['D']?></option>
</select>
<input type="hidden" name="i" value="<?php echo $i; ?>" />
<input type="submit" name="submit" value="Continue" />
I want to output the selected values from the right hand listbox. However I am not successful in getting the values of selected values. The error returns from the submit function of js. Can anyone tell me why does it return:
An object doesn't support this property
in the submit function? How can i get the values.. will i use ajax or just returned value in PHP?
<form name="frmListSubmit" method="post" id="myform" onsubmit="return submit()">
<table cellpadding="2" cellspacing="0" border="0" id="tblMain" class="edit">
<tr valign="top">
<td> </td>
<td>
<SELECT id="s" size="10" name="source" multiple>
<?php
while($row = mysql_fetch_assoc($getEmployeeList))
{
?>
<option name="list" value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
<?php
}
?>
</td>
</SELECT>
<td valign="center">
>>
<<
</td>
<td>
<SELECT id="d" size="10" name="destination" multiple>
<option name="list" value=""> </option>
</SELECT>
<input type="submit" name="submit" value="submit" />
</td>
</tr>
</table>
</form>
and here is the javascript code:
function listbox_moveacross(sourceID,destID)
{
var src=document.getElementById(sourceID);
var dest=document.getElementById(destID);
for(var count=0;count<src.options.length;count++)
{
if(src.options[count].selected==true)
{
var option=src.options[count];
var newOption=document.createElement("option");
newOption.value=option.value;
newOption.text=option.text;
newOption.selected=true;
try
{
dest.add(newOption,null);src.remove(count,null);
}
catch(error)
{
dest.add(newOption);src.remove(count);
}
count--;
}
}
}
function listbox_selectall(listID,isSelect)
{
var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
istbox.options[count].selected=isSelect;
}
}
function submit()
{
listbox_selectall('d', true);
return true;
}
I have something like this in PHP and it returns only one ID after submission.
if(isset($_POST['submit']))
{
echo $_POST['destination'];
}
This is the weird code:
function listbox_selectall(listID,isSelect)
{
var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
istbox.options[count].selected=isSelect;//<-- ?
}
}
Maybe, you wanted to say:
listbox.options[count].selected=isSelect;
You said, that only 1 value from "destination" is submitted to PHP. To allow multiple values to be submitted, add [] to the names of the <select>-s:
<SELECT id="s" size="10" name="source[]" multiple>
<SELECT id="d" size="10" name="destination[]" multiple>
Also, there is no need to use name attribute for options. You don't use it anywhere.