PDA

View Full Version : query using radio button value


red_rose
02-24-2007, 10:46 PM
hello there

im having problem with a page. actually the basic function of this code is reservation. so i have this php page, which catches the institution id and displays the accommodation available in reservation.php.

in reservation, i get the data from db and display it in rows with a radio button for each row. this is because a student can only have one reservation. then i have reserve button at the bottom of the page to submit all the data to update and insert into db.

my problem is, when i click reserve button, the code does not function. i can only see a page with the table titles and the button after i click the button. means my code is not working. could anyone help me in this? is my radio button value correct? thanks alot

reservation.php :


<?php

/////////////////////////////////////
$database = "services";
$username = "root";
$password = "**
/////////////////////////////////////
$link = @mysql_connect('localhost', $username, $password);
$db = mysql_select_db($database, $link);

session_start();

$tm=date("Y-m-d");
$selectReserve = $_POST['selectReserve'];

if(isset($_SESSION['userid']))
{
$userid = $_SESSION['userid'];
if(isset($_GET['id']))
{
$id = $_GET['id'];
////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM accommodation where status = 'Available' and Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
$refNo = $row['AccRefNo'];
////////////////////////////////////////////////////////////////////////////////////////////////
$query1 = sprintf("SELECT * FROM student where StdUsername = '$userid'");
$result1 = @mysql_query($query1);
$row1 = @mysql_fetch_array($result1);
$stdId = $row1['StudentId'];


if(isset($_POST['reserve']))
{
if($selectReserve != '')
{
echo $update = "update `accommodation` set status = 'Unavailable' where AccRefNo = '$selectReserve'";
$result_update = @mysql_query($update);
$row_update = @mysql_fetch_array($result_update);

echo $sql = "insert into reservation (`ReservationId`, `Reservation_date`, `Status`, `StudentId`, `AccRefNo`)
values (NULL, '$tm', 'Unavailable', '$stdId', '$selectReserve')";
$result_sql = @mysql_query($sql);
$row_sql = @mysql_fetch_array($result_sql);

}
else
{
echo "You have not selected any accommodation";
}
}
}
}
else
{
header("location:login.php");
exit;
}
?>

<html>
<head>
<title>:: Reservation ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<form name="form1" method="post" action="reservation.php">
<table width="88%" border="2" cellspacing="2" cellpadding="2">
<tr>
<td width="26%">Selection</td>
<td width="14%">Picture</td>
<td width="11%">Description</td>
<td width="13%">Price (RM)</td>
<td width="17%">Landlord</td>
<td width="19%">Status</td>

</tr>

<?php
do
{
echo "<tr>";
echo '<td><input name="selectReserve" type="radio" value='.$row['refNo'].'></td>';
echo '<td><img src="images/'.$row['picture'].'"></td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>'.$row['LdlordUsername'].'</td>';
echo '<td>'.$row['Status'].'</td>';
echo "</tr>";
}
while($row = @mysql_fetch_array($result));
?>

</table>
<input name="reserve" type="submit" value="Reserve">
</form>
</body>
</html>

davidj
02-24-2007, 11:00 PM
first

please drop your connection script into a seperate file and just include it in every page that needs it. Far more economical this way

/////////////////////////////////////
$database = "services";
$username = "root";
$password = "**
/////////////////////////////////////
$link = @mysql_connect('localhost', $username, $password);
$db = mysql_select_db($database, $link);

davidj
02-24-2007, 11:04 PM
you are looping through the table using the first SELECT

davidj
02-24-2007, 11:08 PM
also your IF is using a $_GET
if(isset($_GET['id']))


but when you click the button the form is using a post method so the selects will never fire

red_rose
02-26-2007, 03:01 PM
i have done some modification to the code. i took out this coding :


if(isset($_POST['reserve']))


and wrote it as the beginning of the new IF

but when i select an accommodation and click the button :


echo "You have not selected any accommodation";


this line is displayed

