I am keeping the value(inserted by user) of a text box in a variable $no. But it is not showing anything when I write an echostatement. I don't understand why??
I believe I am not inserting the value properly. I would like some help.
Thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<title>Attendence form</title>
<link type = "text/css" rel = "stylesheet" href = "style.css">
</head>
<body>
<form action = "<?php $_SERVER['PHP_SELF']; ?>" method = "post">
<table>
<tr>
<th>Department</th>
<th>Year</th>
<th>Course</th>
<th>Lecture No</th>
<th>Date</th>
</tr>
<tr>
<td>
<select name = "dept">
<option value = "cse">CSE</option>
<option value = "eee">EEE</option>
</select>
</td>
<td>
<select name = "year">
<option value = "1st">1st</option>
<option value = "2nd">2nd</option>
</select>
</td>
<td>
<select name = "course">
<option value = "cse2200">CSE 2200</option>
<option value = "cse2201">CSE 2201</option>
<option value = "cse2213">CSE 2202</option>
<option value = "eee2217">EEE 2217</option>
<option value = "math2207">MATH 2207</option>
</select>
</td>
<td>
<input type = "text" name = "days" />
</td>
<td>
<?php echo date("Y/m/d"); ?>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>
<input type = "submit" name = "submit" value = "Take attendence" />
</td>
<td>
<input type = "submit" name = "view" value = "View Attendence" />
</td>
</tr>
</table>
<hr />
<table>
<?php
$year = $_POST['year'];
$dept = $_POST['dept'];
$course = $_POST['course'];
$no = $_POST['days'];
$date = date("Y/m/d");
echo $days;
if(isset($_POST['submit']) && !isset($_POST['calculate']) && !isset($_POST['view'])){
if($year == '1st')
$roll = 1400000;
else if($year == '2nd')
$roll = 1300000;
if($dept == 'cse')
$roll = $roll + 7000;
else if($dept == 'eee')
$roll = $roll + 3000;
$i = 1;
echo '<tr><th>Attendence Form:</th></tr>';
while($i <= 10){
$roll = $roll + 1;
echo '<tr> <td> <input type = "checkbox" value = "'. $roll . '" name = "present[]">'.$roll.'</input> </td></tr>';
$i++;
}
?>
<td>
<input type = "submit" name = "calculate" value = "Calculate" />
</td>
<?php
}
else if(isset($_POST['calculate'])){
$dbc = mysqli_connect('localhost', 'root', '', 'rankodatabase')
or die('Error connecting database');
foreach($_POST['present'] as $p){
$query1 = "SELECT * FROM attendence WHERE roll = '$p' AND course = '$course'";
$result1 = mysqli_query($dbc, $query1)
or die('Error in select query1');
$p_days = 0;
while($row = mysqli_fetch_array($result1)){
$p_days++;
echo 'roll'. $row['roll'] . ' course' . $row['course'] . ' date' . $row['date'] . ' percentage' . $row['percentage'] . '<br />';
}
echo 'Lecture' . $no;
/*$percentage = ($p_days / $no_of_lecture) * 100;
$query2 = "INSERT INTO attendence VALUES('$p', '$course', '$date', '$percentage')";
$result2 = mysqli_query($dbc, $query2)
or die('Error in insert query2'); */
}
//$query = "INSERT INTO attendence VALUES('"
}
?>
</table>
</form>
</body>
</html>
Related
I just get this error below. It does work on localhost. Any ideas?
Any advise on how to google this error would help a lot as well, as I am not sure where the problem might be.
"The requested URL /edit.php was not found on this server."
<?php
include ('includes/connection.php');
include ('includes/functions.php');
include ('includes/header.php');
$jobId = $_GET["id"];
$query = "SELECT * FROM Freight, WHERE id = '$jobId'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$jobArrival = $row["Arrival"];
$jobDeparture = $row["Departure"];
$jobClient = $row["Client"];
$jobAirportOfDeparture = $row["AirportOfDeparture"];
$jobAirportOfArrival = $row["AirportOfArrival"];
$jobAdditionalInfo = $row["AdditionalInfo"];
$jobBoxType = $row["BoxType"];
$jobTemp = $row["Temp"];
// prideti is dezes table
// $pavadinimas = $row["pavadinimas"];
// $likutis = $row["likutis"];
if (isset($_POST['update'])) {
$jobArrival = validateFormData($_POST['jobArrival']);
$jobDeparture = validateFormData($_POST['jobDeparture']);
$jobClient = validateFormData($_POST['jobClient']);
$jobAirportOfDeparture = validateFormData($_POST['jobAirportOfDeparture']);
$jobAirportOfArrival = validateFormData($_POST['jobAirportOfArrival']);
$jobAdditionalInfo = validateFormData($_POST['jobAdditionalInfo']);
$jobBoxType = validateFormData($_POST['jobBoxType']);
$jobTemp = validateFormData($_POST['jobTemp']);
$query = "UPDATE Freight SET Arrival = '$jobArrival',
Departure = '$jobDeparture',
Client = '$jobClient',
AirportOfDeparture = '$jobAirportOfDeparture',
AirportOfArrival = '$jobAirportOfArrival',
AdditionalInfo = '$jobAdditionalInfo',
BoxType = '$jobBoxType',
Temp = '$jobTemp'
WHERE id = '$jobId'";
$result = mysqli_query($connection, $query);
if ($result) {
header("Location: formdisplay.php");
} else {
"Klaida" . mysqli_error($connection);
}
}
}
}
else {
echo "Nera irasu!!!!!!!!!!!!!!";
}
if (isset($_POST['istrinti'])) {
$query = "DELETE FROM Freight WHERE id ='$jobId'";
$result = mysqli_query($connection, $query);
if ($result) {
header("Location: formdisplay.php?alert=deleted");
}
else {
echo "Error" . mysqli_error($connection);
}
}
mysqli_close($connection);
?>
<h1> Iraso koregavimas</h1>
<table>
<tr>
<td>Arrival</td>
<td>Departure</td>
<td>Client</td>
<td>Airport Of Departure</td>
<td>Airport Of Arrival</td>
<td>Additional Info</td>
<td>Box Type</td>
<td>Temp</td>
</tr>
<tr>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?id=<?php echo $jobId; ?>">
<td>
<input type = "text"
value = "<?php echo $jobArrival ?>"
name = "jobArrival">
</td>
<td>
<input type = "text"
value = "<?php echo $jobDeparture ?>"
name = "jobDeparture">
</td>
<td>
<input type = "text"
value = "<?php echo $jobClient ?>"
name = "jobClient">
</td>
<td>
<input type = "text"
value = "<?php echo $jobAirportOfDeparture ?>"
name = "jobAirportOfDeparture">
</td>
<td>
<input type = "text"
value = "<?php echo $jobAirportOfArrival ?>"
name = "jobAirportOfArrival">
</td>
<td>
<input type = "text"
value = "<?php echo $jobAdditionalInfo ?>"
name = "jobAdditionalInfo">
</td>
<td>
<input type = "text"
value = "<?php echo $jobBoxType ?>"
name = "jobBoxType">
</td>
<td>
<input type = "text"
value = "<?php echo $jobTemp ?>"
name = "jobTemp">
</td>
<td>
<input type = "submit"
name = "update"
value = "update"
href = "formdisplay.php"></td>
<td>
<input type ="submit"
name ="istrinti"
value ="istrinti"
href ="formdisplay.php">
</td>
</form>
</tr>
</table>
The key here is likely the / in /edit.php. Try including just edit.php instead, or more directly pathing it to the directory.
Good luck!
Problem SOLVED. Problem on line 143, few wrong spaces.
Took me only a week to find it :)
Code has to look like this <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>?id=<?php echo $jobId ?>">
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've been looking for a solution to this problem, but with no luck. Basically when inserting an item in a table it doesn't return a result (doesn't insert anything). I've checked the table fields and all match, I can't seem to find what error I'm doing.
Here's the portion of the code I currently have, where the problem occurs.
<br>
<h3><center><b>Components management</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Nome: <input type = "text" name = "component_name" required></b> </p><br>
<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>Active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>
<!--Insert button-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>
</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Components management - inserçtion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];
$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '$component_type', '%s')", mysqli_real_escape_string($conn, $component_name),
mysqli_real_escape_string($conn, $component_state));
$resultado_insert = mysqli_query($conn, $insert);
if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Insert successfuly</p>
<p>Click in Continue to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error.</p>
<?php
}
back();
}
}
?>
If anyone could help I'd be really grateful!
EDIT: Changed $$component_name to $component_name. (Typo)
EDIT: Full page code.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"href="/custom/css/ag.css">
</head>
<body>
<?php
require_once("custom/php/common.php");
if(!is_user_logged_in() && !current_user_can('manage_components'))
{
echo "You don't have permission to access this page";
}
else
{
if($_REQUEST['state'] == "")
{
$verify_component = "SELECT * FROM concept";
$result_component = mysqli_query($conn, $verify_component);
if(mysqli_num_rows($result_component) == 0)
{
echo "Não há componentes";
}
else
{
?>
<table class="mytable">
<thead><tr>
<th>Type</th>
<th>ID</th>
<th>Name</th>
<th>State</th>
<th>Ação</th>
</tr></thead>
<!--<tbody>-->
<?php
$choice_comp_type = "SELECT * FROM conc_type";
$resultado_comp_type = mysqli_query($conn, $choice_comp_type);
while($type = mysqli_fetch_array($resultado_comp_type))
{
$choice_components = "SELECT component.*
FROM concept,conc_type
WHERE conc_type.id = concept.conc_type_id
AND conc_type_id = '".$type['id']."'";
$result_components = mysqli_query($conn, $choice_components);
$num_rows = mysqli_num_rows($result_components);
if($num_rows > 0)
{
?>
<tr>
<td class= "Name" colspan = "1" rowspan = "<?php echo $num_rows; ?>"> <?php echo $type['name']; ?> </td>
<?php
while($row = mysqli_fetch_array($result_components))
{
?>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<?php
if($row['state'] == "active")
{
?>
<td>active</td>
<td>[edit] [desactivate]</td>
<?php
}
else
{
?>
<td>inactive</td>
<td>[edit] [activate]</td>
<?php
}
?>
</tr>
<?php
}
}
}
?>
<!--</tbody>-->
</table>
<?php
}
?>
<br>
<h3><center><b>Management Compontent - introduction</b></center></h3>
<br>
<form name = "registo" method = "POST" action = "">
<center><fieldset>
<legend>Insert component</legend>
<br>
<p><b> Name: <input type = "text" name = "component_name" required></b> </p><br>
<p> <b>Type:</b> </p>
<div class = "mytable">
<table><tr>
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<th><?php echo $rowComp_Type['name']; ?></th>
<?php
}
?>
</tr><tr>
<?php
foreach ($resultadoQueryComp_Type as $rowComp_Type)
{
?>
<td><input class= "radio" type= "radio" name= "component_type" value= "<?php echo $rowComp_Type['id']; ?>" required> </td>
<?php
}
?>
</tr></table>
</div><br>
<p> <b>State:</b></p>
<table class="mytable">
<tr><th>active</th><th>Inactive</th></tr>
<tr>
<td> <input class= "radio" type= "radio" name= "component_state" value= "active" required></td>
<td> <input class= "radio" type= "radio" name= "component_state" value= "inactive"></td>
</tr>
</table>
<br>
<!--Botão Inserir-->
<input type= "hidden" name= "state" value= "inserir">
<input class= "button" type= "submit" value= "Inserir Componente">
<br><br>
</fieldset></center>
</form>
<?php
}
else if($_REQUEST['state'] == "inserir")
{
?>
<h3>Management component - insertion</h3>
<?php
$component_name = $_REQUEST['component_name'];
$component_type = $_REQUEST['component_type'];
$component_state = $_REQUEST['component_state'];
$insert = sprintf("INSERT INTO `concept` (`name`,`conc_type_id`,`state`) VALUES ('%s', '%s', '%s')", mysqli_real_escape_string($conn, $component_name), mysqli_real_escape_string($conn, $component_type),
mysqli_real_escape_string($conn, $component_state));
$resultado_insert = mysqli_query($conn, $insert);
if($resultado_insert)
{
mysqli_query($conn,'COMMIT');
?>
<p>Successfull insertion</p>
<p>Click in Continue to advance.</p>
<?php
}
else
{
mysqli_query($conn,'ROLLBACK');
?>
<p>Insertion error</p>
<?php
}
back();
}
}
?>
</body>
</html>
First when you create a table... get a result of query... i think should be like this :
<div class = "mytable">
<?php
$queryComp_Type = "select id, name from conc_type order by name";
$resultadoQueryComp_Type = mysqli_query($conn, $queryComp_Type);
$tableHeader = "";
$tableBody = "";
?>
<table>
<thead>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableHeader .= '<tr>
<th>'.$row['name'].';</th>
</tr>';
}
?>
<?=$tableHeader?>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($resultadoQueryComp_Type)) {
$tableBody .= '<tr>
<td>'.$row['id'].'</td>
</tr>';
}
?>
<?=$tableBody?>
</tbody>
</table>
</div>
<form action="book.php" method="post">
<table>
<thead>
<tr>
<td>FlightID</td>
<td>From</td>
<td>Destination</td>
</tr>
</thead>
<tbody>
<tr>
<td name="flightID" value="1">1</td>
<td name="From" value="Sydney">Sydney</td>
<td name="Destination" value="Bali">Bali</td>
<td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
</tr>
<tr>
<td name="flightID" value="2">2</td>
<td name="From" value="London">London</td>
<td name="Destination" value="HongKong">Hong Kong</td>
<td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
</tr>
</tbody>
</table>
</form>
I created a table like this. At the end of each row, it has a book button.
What I am trying to do is when the user clicked the button, the selected row data(ID,From,Des) will pass to the 'book.php', then the PHP file will do the rest of the job.
But I tried to catch the value using $_POST['name'] in 'book.php', like this
<?php
if(isset($_POST['booking'])){
$ID = $_POST['flightID'];
$From = $_POST['From'];
$To = $_POST['Destination'];
}
?>
It shows all of those values are undefined. Any help would be appreciated.
The problem is that the values in <td> cannot be passed from the form to your PHP file by themselves. You could use hidden inputs for this. Additionally, each row in the table should be its own form to assure that all data is not submitted at the same time.
Try this:
<table>
<thead>
<tr>
<td>FlightID</td>
<td>From</td>
<td>Destination</td>
</tr>
</thead>
<tbody>
<tr>
<form action="book.php" method="post">
<td><input type="hidden" name="flightID" value="1">1</td>
<td><input type="hidden" name="From" value="Sydney">Sydney</td>
<td><input type="hidden" name="Destination" value="Bali">Bali</td>
<td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
</form>
</tr>
<tr>
<form action="book.php" method="post">
<td><input type="hidden" name="flightID" value="2">2</td>
<td><input type="hidden" name="From" value="London">London</td>
<td><input type="hidden" name="Destination" value="HongKong">Hong Kong</td>
<td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
</form>
</tr>
</tbody>
i have the same problem as yours and tried to create an answer so i came up with this code to indicate each row in an HTML table with a special name using loops, i can now take the specified row and do as much PHP operations as i can with it without disturbing the table as a whole and it was well synchronized with my database, hope it helps!
and btw the whole "marking each row with a special name" code is in usersTable.php
users.sql
create table users(
id int,
username varchar(50),
password varchar(50)
);
users.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";
$conn = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno()){
die("can't connect to the Database" . mysqli_connect_errno());
}else{
echo "Database is connected" . "<br>";
}
if (isset($_POST['insert'])){
$idN1= $_POST['id'];
$usernameN1 = $_POST['username'];
$passwordN1 = $_POST['password'];
$query = "insert into users(id, username, pass) values ('".$idN1."' , '".$usernameN1."' , '".$passwordN1."' )";
$result = mysqli_query($conn, $query);
}else if (isset($_POST['update'])){
$idN2 = $_POST['id'];
$usernameN2 = $_POST['username'];
$passwordN2 = $_POST['password'];
$query = "update users set pass = '". $passwordN2 ."'where id = " . $idN2;
$result = mysqli_query($conn, $query);
}else if (isset($_POST['Display'])){
header('Location: usersTable.php');
}
echo "<br>";
?>
<form method="post">
ID: <input type="text" name="id" ><br><br>
username: <input type="text" name="username" ><br><br>
password: <input type="password" name="password" ><br><br>
<input type="submit" name="insert" value="insert">
<input type="submit" name="Display" value="Display">
</form>
userTable.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";
$conn = mysqli_connect($host, $username, $password, $database);
$query = "select * from users";
$result = mysqli_query($conn, $query);
echo "<table border=\"6px\"><thead><tr><th>ID</th><th>username</th><th>password</th><th>Delete</th><th>Update</th></tr></thead>";
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><form method='post'><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>" . $row['pass'] . "</td><td><input type='submit' name='Delete" . $i . "' value='Delete'></td><td><input type='submit' name='Update" . $i . "' value='Update'><input type='text' name='UpdateText" . $i . "' placeholder='insert new password here'></td></form></tr>";
$i++;
}
echo "</table>";
$i = 1;
$result2 = mysqli_query($conn, $query);
while ($row2 = mysqli_fetch_assoc($result2)) {
if (isset($_POST['Delete' . $i])) {
$usernameN4 = $row2['username'];
$query2 = "delete from users where username ='" . $usernameN4 . "'";
$result2 = mysqli_query($conn, $query2);
header("Refresh:0");
break;
}
$i++;
};
$i = 1;
$result3 = mysqli_query($conn, $query);
while ($row3 = mysqli_fetch_assoc($result3)) {
if (isset($_POST['Update' . $i]) && $_POST['UpdateText' . $i] != null ) {
$id4 = $row3['id'];
$Utext = $_POST['UpdateText' . $i];
$query3 = "update users set pass ='" . $Utext . "' where id = " . $id4;
$result3 = mysqli_query($conn, $query3);
header("Refresh:0");
break;
}
$i++;
};
mysqli_free_result($result);
I'm doing a database project for university and I'm having a problem in here.
I receive from a previous page an id as $_POST['ids'] and in the form I send that same value in a hidden field so it can do a sort of a cicle.
But when I click the submit button I got a lot of errors on $service_info and no information is loaded on the page. I tried do var_dump() everything and I just can't find what is the problem in here.
<?php
//error_reporting();
require 'core/init.php';
require 'db/connect.php';
require 'functions/security.php';
?>
<html>
<head>
<title>Make a reservation</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/common.css">
</head>
<body>
<?php require 'parts/header.php'; ?>
<hr>
<?php
$query = "SELECT * FROM service WHERE id=" . $_POST['ids'];
if ($result = $db->query($query)) {
if ($result->num_rows) {
$service_info = $result->fetch_object();
$result->close();
}
}
$query = "SELECT name FROM tour WHERE id =" . $service_info->idtour;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$tour_name = $result->fetch_object();
$result->close();
}
}
$query = "SELECT SUM(nrseats) AS res_seats FROM reservation_service WHERE idservice =" . $service_info->id;
$nr_reservations_info = $db->query($query);
$nr_reservations = $nr_reservations_info->fetch_row();
$nr_reservations_info->close();
$count = $service_info->nrseats - $nr_reservations[0];
if($count === 0){
echo "<script>alert('There are no more places available for this tour. You are being redirected for the main page!')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else{
$count = $service_info->nrseats;
}
?>
<form action="" method="POST">
<div class="registering">
<table>
<tbody>
<tr>
<td>
<label for="tname">Related tour</label>
</td>
<td>
<label for="splace"><br>Service name</label>
</td><p><br></p>
</tr>
<tr>
<td>
<input type="text" readonly="" name="tour" id="tour" required="" autofocus="" value="<?php echo $tour_name->name ?>">
</td>
<td>
<input type="text" readonly="" name="name" id="name" required="" value="<?php echo $service_info->name ?>">
</td>
</tr>
<tr>
<td>
<label for="sprice"><br>Price (€)</label>
</td>
<td>
<label for="sdescription"><br>Description</label>
</td>
</tr>
<tr>
<td>
<input type="number" name="price" id="price" readonly="" required="" value="<?php echo $service_info->price ?>">
</td>
<td>
<input type="text" name="description" id="description" required="" readonly="" value="<?php echo $service_info->description ?>">
</td>
</tr>
<tr>
<td>
<label for="sseats"><br>Seats left</label>
</td>
<td>
<label for="snreservations"><br>Number of reservations (people)</label>
</td>
</tr>
</tr>
<tr>
<td>
<input type="number" name="nrseats" id="nrseats" required="" value="<?php echo $count ?>" readonly="">
</td>
<td>
<input type="number" name="nrreservations" id="nrreservations" required="" value="1">
</td>
<td>
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
</td>
</tr>
</tr>
<tr>
<td colspan="2">
<label for="next"><br></label>
<input type="submit" value="Next">
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
<?php
if (!empty($_POST)) {
if (isset($_POST['name'], $_POST['ids'], $_POST['tour'], $_POST['price'], $_POST['description'], $_POST['nrseats'], $_POST['nrreservations'])) {
$_POST = array_map("trim", $_POST);
$name = $_POST['name'];
$tour = $_POST['tour'];
$price = $_POST['price'];
$description = $_POST['description'];
$nrseats = $_POST['nrseats'];
$nrreservations = $_POST['nrreservations'];
$ids = $_POST['ids'];
if (!empty($name) && !empty($ids) && !empty($tour) && !empty($price) && !empty($description) && !empty($nrseats) && !empty($nrreservations)) {
$query = "SELECT id FROM customer WHERE email='" . $_SESSION['user_email'] . "'";
if ($result = $db->query($query)) {
$id_user = $result->fetch_object();
$result->close();
}
$query = "SELECT id FROM reservation WHERE idtour={$service_info->idtour} AND idcustomer={$id_user->id}";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$id_reservation = $result->fetch_object();
$result->close();
}
}
$query = "SELECT * FROM reservation_service WHERE idservice=" . $service_info->id;
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_service_exists = $result->fetch_object();
if ($nrreservations < 1) {
echo "<script>alert('Your must make a reservation for, at least, one person!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($count - $nrreservations < 0) {
echo "<script>alert('You can not make the reservation because there are only " . $count . " seats available in this tour!')</script>";
echo "<script>window.open('new_reservation_service.php','_self')</script>";
} else if ($result->num_rows) {
$query = "SELECT * FROM reservation WHERE idcustomer= '" . $id_user->id . "' AND idtour= '" . $service_info->idtour . "'";
if ($result = $db->query($query)) {
if ($result->num_rows) {
$reservation_exists = $result->fetch_object();
$result->close();
if ($reservation_exists->idcustomer === $id_user->id) {
if ($reservation_exists->id === $reservation_service_exists->idreservation) {
echo "<script>alert('You already made a reservation for this service. Please see your reservation panel!')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
}else {
$query = "INSERT INTO reservation_service (idreservation, idservice, date, nrseats) VALUES (?, ?, NOW(), ?)";
$insert = $db->prepare($query);
$insert->bind_param('iii', $id_reservation->id, $service_info->id, $nrreservations);
$insert->execute();
echo "<script>alert('You successfully made a reservation! You are being redirected to your reservations page')</script>";
echo "<script>window.open('reservations.php','_self')</script>";
}
}
}
}
}
?>
change inside your form this input hidden you created:
<input type="hidden" name="ids" required="" value="<?php $service_info->id ?>">
to
<input type="hidden" name="ids" required="" value="<?php echo $service_info->id ?>">
If you don't echoing this value, $_POST['ids'] won't be get any value passed from form.
I tried to create a button which the function is to sort data either descending or ascending. However, I don't have idea how to do it
I did some research in internet, but none of them give the answer.
anyone know how to do it or some source code which can be a references???
this is my code
test.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="formSubmit" value-"Submit">
</td>
</table>
</form>
</body>
</html>
showDB.php
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if($_POST['formSubmit'] == "Submit")
{
$country = $_POST['country'];
}
//query the database
if($country == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'");
}
elseif($country == 'NZ') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'");
}elseif($country == 'JP') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'");
} else {
// query to get all records
$query = mysql_query("SELECT * FROM auip_wipo_sample");
}
//fetch the result
Print "<table border cellpadding=3>";
//ascending descending button
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print "<td>".$row['invention_title'] . "</td>";
Print "<td>".$row['invention-title'] . " </td></tr>";
}
//sorting the data, I got from internet but doesn't work
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
{
$query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title ASC";
}else{
$query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title DESC";
}
Print "</table>";
?>
Change this:
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";
To
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value='Asc'></input></th></tr>";
And this
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
to
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']))
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th colspan="3">test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
<td><input type="checkbox" name="asc" value="1" /> Ascending?</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="formSubmit" value-"Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Few things:
You were pulling a query statement in the middle of a while where
you're still retrieving the previous statement. This would require
pulling the data into multiple arrays and then processing.
You should be using something like pdo or mysqli for this, but here's an
example with your current methods.
Also you were using isset on POST... if POST has anything in it, it will return as set.
Referencing a POST variable that is not set will cause an error, So I'm checking to make sure that country is set then I'm provided a NULL answer to the switch.
Example Code:
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if(!empty($_POST['formSubmit'])&&($_POST['formSubmit']=="Submit")&&(!empty($_POST['country']))){
$country = $_POST['country'];
} else {
$country = '';
}
if(!empty($_POST['asc']))
{
$append = " ORDER BY invention_title ASC";
}else{
$append = " ORDER BY invention_title DESC";
}
switch($country){
case 'US':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'$append";
break;
case 'NZ':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'$append";
break;
case 'JP':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'$append";
break;
default:
//all records
$query = "SELECT * FROM auip_wipo_sample$append";
}
//query the database
if($result = mysql_query($query)){
//fetch the result
print "<table border cellpadding=3>";
while($row = mysql_fetch_array($result))
{
print "<tr>";
print "<td>".$row['invention_title'] . "</td>";
print "<td>".$row['invention-title'] . " </td></tr>";
}
//sorting the data, I got from internet but doesn't work
print "</table>";
} else {
//For Testing
echo "Query Failed:<br />$query";
}
?>