html/php/sql form with insert/update - php

I have a table that has a list of clients on a waiting list where you can select to move one of them into a vacant room.
When you select this client, it moves onto a page called 'MoveIn1.html' where it gets that client's ID and uses it to display his/her name and ID. It also displays a drop-down box showing all the rooms that are vacant, giving the user a choice of which room to move the client into. The user then also chooses the date the client will move in and submits this information.
After it is submitted, it goes onto 'MoveIn2.php' where it should update the client's active to '1' (aka, yes), update the selected room's room_vacant to '0' (aka, no) and create/insert into a new occupancy record, along with the client's id (occupancy_client_id), the room id (occupancy_room_id) and the start date. I don't get a single error but my tables do not get updated and no occupancy record is created; when echo'ing my carried variables nothing is displayed either so I'm not sure how to sort this. Any help will be much appreciated.
Some of MoveIn1.html code:
$id = $_GET['id'];
//SELECTS from client where the selected client's ID is equal to client_id
$stmt = $dbh->prepare("SELECT * FROM client WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch();
echo "<b>Client ID: </b>" . $row['client_id'];
echo "<br><br>";
echo "<b>Client Name: </b>" . $row['first_name'] . " " . $row['last_name'];
echo "<br><br>";
///////////////////////////////////////////////////////////////
//Creates a form for room_id
echo "<form action='MoveIn2.php'>";
//Creates drop down box to show the current rooms vacant
echo "<b>Choose a room: </b>";
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
echo "<select name='room_id'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select><br><br>";
//Move-in date
echo "<b>Move-in date: </b>";
echo "<input type='date' name='start_date' value='" . date("Y/m/d") . "' required/><br><br>";
echo "<input type='hidden' name='id' value='" . $id . "'/>";
echo "<input type='hidden' name='submit' value='submit' />";
//Submit button
echo "<input type='submit' value='Submit'><br><br>";
echo "<input type='button' name='cancel' value='Cancel' onclick='window.location='ViewWaitingList.html'' /><br><br>";
//Closes form
echo "</form>";
/////////////////////////////////////////////////////////////////
?>
Some of MoveIn2.php code:
<?php
require("dbconnect.php");
//CLIENT
$id = $_POST['id'];
//UPDATES client's to 1/Yes to say that they're now an active client in JRH,
//where the selected client's ID is equal to :id
$stmt = $dbh->prepare("UPDATE client SET active = 1 WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();
//ROOM
$room_id = $_POST['room_id'];
$stmt = $dbh->prepare("UPDATE room SET room_vacant = 0 WHERE room_id = :room_id");
$stmt->bindParam(':room_id', $room_id);
$stmt->execute();
//OCCUPANCY
$id = $_POST['id'];
$room_id = $_POST['room_id'];
$start_date = $_POST['start_date'];
$stmt = $dbh->prepare("INSERT INTO occupancy (occupancy_client_id, occupancy_room_id, start_date) VALUES(:id, :room_id, :start_date)");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':room_id', $room_id);
$stmt->bindParam(':start_date', $start_date);
$stmt->execute();
echo $id;
echo $room_id;
echo $start_date;
?>

You're using echo "<form action='MoveIn2.php'>"; without explicitly stating that it should be a POST method and you're trying to pass POST arrays in the next page, forms default to GET, which in turn will fail silently.
Change:
echo "<form action='MoveIn2.php'>";
to
echo "<form action='MoveIn2.php' method='post'>";
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.

Related

how to get last inserted value when multiple users enter details in form doing in php and oracle

i am trying to insert user data and show that particular details in form who is last inserted :
$name=$_POST['name'];
$email_id=$_POST['email_id'];
$number=$_POST['number'];
$remarks=$_POST['remarks'];
$req_name=$_POST['req_name'];
date_default_timezone_set('Asia/Kolkata');
$date=date("d-m-Y");
$insert_data="INSERT INTO FEEDBACK(id, name, email, mobile_num, remarks,req_date) VALUES(FEEDBACK_sequence.nextval,:p_name, :p_email, :p_number, :p_remarks,:p_date)";
oci_bind_by_name($stid, ":p_name", $name);
oci_bind_by_name($stid, ":p_email", $email_id);
oci_bind_by_name($stid, ":p_number", $number);
oci_bind_by_name($stid, ":p_remarks", $remarks);
oci_bind_by_name($stid, ":p_date", $date);
oci_execute($stid) or die(oci_error());
in the same page, here i am inserting in user details with auto increment using sequence created and now i want to get last inserted user details in form to show,
like this i am showing the last inserted details, but i want to get details through sequence number using where condition in select query, how can i get last inserted record.
$stid = oci_parse($conn, 'select * from FEEDBACK');
$r = oci_execute($stid) or die(oci_error());
while ($row=oci_fetch_array($stid)) {
echo "<tr>\n";
echo "<td><input type='hidden' value='' name='id'>".$row['ID']."</td>";
echo "<td><input type='text' value='' name='name'>".$row['NAME']."</td> ";
echo "<td><input type='text' value='' name='email'>".$row['EMAIL']."</td>";
echo "<td><input type='text' value='' name='mobile_num'>".$row['MOBILE_NUM']." </td>";
echo "<td><input type='text' value='' name='remarks'>".$row['REMARKS']."</td>";
echo "<td>".$row['REQ_DATE']."</td>\n";
echo "</tr>\n";
please help me.
Maybe I'm looking at it to easy, but why not change
$stid = oci_parse($conn, 'select * from FEEDBACK');
to
$stid = oci_parse($conn, 'select * from FEEDBACK ORDER BY id DESC LIMIT 1');
It selects the rows in a reversed order, and will only return one row (with the highest ID).

PHP SELECT acting strange

I have a problem with PHP and Mysql. PHP is acting very strange. This is my code
echo "<form action='scripts/leerling.php' method='post'>";
echo "Nieuwe Leerling: <br/><br/>";
echo "Naam<br/>";
echo "<input type='text' name='naam'/><br/><br/>";
echo "Leeftijd<br/><input type='number' name='leeftijd'/><br/><br/>";
echo "Ouder:<br/>";
echo "<select name='ouder'>";
$result2 = mysqli_query($con, "SELECT * FROM users WHERE group=3");
while($record2 = mysqli_fetch_array($result2)){
echo "<option value='" . $record2["id"] . "'>" . $record2["username"] . "</option>";
}
echo "</select><br/> <br/>";
echo "<input type='image' src='img/plus.png'/><span style='font-size: 11pt;'> Leerling Toevoegen</span>";
echo "</form>";
i think everyting is allright. I want to make a selectbox with variable options. Now comes the annoying part: if i change this:mysqli_query($con, "SELECT * FROM users WHERE group=3") To this:mysqli_query($con, "SELECT * FROM users WHERE id=3") it works! and i dont know why... My table of my database sure has a column named ID AND a column named group and they are both the datatype INT but ID is also A_I. I dont know if that matters...
Perhaps because GROUP is a reserved word in MySQL? Try this:
$result2 = mysqli_query( $con, "SELECT * FROM users WHERE 'group' = 3" );

insert statement using request and session

for some reason I cant seem to get the data to insert php and mysql.
I have a init.php with the connection string in, a order.php file and finaly a band_list.php.
I am having some problem getting the data in to database.
the database has 3 tables:
order it has id, order_id and band_id columns
users it has id, name and password columns
bands it has Band_id, name and stock columns
band_list.php has a band gig details in it shown to the user.
<?php
require 'core/init.php';
$Band_id = $_GET['id'];
$result = mysql_query("SELECT * FROM bands WHERE Band_id = $Band_id");
echo "<table border = '1'>
<tr>
<th>Band Name</th>
<th>Venue</th>
<th>Category</th>
<th>Stock</th>
<th>Add</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><form name=\"myform\" action=\" order.php\" method=\"post\">";
echo "<td> <input name=\"band\" type=\"hidden\" value=\"". $Band_id."\" ></td>";
echo "<td>" .$row['Name']. "</td>";
echo "<td>" .$row['Venue']. "</td>";
echo "<td>" .$row['Category']. "</td>";
echo "<td>" .$row['Stock']. "</td>";
echo "<td><button>Buy Ticket</button></td>";
echo "<td><input type=\" submit\" value=\"Buy Ticket\"></td>";
echo "</tr> </form>";
}
echo "</table>";
?>
order.php has the query that is meant to send the data to the database
<?php
require 'core/init.php';
session_start();
$Band_id = $_REQUEST['Band_id'];
$user_id = $_SESSION['user_id'];
$sql = "INSERT INTO orders (band_id,user_id) VALUES($Band_id,$user_id)";
mysql_query ($sql, $linkme)
or die ("could not add to database");
?>
and the connection string is in a init file.
so the idea is when the user clicks buy ticket it gets the current Band_id and user_id and inserts them into the database table orders in to columns band_id and user_id.
This is not happing I am just getting my or die ("could not add to database"); string come up.
Is there a problem with the way I have done it?
<input name=\"band\" type=\"hidden\" value=\"". $Band_id."\" >
And you're trying to get POST variable "Band_id" (which does not exist)
$Band_id = $_REQUEST['Band_id'];
Should be
$Band_id = mysql_real_escape_string($_REQUEST['band']);
Other details:
In order.php, it's better to place "session_start()" at the
beginning (before anything)
Consider using mysqli or PDO instead of deprecated mysql
You could get band id with $_POST instead of $_REQUEST because your
form sends it with post method

I have PHP populating a Dropdown box, and I want it to also populate two read only Text boxs from the same Table

I have a table called Parts, (PartID, PartName, Cost) and I have PHP populating a Drop down box with the PartID's. I want it to when a user selects a Part ID, it populates one text box with the Part's name and the other box, with the Part's cost.
Here's what my Code look's like for the drop down box if it helps any :)
$sql = "SELECT PartID FROM Parts WHERE PartID LIKE 'C0%'";
$result = mysql_query($sql);
echo "<select name='PartID'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['PartID'] . "'>" . $row['PartID'] . "</option>";
}
echo "</select>";
I don't know what you're exactly trying to do, but this sounds more like javascript, to me.
Set a onchange function on your dropdown and get it to fill the fileds with name and cost that you would have stock into a javascript array before. Sounds ugly but it would work
You'll have to POST back to retrieve the values, either with a simple POST or an AJAX POST
if (!empty($_POST)) {
$partid = $_GET["PartID"];
$sql = "SELECT * FROM Parts WHERE PartID = '$partid'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$part_name = $row ["PartName"];
$part_cost = $row ["PartCost"];
}
$sql = "SELECT PartID FROM Parts WHERE PartID LIKE 'C0%'";
$result = mysql_query($sql);
echo "<select name='PartID' onchange='document.getElementById(\'form1\').submit();'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['PartID'] . "'>" . $row['PartID'] . "</option>";
}
echo "</select>";
echo "<input type='text' value='$part_name' />";
echo "<input type='text' value='$part_cost' />";

