Jump to content

Passing value using radio button and update the database


moonjazz

Recommended Posts

Hi there.

I have this course registering website whereby i will need to :

1. pull out all course info and display (done that)

2. when user choose the course they are interested in using radio button and click submit , it will display you're registered and update the database to decrease the head count by 1.

 

i m stuck on the radio button part... after i click submit nothing happens...

 

Please advice... thanks a lot

 

 

<html>
<head>
  <title>TimeTable</title>

</head>

<body>
<h1> Here is the timetable</h1>

<?php


     
  @ $db = new mysqli('localhost', 'root', '', 'mylifestyle');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }
  
  $query = "SELECT course.CourseName , course.Instructor , course.ClassSize , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID";
  $result = $db->query($query);
  $num_results = $result->num_rows;
  
  
  echo "<table border='1'>
  <tr>
  <th>Course Name</th>
  <th>Instructor</th>
  <th>Class size</th>
  <th>Date</th>
  <th>Time</th>
  <th>Location</th>
  <th>Register</th>
  </tr>"; ?>

<?php 

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();?>
  <tr>
      <form action = ""
          method = "post">
<td><? echo $row['CourseName']; ?></td>
<td><? echo $row['Instructor']; ?></td>
<td><? echo $row['ClassSize']; ?></td>
<td><? echo $row['Date']; ?></td>
<td><? echo $row['Time']; ?></td>
<td><? echo $row['Location']; ?></td>


<td align="center"><input name="radio" type="radio" id="radio[]" value="<? echo $row['ClassSize']; ?>"></td>
</tr>
<?php
}
?>

<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="submit" type="submit" id="submit" value="submit"></td>

  
<?php




if($submit){
for($i=0;$i<$num_results;$i++){
$del_id = $radio[$i];
$query = "UPDATE course SET ClassSize = ClassSize -1 Where id='$del_id'";
$result = mysql_query($query);
}

// if successful redirect to delete_multiple.php
if($result){

echo  "$CourseName was deleted from database - myliftstyle!!<br />";

// Compute the item costs and total cost


  }
  }



    ?>

   </div>
</body>
</html>

 

[attachment deleted by admin]

Link to comment
Share on other sites

if($submit) is meaningless if you don't assign the value of $_POST['submit'] to $submit.

 

However, you really shouldn't depend on the submit button's value being present to see if a form has been submitted since some browsers mishandle that value. You'd be better off to either set a hidden field, and check for its value, or check to see if strtolower($_SERVER['REQUEST_METHOD']) === 'post' (as long as the form's method="post", that is).

Link to comment
Share on other sites

Thanks for the respond...

i have made some changes but it seems like the database is not updated..

am i doing it right?

 

Please advice..

 

<html>
<head>
  <title>TimeTable</title>

</head>

<body>
<h1> Here is the timetable</h1>

<?php


     
  @ $db = new mysqli('localhost', 'root', '', 'mylifestyle');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }
  
  $query = "SELECT course.CourseName , course.Instructor , course.ClassSize , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID";
  $result = $db->query($query);
  $num_results = $result->num_rows;
  
  
  echo "<table border='1'>
  <tr>
  <th>Course Name</th>
  <th>Instructor</th>
  <th>Class size</th>
  <th>Date</th>
  <th>Time</th>
  <th>Location</th>
  <th>Register</th>
  </tr>"; ?>

<?php 

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();?>
 <form method="post" action="haha.php">
  <tr>
   
<td><? echo $row['CourseName']; ?></td>
<td><? echo $row['Instructor']; ?></td>
<td><? echo $row['ClassSize']; ?></td>
<td><? echo $row['Date']; ?></td>
<td><? echo $row['Time']; ?></td>
<td><? echo $row['Location']; ?></td>


<td align="center"><input name="radio" type="radio" id="radio[]" value="<? echo $row['ClassSize']; ?>"></td>
</tr>
<?php
}
?>