this means the if($selectReserve != '') is not catching the value. is something wrong with this coding?

below is the coding after modification :


<?php

session_start();

$tm=date("Y-m-d");
$selectReserve = $_POST['selectReserve'];

if(isset($_SESSION['userid']))
{
$userid = $_SESSION['userid'];
if(isset($_GET['id']))
{
$id = $_GET['id'];
////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM accommodation where status = 'Available' and Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
$refNo = $row['AccRefNo'];
////////////////////////////////////////////////////////////////////////////////////////////////
$query1 = sprintf("SELECT * FROM student where StdUsername = '$userid'");
$result1 = @mysql_query($query1);
$row1 = @mysql_fetch_array($result1);
$stdId = $row1['StudentId'];
}
}
else
{
header("location:login.php");
exit;
}
if(isset($_POST['reserve']))
{
if($selectReserve != '')
{
echo $update = "update `accommodation` set status = 'Unavailable' where AccRefNo = '$selectReserve'";
$result_update = @mysql_query($update);
$row_update = @mysql_fetch_array($result_update);

echo $sql = "insert into reservation (`ReservationId`, `Reservation_date`, `Status`, `StudentId`, `AccRefNo`)
values (NULL, '$tm', 'Unavailable', '$stdId', '$selectReserve')";
$result_sql = @mysql_query($sql);
$row_sql = @mysql_fetch_array($result_sql);

}
else
{
echo "You have not selected any accommodation";
}
}

?>



for the table :


<?php
do
{
echo "<tr>";
echo '<td><input name="selectReserve" type="radio" value='.$row['refNo'].'></td>';
echo '<td><img src="images/'.$row['picture'].'"></td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '<td>'.$row['LdlordUsername'].'</td>';
echo '<td>'.$row['Status'].'</td>';
echo "</tr>";
}
while($row = @mysql_fetch_array($result));
?>


is the radio button value correct?

thanks for your help

davidj
02-26-2007, 03:54 PM
you only have one radio button. Is this correct? (bad practice using one radio button!!)

it is called selectReserve and is not initially checked? so no value will be sent unless the user checks the radio button.


The value of the radio button is derived from $row['refNo']and this is defined by the first SQL Select statement. This statement is wrapped in an IF...


if(isset($_GET['id']))
{
$id = $_GET['id'];
////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM accommodation where status = 'Available' and Inst_Id = '$id'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
$refNo = $row['AccRefNo'];

and as you can see its conditional on a GET['id']. But im thinking your are using a POST method on your form so this IF will never be true

red_rose
02-26-2007, 04:08 PM
since the rows are in do while loop, there will be many radio buttons. one user can reserve only one accommodation, that's why i chose radio buttons.

ok, so for the value of the radio button, i derived from the first SELECT statement. this is because i want to have a unique id for each of the radio button. then when the user select one radio button, the radio button name ($selectReserve) will catch the value and will be used to execute UPDATE and INSERT queries. im not sure whether my concept is correct.

what do you think about my coding? how should i modify or rearrange it? could you guide me on this? thank you so much for your help

davidj
02-26-2007, 04:12 PM
sorry

i missed the do while !

davidj
02-26-2007, 04:16 PM
ok

start debugging by echoing inside the IF's


if(isset($_POST['reserve']))

echo "hello";

{
if($selectReserve != '')
{


make sure its getting into where it needs to.

Then echo the required vars to see if they are populated

Its just trial and error. Process of elimination.

Work your logic out on paper and trace it through your code

red_rose
02-26-2007, 04:53 PM
hey dj, i have solved the problem !! :-D :-D :-D

actually the $row['refNo'] was incorrect, it was suppose to be $row['AccrefNo'] :( im sorry i troubled u again.

and yeah, the second SELECT statement was suppose to be inside if(isset($_POST['reserve'])), then only it will catch the $StdId value, if not it will not execute the INSERT query. echo-ing sql statements really do help.

so yes, finally i've done it !! thank you so so much for your help, i really really appreciate it !!