I don't know what I have done to this but I had the form working and now it has stopped. My problems are with the bottom if statement where I am trying to delete the user. As I said it was working properly and now it has stopped. I just can't figure out what the issue is? And yes I do backup but clearly should do it more!
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
if(isset($_POST['action2'])){
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
}
}
Use the code like this,
function HMdisplayrooms() {
global $wpdb;
echo '<html><body><h1>Display Rooms</h1>';
echo '<p>Order room view by: <p>
<form name = "view_HM_rooms" method="post" action="">
<select name="roomsView" size = "1">
<option value = "room_id">Room ID</option>
<option value = "room_type">Room Type</option>
</select></br>
<input type="submit" name="action">
</form></body></html>';
echo '<table border="1" style="width:1000px" cellspacing ="0"><tr><td><b>Room ID</b></td><td><b>Room Type</b></td><td><b>Options</b></td></tr>';
if(isset($_POST['action'])) {
$roomType = $_POST['roomsView'];
$query = "SELECT * FROM hm_room ORDER BY $roomType ASC";
$rooms = $wpdb->get_results($query);
foreach ($rooms as $room) {
$roomID = ($room->room_id);
echo '<tr><td>'.format_to_post($room->room_id).'</td><td>'.format_to_post($room->room_type).'</td><td><form name = "HotelManiaRoomDeletion" method="post" action="">
<input type="hidden" name="DelRooMId" value="'.$roomID.'">
<input type="submit" name="action2" value="Delete Room"></form></td></tr>';
}
}
if(isset($_POST['action2'])){
$roomID = $_POST['DelRooMId']; // sanitize the input
$results = $wpdb->query("DELETE FROM hm_room WHERE room_id='".$roomID."'");
$msg = "Room deleted";
return $msg;
}
}
I have pulled the DELETE block outside of your code. It may help you. The problem with your code is $_POST['action'] will be null at the time of DELETE operation , so the code never executes, that may be the error
Note - Make sure to get the $roomID when you are using the below structure
Related
I want to save the selected value in post and access it from checkout.php. So far this method has worked for my other pages but here it just doesn't. This is how it looks like:
Source page of the value:
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" value="Checkout">
</form>
Then in checkout.php it looks like this.
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
If I echo the $time variable, I always get the default value '10:00:00'. Any idea what could cause this problem?
You forgot to add name attribute in <input class="submit-block" type="submit" value="Checkout">
Like :- <input class="submit-block" type="submit" name="submit" value="Checkout">
<form action = "checkout.php" method="post" >
<select name='time' required>
<option value="">-- Time --</option>
<?php
$result = getFreeAppointments($empid,$date);
while ($row = mysqli_fetch_array($result)) {
?>
<option value = <?php echo $row['StartTime'];?>><?php echo $row['StartTime'];?></option>
<?php } ?>
</select>
<input class="submit-block" type="submit" name="submit" value="Checkout">
</form>
you have set isset($_POST["submit"]) but in HTML the form, there is no data with this name.
if (isset($_POST['submit'])){ // Problem is here
insertKunde($_POST);
if(isset($_POST['time']) && !empty($_POST['time'])) {
$time = $_POST['time'];
}
else{
$time = '10:00:00';
}
}
into schooling entry form, I am not able to get value of employee_id from post.
I did Print_r for $employee_id, blank output is rendered.
Also if allocated static value to $employee_id, $sum contains only value posted through the form, instead it should show the value from input form plus value from existing value available in database.
<?php
$get = db_query("SELECT field_employee_id_value FROM field_data_field_employee_id ORDER BY field_employee_id_value ASC");
$getempnames = db_query("SELECT field_employee_id_value FROM field_data_field_employee_id ORDER BY field_employee_id_value ASC");
if(isset($_POST['apply'])){
$sql = db_query("SELECT COUNT(id) as count_id FROM schooling WHERE employee_id = '$_POST[employee_id]' AND claim_year = '$_POST[claim_year]'");
$row = $sql->fetchAssoc();
if('1' == $row['count_id']){
$sqlupdate = db_query("UPDATE schooling SET limit_amount = '".$_POST['limit_amount']."' WHERE employee_id = '$_POST[employee_id]'");
echo "Schooling limit updated to user ";
} elseif ('0' == $row['count_id']){
$sqlinsrt = db_query("INSERT INTO schooling (employee_id, limit_amount, claim_year) VALUES ('".$_POST["employee_id"]."','".$_POST["limit_amount"]."','".$_POST["claim_year"]."')" );
echo "Schooling limit applied to user";
} else{
echo "Already Applied schooling limit";
}
}
if(isset($_POST['save'])){
$employee_id = $_POST['employee_id'];
$claim_amount = $_POST['claim_amount'];
$claim_year = $_POST['claim_year'];
$sqlchkemp = db_query("SELECT COUNT(id) as count_id FROM schooling WHERE employee_id = '$employee_id' AND claim_year = '$claim_year'");
$empavailable = $sqlchkemp->fetchAssoc();
if('1' == $empavailable['count_id']){
$getlimit = db_query("SELECT limit_amount FROM schooling WHERE employee_id = '$employee_id' AND claim_year = '$claim_year'");
$limit = $getlimit->fetchAssoc();
$getemptotalclaim = db_query("SELECT claim_amount FROM schooling
WHERE employee_id = '$employee_id' AND claim_year = '$claim_year'");
$emptotalclaim = $getemptotalclaim->fetchAssoc();
$totalclaimed = array_sum($emptotalclaim);
$availability = $limit['limit_amount'] - $_POST['claim_amount'];
$sum = $totalclaimed['claim_amount'] + $claim_amount;
if ($sum <= $limit['limit_amount']){
$sqlinsert = db_query("UPDATE schooling SET claim_amount = '$sum' WHERE employee_id = '$employee_id'");
echo "values updated successfuly";
}
else{
echo "limit is over, you can avail total amount ".$availability." as per ".$limit['limit_amount']." alloted";
}
}
else{
echo "employee schoolig limit is not set";
}
}
?>
<html>
<body>
<form id='applylimit' action='' method='post' accept-charset='UTF-8'>
<fieldset>
<label>Apply Schooling Limit amount to Employee</label>
<label for='employee_id'>Employee Id</label>
<select name='employee_id'>
<option value="0">Please Select</option>
<?php
while($row = $getempnames->fetchAssoc())
{
?>
<option value = "<?php echo($row['field_employee_id_value'])?>">
<?php echo($row['field_employee_id_value']) ?>
</option>
<?php
}
?>
</select>
<label for='limit_amount'>Limit Amount</label>
<input type='number' name='limit_amount' id='limit_amount' maxlength="50" />
<label for='claim_year'>Claim Year</label>
<select type='number' name='claim_year' id='claim_year' maxlength="50">
<option value="2018-19">2018-19</option>
<option value="2019-20">2019-20</option>
</select>
<button type="submit" name="apply">Apply</button>
</fieldset>
</form>
<form id='schoolingentry' action='' method='post' accept-charset='UTF-8'>
<fieldset>
<label for='employee_id'>Employee Id </label>
<select name='employee_id'>
<option value="0">Please Select</option>
<?php
while($rowemp = $get->fetchAssoc())
{
?>
<option value = "<?php echo($row['field_employee_id_value'])?>" >
<?php echo($rowemp['field_employee_id_value']) ?>
</option>
<?php
}
?>
</select>
<label for='claim_amount'>Claim Amount</label>
<input type='number' name='claim_amount' id='claim_amount' maxlength="50" />
<label for='claim_year'>Claim Year</label>
<select name='claim_year' id='claim_year' maxlength="50">
<option value = "2018-19">2018-19</option>
<option value = "2019-20">2019-20</option>
</select>
<button type="submit" name="save">save</button>
</fieldset>
</form>
</body>
</html>
word of warning, do not put anything submitted from a $_POST straight into a database query. You should sanitize it all by passing in the parameters.
e.g.
$result = db_query('SELECT n.name FROM users n WHERE n.name = :name', array(':name' => $name));
If the first query isn't returning any results, it's likely those two parameters you are passing into the string are not what you expect, or not valid. Try echoing out the two variables, then running the SQL query manually.
Or if you want Drupal to be a bit more verbose, wrap it in a exception catcher..
e.g.
catch (\PDOException $e) {
$error = $e->getMessage();
I have a small problem.
<select name="level_id"> is not posting.
So i get an error like : Undefined index : level_id
Exactly what can i do?
$sql1 = 'SELECT T_ABILITY.PK AS AB_PK,T_ABILITY.ABILITY_NAME AS AN,T_ABILITY_LEVEL.PK AS LE_PK,T_ABILITY_LEVEL.LEVEL_NAME AS LN
FROM T_USER_ABILITY_REL,T_ABILITY,T_ABILITY_LEVEL WHERE
T_USER_ABILITY_REL.ABILITY_FK = T_ABILITY.PK AND
T_USER_ABILITY_REL.ABILITY_LEVEL_FK = T_ABILITY_LEVEL.PK AND
T_USER_ABILITY_REL.USER_FK = '.$user_id.'
ORDER BY AN';
$stmt1 = oci_parse($conn, $sql1);
$r1 = oci_execute($stmt1);
while ($row1 = oci_fetch_array($stmt1, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<form method="post">';
echo '<tr>';
echo '<td>'.$row1["AN"].'</td>';
echo '<input type="hidden" name="ability_id" value="'.$row1["AB_PK"].'"/>';
echo '<td class="select-level">';
$sql2 = 'SELECT PK,LEVEL_NAME FROM T_ABILITY_LEVEL ORDER BY LEVEL_ORDER';
$stmt2 = oci_parse($conn, $sql2);
$r2 = oci_execute($stmt2);
echo '<select name="level_id" class="form-control selectpicker" data-container="body" data-live-search="true" data-size="5" title="Seviye Seçiniz">';
while ($row2 = oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC)) {
echo '<option '.($row2["PK"] == $row1["LE_PK"] ? 'selected="selected"' : "").' value="'.$row2["PK"].'">'.$row2["LEVEL_NAME"].'</option>';
}
echo '</select>';
echo '<button type="submit" name="update-user-ability" class="btn btn-success">Güncelle</button>';
echo '<button type="submit" name="delete-user-ability" class="btn btn-danger">Sil</button>';
echo '</td>';
echo '</tr>';
echo '</form>';
}
submit part below
if (isset($_POST["update-user-ability"])) {
$user_id = $_GET["user_id"];
$ability_id = $_POST["ability_id"];
$level_id = $_POST['level_id'];
This is because your select is empty (that is, the oci_fetch_array($stmt2, OCI_RETURN_NULLS + OCI_ASSOC returns no rows.)
A select, when no option are present, will not return a value to the receiving php script.
Add a default option before the loop to be sure something is passed even if your sql query returns nothing.
this script will return an empty post:
<html>
<body>
<form method="post">
<select name="select"></select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(0) { }
while this one will have the value 12 defined:
<html>
<body>
<form method="post">
<select name="select">
<option value="12">12</option>
</select>
<input type="submit">
</form>
</body>
</html>
<?php
var_dump($_POST); // array(1) { ["select"]=> string(2) "12" }
Could be that the value of level_id isn't send along, because it is a <select>. Not sure, but could be that only <input /> values are properly posted.
You could use a <input type="hidden" name="level_id_val" /> and update its value with JavaScript every time the value of <select name="level_id"> is changed (using .onchange).
Then, in PHP, use that field instead of the select: $level_id = $_POST['level_id_val'];
I have a multiple select form:
<form method="post" action="register_results.php" name="registerform" class="form-horizontal" role="form">
<div class="label">Select Name:</div>
<select name="names" multiple="yes" size="15">
<option value = "">---Select---</option>
<?php
while ( $row=mysqli_fetch_assoc($result)) {
echo "<option value='".$row['registrant_name']."'>".$row['registrant_name']."</option>";
}
mysqli_close($con);
?>
</select>
</form>
The register_results.php file looks like this:
$registrant_name = $_POST['names'];
$event_result = $_POST['result'];
$query = "UPDATE events_regged SET result = $event_result WHERE event_name='event10' AND registrant_name=('$registrant_name') ";
$result = mysqli_query($con, $query);
I want to be able to add multiple mysql rows (one row for each username) if multiple names are selected in the form. How can I do that?
Use
<select name="names[]" multiple="yes" size="15">
instead of
<select name="names" multiple="yes" size="15">
and use foreach
foreach($_POST['names'] as $value)
{
//your query goes here
}
You have some changes required within your code as
<select name="names" multiple="yes" size="15">
^^^^^
it should be
<select name="names[]" multiple size="15">
This will result into an array so the result of
$registrant_name = $_POST['names']
will be an array
Change the form select element name from "names" to "names[]" and then you can access an array of posted values - which you can then iterate through.
First change your form as below (it's more elegant and fast to use Id instead of name (string) in Sql):
<select name="ids[]" multiple size="15">
<option value = "">---Select---</option>
<?php
while ( $row=mysqli_fetch_assoc($result)) {
echo "<option value='".$row['registrant_id']."'>".$row['registrant_name']."</option>";
}
mysqli_close($con);
?>
</select>
And in your PHP use foreach:
$registrant_ids = $_POST['ids'];
$event_result = $_POST['result'];
foreach($registrant_ids as $id)
{
$query = "UPDATE events_regged SET result = $event_result WHERE event_name='event10' AND registrant_id=".$id;
$result = mysqli_query($con, $query);
}
I'm trying to use this search for searching more than one words or single word at a time from a database. Now the problem is that my script only running properly but please help me...
<form name="ds" method="post">
<input type="text" name="name" placeholder="Entername" />
<!--<input type="text" name="search" placeholder="Location" />-->
<select name="location[]" multiple="multiple">
<option value="delhi">Delhi</option>
<option value="Mumbai">Mumbai</option></select>
<input type="submit" name="submit" value="Search" />
</form>
<?
$conn=mysql_connect("localhost","root","");
mysql_select_db("dbrozgarexpress",$conn);
echo $search =$_POST['location'];
$description = "";
$name2=$_POST['name'];
if(isset($name2)&&$name2 != ""){
if($flag){
$cheack.= "AND ";
}
$cheack.="user_first_name ='$name2' ";
$flag =true;
}
if(isset($search)&&$search != ""){
if($flag){
$cheack.= "AND ";
}
foreach($search AS $s)
{
$cheack.="user_current_location_id ='$s' or ";
$flag =true;
}
}
$cheack = substr($cheack, 0, -4);
echo $query = "SELECT * FROM `tb_user` WHERE $cheack ";
?>
error SELECT * FROM `tb_user` WHERE user_first_name ='kum
I think I have a general idea of what you are after.
P.S. the query will only show after you submit the form - i.e. after you click search button
Try this:
<?php
// Handle Post
if (count($_POST))
{
// Load Params
$name = isset($_POST['name']) ? $_POST['name'] : '';
$locations = isset($_POST['location']) ? $_POST['location'] : array();
// Start Query
$sql = 'SELECT * FROM `tb_user` WHERE ';
// Build Query
$sql_parts = array();
if (!empty($name)) {
$sql_parts[] = "`user_first_name` = '$name'";
}
if (sizeof($locations)) {
foreach ($locations as $location) {
$sql_parts[] = "`user_current_location_id` = '$location'";
}
}
$sql = $sql . implode(' AND ', $sql_parts);
// Debug
echo $sql ."<br><br>";
}
?>
<form action="" name="ds" method="post">
<input type="text" name="name" placeholder="Entername" />
<select name="location[]" multiple="multiple">
<option value="delhi">Delhi</option>
<option value="Mumbai">Mumbai</option></select>
<input type="submit" name="submit" value="Search" />
</form>
Here's an example of this working:
No location selected, name only
name and one location
name and two location