Handling a radio button array in php

I have a bit of PHP code that is really taking a toll on me. To simplify, I have a table where three of the columns are foreign keys (event_type, accommodation_type and public_user). I am dynamically creating a set of radio buttons and I have set the ID attribute on each input value correctly.
$result2 = mysql_query("SELECT * FROM event_type", $connection);
if(!$result2){
die("Database query failed: " . mysql_error());
}
$event = mysql_fetch_array($result2);
echo "<input type='radio' name='event_type' value='";
echo $event["id"];
echo "' > ";
echo $event['name'];
echo "</input><br /><br />";$result4 = mysql_query("SELECT * FROM accommodation_type", $connection);
if(!$result4){
die("Database query failed: " . mysql_error());
}
while($row = mysql_fetch_array($result4)){
if($row["id"] == 7 || $row["id"] == 8){
echo"<tr><td>";
echo $row["name"] ;
echo"</td><td>$";
echo $row["price"];
echo ".00</td><td>";
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
echo "' />";
echo "</td></tr>";
}
}
I retrieved the correct id from the query. Thus after submitting the form with POST, I go on to do some minor validation and prepped the names from my post as follows:
$event_type = trim(mysql_prep($_POST['event_type']));
$accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
$public_user = trim(mysql_prep($_POST['public_user']));
$comments = trim(mysql_prep($_POST['comments']));
$grand_total = trim(mysql_prep($_POST['grand_total']));
I then proceeded to write insert statements to insert the data into the relevant tables. This requires two queries as follows.
$query = "INSERT INTO event_registration (event_type, accommodation_type, public_user, comments, grand_total) VALUES ('{$event_type}','{$accommodation_type}','{$public_user}','{$comments}','{$grand_total}')";
$query1 = "INSERT INTO additional_member (first_name, last_name, gender, age_group, public_user) VALUES ('{$first_name}','{$last_name}','{$gender}','{$age_group}','{$public_user}')";
$result = mysql_query($query, $connection);
$result1 = mysql_query($query1, $connection);
The query1 works as expected, however the first query fails to insert the data. It says undefined index at the lines where I have
$event_type = trim(mysql_prep($_POST['event_type']));
$accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
I am not entirely sure where things went wrong. Everything seems to be set up correctly and the data from the form just refuses to be saved.
Any ideas ?
why not try reviewing the code from scratch and see if there are any syntax errors and then move on.
You are creating accommodation type
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
But not echoing event_type anywhere. Try echoing that too:
echo "<input type='radio' name='accommodation_type' value='";
echo $row["id"];
echo "<input type='radio' name='event_type' value='";
echo $row["whatever"];

Categories