I have a query to display all the records of the ebooks table from the database.
I displayed it in a table with a checkbox from the first column.
I'm trying to save the rows from the selected checkbox in the table width the Name of School but I don't have any idea.
I'm just a beginner in php, any help is appreciated. Here is my codes:
HTML codes:
$result1 = mysql_query("SELECT * FROM school GROUP BY SCHOOL_NAME");
while($row1=mysql_fetch_array($result1)) {
?>
<option><?php echo $row1['SCHOOL_NAME'];?></option>
<?php } ?>
</select>
<table class="table table-striped table-hover table-bordered datatable">
<thead style="background: #a83535; color: black;">
<tr>
<th style="width: 50px;"></th>
<th style="color: #3b3b3b;">Ebook Title</th>
<th style="color: #3b3b3b;">Category</th>
<th style="color: #3b3b3b;">File Name</th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
$count = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td>
<center><input type="checkbox" name="check[]" ></center>
</td>
<td><input type="" name="ebookname[]" value="<?php echo $row['EBOOK_TITLE'];?>"></label></td>
<td><input type="" name="ebookcategory[]" value="<?php echo $row['EBOOK_CATEGORY'];?>"></label></td>
<td><input type="label" name="ebookfilename[]" value="<?php echo $row['EBOOK_FILENAME'];?>"></label></td>
</tr>
<?php } //end of while loop ?>
</tbody>
</table>
<button type="submit" class="btn btn-hg btn-primary" name="AddEbooks">Submit</button>
I want to save the rows selected by the checkbox to database:
Table: school_management
Columns: ID_NUMBER, SCHOOL, EBOOK, FILE_NAME, DATE_ASSIGN
Values (from the HTML table to database): ('','$school_ebook','$ebookname','$ebookfilename','')
Here is the screenshot:
First, you will need to assign an ID number to the checkboxes so that you can differentiate each record. Using the ID_NUMBER would be ideal here. In your current script where you are displaying the checkbox, your line that reads this:
<center><input type="checkbox" name="check[]" ></center>
Should include the ID_NUMBER like this in the name. Additionally, a value should be assigned to the checkbox so we can verify later.
<center><input type="checkbox" name="check[<?php echo $row['ID_NUMBER'];?>]" value="1"></center>
Now you can go ahead and set up something that gets triggered when your form is submitted. You can essentially loop through each record and check it against the "check[ID_NUMBER]" checkbox to see if the value is 1. If the value is 1 then the record had been checked to save and you can then carry on saving the record.
How you want to handle the form is up to you - you could do AJAX to avoid reloading the entire page or you could do a simple form action="page_name_here.php" and handle the submission there. If you go about doing a simple form post without AJAX, it could be something like this -
Current page form tag:
<form name="" action="page_name_here.php" method="POST">
If you want to use the school_ebook var you need to assign the select options for the school_ebook a value:
<option value="<?php echo $row1['SCHOOL_NAME']'?>">
page_name_here.php:
<?php
/* Get the school name */
$school_ebook=$_POST["school_ebook"];
/* Query your books DB to get the records */
$result=mysql_query("SELECT * from ebooks ORDER BY EBOOK_TITLE");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
$bookID=$row['ID_NUMBER'];
/* Get the Book ID and now we have to fetch from $_POST the value from the form */
if (array_key_exists($bookID, $_POST["check"])) {
$ischecked=$_POST["check"][$bookID];
/* See if this has a value of 1. If it does, it means it has been checked */
if ($ischecked==1) {
/* It is checked, so now in this area you can finish the code to retrieve the data from the row and save it however you like */
}
}
}
?>
Once completed with your inserts, etc., it should accomplish what you need.
Related
I have a dynamic table which is set inside a foreach, so for each item of the array fetched create a new row. I have in the last column a button for each row. When clicking that submit button I am suppose to receive the id of that in PHP. Submission is being done correctly, but I am receiving the wrong id in PHP. Its basically taking the last id of the array when submitting. Any idea why?
Here is the table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
When clicking on see_rma this is what I receive in PHP:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
If I try printing $cases["Dev_Id"]; inside loop in the table, it prints perfectly fine, so it prints the Dev_Id of each row correctly. So, that means there is nothing wrong with the array or data. I don't why is this happening but it's for sure the first time I am having this issue.
I do this in many other tables but for some reasons in this one its not working properly.
You have multiple <input> elements with the same name within your form, and all of them are going to be submitted when you submit the form, but PHP can only get one of them. That's why you end up with only the last one in $_POST.
It looks like you should be able to fix this by just moving some attributes from the hidden input into the button (replacing the hidden input).
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
Only the button that was clicked will be submitted. Note that after changing the name of the button, you won't have see_rma in $_POST any more, so if you have any code that depends on that you'll need to change it to look for the other name instead.
I am trying to make a rating system using multi-select dynamically generated from rating text on the database. The final result should look like this: Rating Service but now I need to save selected option after save is clicked.
in the database there is two table the first one have 2 column the first is the id and the second is the text of the rate
the second table is where i wont to save the selection that the user chose using php
page code
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>About</th>
<th>Provider</th>
<th>Rating</th>
</tr>
</thead>
<?php
$query_rate_text = "SELECT * FROM rateing_text";
$selecting_rates = mysqli_query($con,$query_rate_text);
$i = 0;
while($row_rate = mysqli_fetch_assoc($selecting_rates)){
$rate_id= $row_rate['rate_id'];
$rate_text= $row_rate['rate_text'];
$i++
?>
<form name="rating_form">
<tbody>
<tr>
<td><?php echo $i ?></td>
<td> <?php echo $rate_text ?></td>
<td><span class="badge badge-danger">
<?php echo $provider_name ?></span></td>
<td>
<select name="p<?php echo $i ?>">
<option value="1">Very Bad</option>
<option value="2">Bad</option>
<option value="3">Good</option>
<option value="4">Very Good</option>
<option value="5">Excelent</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
<tfoot>
<tr>
<td colspan="3"><button type="submit" name="save_rate"> Save </button> </td>
</tr>
</tfoot>
`enter code here`
</form>
</table>
How can I get info form the select and save them to the database ?
Radio button is much better than <select>.
The rate id and selected value must be passed as a key=>value
You need to stop using Word Press syntax.
Learn to use Herdoc Syntax
Using SELECT * is a poor standard practice.
Fetch array and assign values with a list
This code is untested.
<?php
echo <<<EOT
<form name="rating_form" action="#">
<table class="table table-striped">
<tr><th>#</th><th>About</th><th>Provider</th><th>Rating</th></tr>
EOT;
$query_rate_text = "SELECT `rate_id`,`rate_text` FROM rateing_text WHERE 1 ORDER BY `rate_id`";
$selecting_rates = mysqli_query($con,$query_rate_text);
$i = 0;
while(list($rate_id, $rate_text) = mysqli_fetch_array($selecting_rates)){
$i++;
echo "<tr><td>$i</td><td>$rate_text</td><td>$provider_name</td><td>5<input type="radio" name=\"$rate_id\" value=\"5\" /> 4<input type="radio" name=\"$rate_id\" value=\"4\" /> 3<input type="radio" name=\"$rate_id\" value=\"3\" /> 2<input type="radio" name=\"$rate_id\" value=\"2\" /> 1<input type="radio" name=\"$rate_id\" value=\"1\" /></td></tr>\n";
}
echo '</table><button type="submit" name="save_rate"> Save </button></form>';
?>
UPDATE
thank you very much that's helped to understand new way's,one more
thing how can i get the data out from this radio 5 buttons to insert
them to the database to the table named rating which have let's say 3
column id and provider name and the rate it self , again thank you for
helping
The radio input type is a more user friendly way then the cumbersome select.
The way I setup the radio buttons with the rate_id as the "name" so the selected "value" is passed as a key=>value pair to the form's action script.
Your select name should also contain the rate_id
The problem your code has is, the action script will receive will receive keys of sequential numbers that have no meaning and difficult to know how many were posted by the form.
To pass the provide name add a hidden input type:
<input type="hidden" name="provider" value="$provider_name" />
And I would remove the provider from the table td.
Not knowing your rate_id convention I could not improve the radio button naming convention.
The way I would do it is when passing key=>value pairs I would begin the key "name" with a unique character where no other "name" in the form would start with that character.
For example if the rate_id is a numeric value I may prepend a 'k' to the numeric key value.
So instead of
name=\"$rate_id\"
I would use
name=\"k$rate_id\"
The in the receiving action script I would get the key=>values like this
$provider_name = $_GET['provider']
foreach($_GET as $key => $value)){
if(substr($key,0,1) == 'k'){
$rate_id = intval(substr($key,1));
$rating = intval($value);
$sql = "INSERT INTO `table` (`rate_id`, `provider_name`, `rating`) VALUES ($rate_id, '$provider_name', $rating)";
mysqli_query($link,$sql);
}
}
so the requirement is to have a table which allows the user to create a new estimate online. I went as far as adding rows/ deleting them, having my DB's product list on the product field. But once I select the product, I would like the field 'Price' to pull up its price.
Here's the code I am currently using:
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD><INPUT type="text" name="txt"/></TD>
<TD>
<SELECT name="country" onchange="getPrice(this.value, this.closest('tr').rowIndex);">
<?php while($row = mysql_fetch_array($query)) {
echo '<option value="' .$row['nom'] . '">'.$row['nom'].'</option>'; }
?>
</SELECT>
</TD>
<TD><INPUT type="text" name="txt"/></TD>
</TR>
</TABLE>
Ok, so what I assume is that your javascript function getPrice(name, tablecell) should retrieve the price stored in your database and puts it's price-value into the given table cell.
Before I write out a possible solution, I would like to add: This solution will keep the values from the database in a client-side array. Thus visible for any user, reviewing the code of your page. This shouldn't be an issue if you only store non-critical information though.
// For testing purpose i filled in a mini-json, but in
// the PHP-example later this post you will find a short
// example how to fill this with PHP from your database
var prices = {
'test_prod_1' : 16.46,
'test_prod_2' : 21.55,
'test_prod_3' : 7.4
};
function getPrice( prodID, tableCell )
{
if (typeof prices[prodID] !== 'undefined')
tableCell.innerHTML = '€' + prices[prodID]; // Get the price from the pre-loaded price information
else
tableCell.innerHTML = '?';
}
<input type="button" value="click me" onClick="javascript:getPrice('test_prod_2', document.getElementById('test_cell'));">
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td>Product name</td>
<td>price</td>
</tr>
<tr>
<td>test</td>
<td id="test_cell"></td>
</tr>
</table>
And then with PHP You would be able to create this price list dynamically. But, remember that the data is visible client side, so this example will only select the price and name/id data. Make sure to edit the variable names to the ones you use.
<script type="text/javascript">
var prices = <?php
$js_pricelist = array();
// Retireve the information from the server's database. Assume that $result is a result set in an ASSOC
foreach( $result as $row )
$js_pricelist[ $row['prodID'] ] = $row['prodPrice'];
echo JSON_Encode( $js_pricelist );
?>
</script>
Please check the last script as I haven't tested it yet, and is meant more to give you an idea of the possibilities
Conclusion: The main idea is, that once you load the page, you assemble a javascript object containing all price data (Using PHP). You echo this object to a javascript block, and make a function that reads from this newly created array/object to retrieve the price that corresponds to the product_id.
My problem is only user id is inserted into database but I want to insert user id, date and radio button value. How can I insert radio button value into the database?
I have 3 employees and I want to insert user id, date and status of a particular user. I have a single submit button and if I press submit then I want to insert all data into database.
Here is my Code:
<form>
<input type="date" name="date" >
<table class="display data_tbl">
<thead>
<tr>
<th>
Date
</th>
<th>
Employee Name
</th>
<th>
Status
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
<?php
error_reporting(0);
$us=0;
$at="select * from user_information";
$atd=mysql_query($at);
while($data=mysql_fetch_array($atd))
{
$us++;
if(isset($_GET['submit']) && $_GET['submit']!='' && $_GET['date']!='')
{
$a=$_GET['date'];
echo $b=date('d-m-Y',strtotime($a));
$insert=mysql_query("insert into attendence set user_id='".$data['user_id']."',date='".$b."',status='".$_GET['radio']$us."'");
}
?>
<tr>
<td><?php echo $b;?></td>
<td align="center"> <?php echo $data['name'];?>
</td>
<td class="center">
</td>
<td class="center">
<input type="radio" name="radio<?php echo $us;?>" value="Late">Late
<input type="radio" name="radio<?php echo $us;?>" value="Absent">Absent
<input type="radio" name="radio<?php echo $us;?>" value="Present">Present
<input type="radio" name="radio<?php echo $us;?>" value="Halfday">Halfday
<input type="radio" name="radio<?php echo $us;?>" value="Leave">Leave
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="Take Attendance">
</form>
firstly there is no need to give radio button name like you give because in radio button only one value can be selected at a time so remove the php value(i think you are using this to make radio button name unique) you are using with radio button name value from radio button name and than try.
You're insert statement has a syntax error, try replacing the following line
$insert=mysql_query("insert into attendence set user_id='".$data['user_id']."',date='".$b."',status='".$_GET['radio']$us."'");
with the following
$insert=mysql_query("insert into attendence(user_id, date, status) values('".$data['user_id']."','".$b."','".$_GET['radio'].$us."')");
There are many problem in your posted script
firstly as cyber_rookie mentioned, there is query syntax error in
data insert
secondly you have added submit button code inside while loop, which
is wrong, as while you are using for preparing display data and
submit code should be executed on submit button click
I have tried to make bit correct, please find code here https://jsfiddle.net/h5e5Lxnm/ you can check and modify it according to your requirement.
This code will give you all selected data (including date, radio vlaues and all)
EDIT:
My bad for saying you shouldn't use SET but here's another way for INSERT query:
INSERT INTO tablename (column1, column2, etc...) VALUES(value1, value2, etc....)
Applying to your code, this:
$insert=mysql_query("insert into attendence set user_id='".$data['user_id']."',date='".$b."',status='".$_GET['radio']$us."'");
to this:
$insert=mysql_query("insert into attendence (user_id, date, status)
values('".$data['user_id']."', '".$b."','".$_GET['radio'].$us."')");
And, your radio button should not have a different name so that the user can only select one option.
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>