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"];
Related
I have created a html form. In that I have 2 drop down lists. Drop-down list1 retrieves values from sql database table and displays over there. Now I want to display another drop-down list with input from drop-down list 1.
This is my code:
<label for="bname">Select a Building</label>
<?php
session_start();
include 'db_connection.php';
$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
$result = mysqli_query($conn, $sql);
echo "<select name='bname'>";
while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
}
echo "</select>";
echo "<label for='rtype'>Select a rtype</label>";
$sql2 = "SELECT rtype FROM room_details WHERE bname='bname'";
$result2 = mysqli_query($conn,$sql2);
echo "<select name='rtype'>";
while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
}
echo "</select>";
Here once bname value From building_details is selected then based on that input I need to display another dropdown list which is the rtype column from room_details table.
Can some one help me with this???
This is the only way I found to do it.
I hope it's what you're looking for!
<label for="bname">Select a Building</label>
<?php
session_start();
include 'db_connection.php';
$conn = OpenCon();
$sql = "SELECT bname FROM building_details";
$result = mysqli_query($conn, $sql);
$bnames = "";
echo "<select name='bname'>";
while ($row = mysqli_fetch_array($result)){
echo "<option value='". $row['bname'] ."'>".$row['bname'] ."</option>";
$bnames .= $row['bname'] . ", ";
}
$bnames = substr($bnames, 0, -2);
echo "</select>";
echo "<label for='rtype'>Select a rtype</label>";
$sql2 = "SELECT rtype FROM room_details WHERE bname IN($bnames)";
$result2 = mysqli_query($conn,$sql2);
echo "<select name='rtype'>";
while ($row2 = mysqli_fetch_array($result2)){
echo "<option value='". $row2['rtype'] ."'>".$row2['rtype'] ."</option>";
}
echo "</select>";
Don't forget to escape the $bnames variable with the htmlspecialchars function if needed to avoid sql injections.
mysqli_connect_errno() - Returns the error code from last connect call
mysqli_connect_error() - Returns a string description of the last connect error
mysqli_errno() - Returns the error code for the most recent function call
mysqli_sqlstate() - Returns the SQLSTATE error from previous MySQL operation
For more help use this link: http://php.net/manual/en/mysqli.error.php
I have done this many time to get the form POST and saving in a mysql db. I have a form with some data and when i save the button the data should be saved to another table. I have some 350 records to show in the form and when i save it, it should write to another table. But when the records are saved to another table its just saving some 63 records to another table from the form where as the actual record numbers is above 250 records.
My save.php file is as follows:
$size = count($_POST['sl']);
$i = 0;
while ($i < $size) {
$sl= $_POST['sl'][$i];
$item_id= $_POST['item_id'][$i];
$item_name= $_POST['item_name'][$i];
$query = "INSERT INTO anothertable SET slno = '$sl',item_name = '$item_name',item_id =
'$item_id' ";
mysql_query($query) or die ("Error in query: $query");
++$i;
}
Is this script fine? Or Am i making any mistake?
The Form script is posted below:
<?php
echo "<form name='cart' method='post' action='price_add_save.php?supplier_name=$supplier_name_enc&tender_id=$tender_id' >";?>
$sql= "(SELECT item_name, item_id, tender_id, slno FROM tender_items WHERE tender_id=$tender_id) order by slno";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$i = 0;
while ($list5 = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td width='4%'><input size='1' type='text' id='sl[$i]' name='sl[$i]' value='{$list5['slno']}' readonly/></td>";
echo "<td width='10%' id='addinput'><input type='text' size='15' name='item_name[$i]' placeholder='{$list5['item_name']}' value='{$list5['item_name']}'></td>";
echo "<td width='3%'><input size='2' class='item_id' type='text' name='item_id[$i]' value='{$list5['item_id']}' readonly/></td>";
++$i;
}
echo '<input type="submit" value="--Save Data--" />';
?>
It was basically an issue with max_input_vars in php.ini. I changed from 1000 to 5000..its working fine now. Thank you all..
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.
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" );
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' />";