<p>
     <form method="post" action="<?php echo $PHP_SELF;?>">

   <td> <input type = "submit"  name="reg" value="Register" /></td>

        <input type = "hidden" name="formVar" value="<?php echo $ClassSize?>" />
        </form>
</p>


   


   </div>
</body>
</html>

 

 

haha.php

<?php


if (!empty($_POST['reg']))
{
$myvarA = $_POST['formVar'];
$ClassSize=$myvarA;
//echo "myvarA= "." $myvarA". "</br>";


  if (!get_magic_quotes_gpc()) 
    {
    $ClassSize = addslashes($ClassSize);
    }

  @ $db = new mysqli('localhost', 'root', '', 'mylifestyle');

  if (mysqli_connect_errno()) 
    {
     echo "Error: Could not connect to database.  Please try again later.";
     exit;
    } 
for($i=0;$i <$num_results;$i++){
$del_id = $radio[$i];
$query = "UPDATE course SET ClassSize = ClassSize -1 Where id='$del_id'";
$result = $db->query($query);
  
  if ($result) {
                 echo  "u r reg!<br />";
                 } 
			   $db->close();
  
  }
}

    ?>

Link to comment
Share on other sites

My 2p...

 

It seems to me as though you are copying and pasting code all over the place, you need to re-look at the Time Table file and remove the nested form element start.

 

Also, you shouldn't be passing the class size ($row['ClassSize']) to the file "haha.php", you should in fact pass the class ID.

Once you have sent the class ID, you should then update the corresponding record set and decrease the class size by 1.

 

Regards, PaulRyan.

Link to comment
Share on other sites

Hello...

i have done some changes

i do a GET to see whats the value of ClassSize and HeadCount

but each time after i hit the register button

the url shows

http://localhost/xampp/1/conreg.php?radio=COM01&Submit=Register&ClassSize=&HeadCount=

ClassSize and HeadCount has no value...

 

May i know whats causing the problem ? thanks very much

 

<html>
<head>
  <title>TimeTable</title>

</head>

<body>
<h1> Here is the timetable</h1>

<?php

  @ $db = new mysqli('localhost', 'root', '', 'mylifestyle');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }
  
  $query = "SELECT course.CourseCode, course.CourseName , course.Instructor , course.ClassSize , courseschedule.HeadCount , courseschedule.Date ,courseschedule.Time , courseschedule.Location FROM course INNER JOIN courseschedule ON course.CourseID = courseschedule.CourseID";
  
  $result = $db->query($query);
  $num_results = $result->num_rows;
  
  
  echo "<table border='1'>
  <tr>
  <th>CourseID</th>
  <th>Course Name</th>
  <th>Instructor</th>
  <th>HeadCount</th>
  <th>Class size</th>
  <th>Date</th>
  <th>Time</th>
  <th>Location</th>
  <th>Register</th>
  </tr>"; ?>

<?php 
while($row = $result->fetch_assoc()) {
     ?>
  <tr>
  <form name="form1" method="get" action="conreg.php"> 

<td><? echo $row['CourseCode']; ?></td>
<td><? echo $row['CourseName']; ?></td>
<td><? echo $row['Instructor']; ?></td>
<td><? echo $row['HeadCount']; ?></td>
<td><? echo $row['ClassSize']; ?> </td>
<td><? echo $row['Date']; ?></td>
<td><? echo $row['Time']; ?></td>
<td><? echo $row['Location']; ?></td>

<td align='center'><input name='radio' type='radio' value=<? echo $row['CourseCode'];?> onClick='document.Submit.formVar.value='.$row['CourseName'].'>  </td>




</tr>
<?php
}
    $db->close();

?>

<td><input type="submit" name="Submit" value="Register"></td>

<input type="hidden" name="ClassSize" value="<? echo $row['ClassSize']; ?>"/>
<input type="hidden" name="HeadCount" value="<? echo $row['HeadCount']; ?>"/>



   </form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.