Hey,guys!

I have a form which have some hidden fields because those fields' value need to be displayed in next page by using $_GET.However,I also need to store them into database while form submitted,but my query statement didn't work,what went wrong with it?

<?
$tbl_name = "reserve";
if (isset($_GET['confirm'])){
$insert = mysql_query("INSERT INTO $tbl_name (reserve_date,reserve_time,seat_qty,ref_code)VALUES ('" . $_GET['selected_date'] . "','" . $_GET['showtime'] . "','" . $_GET['noOfSeat'] . ".,'" . $_GET['refCode'] . "')") or die(mysql_error()); 

}
?>
<tr>
    <td><strong>Showtime</strong></td> 
    <td><?php echo $time; ?></td> 
     <input type="hidden" name="showtime" value="<?php echo $time; ?>">
</tr> 

<tr>
      <td><strong>Date</strong></td> 
      <td><?php echo $_GET['selected_date']; ?></td>
      <input type="hidden" name="selected_date" 
       value="<?php $_GET['selected_date']; ?>">
</tr> 

<tr> 
     <td><font face="Arial"><strong>No. of Seats</strong></font></td>
     <td><font face="Arial"><?php echo $noOfSeat; ?></td>
     <input type="hidden" name="noOfSeat" 
      value="<?php echo $noOfSeat; ?>">
</tr>

<tr>
     <td>
     <input type="hidden" name="refCode" value="<?php echo $refCode; ?>">
     </td>
</tr>

RefCode is generated in current page but only displays in next page.

    What type of error are you getting?

    When looking through your query it looks a little bit strange where you have:

    "','" . $_GET['noOfSeat'] . ".,'" 
    

    Maybe it should be:

    "','" . $_GET['noOfSeat'] . ",'" 
    

    Notice that I removed a . before your comma within the quotation.

      Well, I changed already.However values still not insert to database.It doesn't show any error,all values are displayed in next page but not store into database.

        Well, I changed already.However values still not insert to database.It doesn't show any error,all values are displayed in next page but not store into database.

        Is it because other two more foreign keys which I didn't know how to refer any value so query is not well established?

        CREATE TABLE `reserve` (
          `reserve_id` int(4) NOT NULL auto_increment,
          `reserve_date` date NOT NULL,
          `reserve_time` time NOT NULL,
          `seat_qty` int(2) NOT NULL,
          `ref_code` varchar(50) collate latin1_general_ci NOT NULL,
          `movie_id` int(4) NOT NULL,
          `member_id` int(4) NOT NULL,
          PRIMARY KEY  (`reserve_id`),
          KEY `movie_id` (`movie_id`),
          KEY `member_id` (`member_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;
        
        

          Maybe no value being assigned to two foreign keys(movie_id and member_id) because I not sure how to refer it.So,I rewrite the query statement as shown below BUT data still CAN'T store in database.Hope someone can help me...

          <? 
          
          if (isset($_GET['confirm'])){  
          $sql = 'INSERT INTO `reserve` SET'; $sql .= ' `reserve_date` = \''.mysql_real_escape_string($_GET['selected_date']).'\''; $sql .= ', `reserve_time` = \''.mysql_real_escape_string($_GET['showtime']).'\''; $sql .= ', `seat_qty` = '.((int)$_GET['noOfSeat']); $sql .= ', `ref_code` = \''.mysql_real_escape_string($_GET['refCode']).'\''; $sql .= ', `movie_id` = 1'; // I just put 1 so it would actually do something. $sql .= ', `member_id` = 1'; // I just put 1 so it would actually do something. mysql_query($sql) or die(mysql_error()); } ?>

            Just to make sure you are executing the query statement

            mysql_query($sql) or die(mysql_error());

            enter a echo statement on the row before. This will also help you verify the query string.

            echo $sql;
            mysql_query($sql) or die(mysql_error());

              Well,I have tried this code

              <?php 
              //$tbl_name = "reserve"; 
              if (isset($_GET['confirm'])){ 
                  $sql = 'INSERT INTO `reserve` SET'; 
                  $sql .= ' `reserve_date`=                    \''.mysql_real_escape_string($_GET['selected_date']).'\'';
                  $sql .= ', `reserve_time` = \''.mysql_real_escape_string($_GET['showtime']).'\'';
                  $sql .= ', `seat_qty` = '.((int)$_GET['noOfSeat']); 
                  $sql .= ', `ref_code` = '.((int)$_GET['refCode']); 
                  $sql .= ', `movie_id` = 1'; //I just put 1 so it would actually do something. 
                  $sql .= ', `member_id` = 1'; // I just put 1 so it would actually do something. 
              
                  print "SQL STATEMENT: "; 
                  var_dump($sql); 
              
              $result = mysql_query($sql); 
              
                  print "MYSQL RETURN: "; 
                  var_dump($result); 
              
                  print "MYSQL ERROR: "; 
                  var_dump(mysql_error()); 
              } else { 
                      print "FORM NEVER SENT confirm VALUE"; 
              } 
              ?>
              

              These are what had displayed
              FORM NEVER SENT confirm VALUE

              SQL STATEMENT: string(155) "INSERT INTO reserve SET reserve_date = 'Sun 30th Sep', reserve_time = '6 :00pm', seat_qty = 3, ref_code = 451910, movie_id = 1, member_id = 1" MYSQL RETURN: bool(true) MYSQL ERROR: string(0) ""

              Warning: Invalid argument supplied for foreach()

              <? 
              // key-value pair of time array 
              foreach ($_GET['time'] as $key => $value) { 
                  $name = $key; 
                  $time = $value; 
              } 
              ?>
              

              'time' is derived from previos page(reservation2.php)

              <? 
              if (isset($_SESSION['gmemberid'])) { 
              
              $tbl_name = "movie"; 
              $result = mysql_query(sprintf('SELECT name,classification,screeningTime FROM %s 
                       LIMIT 7', $tbl_name)) or die('Cannot execute query.'); 
              
              
              //$numrow = mysql_num_rows($result); 
              
              
              while ($rows = mysql_fetch_assoc($result)) { 
                  echo '<table width="100%" border="0"><tr><td height="68"> 
                            <table width="100%" height="47" border="0"> 
                             ---------------------------------------------------------------------------------------------------------<br>'; 
              
                  echo '<strong>' . $rows['name'] . ' (' . $rows['classification'] . ') 
                           <br></strong>'; 
                  foreach (explode(',', $rows['screeningTime']) as $time) { ?> 
                      <label> 
                      <input type="radio"  name="time[<?php echo $rows['name']; ?>]" 
                        title ="screening time" value="<?php echo $time; ?>"> 
                       <input type="hidden" name="selected_date" 
                          value="<?php echo $_GET['selected_date']; ?>"> 
              
                      <?php echo $time; ?>&nbsp;&nbsp;&nbsp; 
                      </label> 
                      <?php } ?> 
              <? 
                  } 
              
              } 
              ?>
              

                Well,I have tried this code

                <?php 
                //$tbl_name = "reserve"; 
                if (isset($_GET['confirm'])){ 
                    $sql = 'INSERT INTO `reserve` SET'; 
                    $sql .= ' `reserve_date`=                    \''.mysql_real_escape_string($_GET['selected_date']).'\'';
                    $sql .= ', `reserve_time` = \''.mysql_real_escape_string($_GET['showtime']).'\'';
                    $sql .= ', `seat_qty` = '.((int)$_GET['noOfSeat']); 
                    $sql .= ', `ref_code` = '.((int)$_GET['refCode']); 
                    $sql .= ', `movie_id` = 1'; //I just put 1 so it would actually do something. 
                    $sql .= ', `member_id` = 1'; // I just put 1 so it would actually do something. 
                
                    print "SQL STATEMENT: "; 
                    var_dump($sql); 
                
                $result = mysql_query($sql); 
                
                    print "MYSQL RETURN: "; 
                    var_dump($result); 
                
                    print "MYSQL ERROR: "; 
                    var_dump(mysql_error()); 
                } else { 
                        print "FORM NEVER SENT confirm VALUE"; 
                } 
                ?>
                

                These are what had displayed
                FORM NEVER SENT confirm VALUE

                SQL STATEMENT: string(155) "INSERT INTO reserve SET reserve_date = 'Sun 30th Sep', reserve_time = '6 :00pm', seat_qty = 3, ref_code = 451910, movie_id = 1, member_id = 1" MYSQL RETURN: bool(true) MYSQL ERROR: string(0) ""

                Warning: Invalid argument supplied for foreach()

                <? 
                // key-value pair of time array 
                foreach ($_GET['time'] as $key => $value) { 
                    $name = $key; 
                    $time = $value; 
                } 
                ?>
                

                'time' is derived from previos page(reservation2.php)

                <? 
                if (isset($_SESSION['gmemberid'])) { 
                
                $tbl_name = "movie"; 
                $result = mysql_query(sprintf('SELECT name,classification,screeningTime FROM %s 
                         LIMIT 7', $tbl_name)) or die('Cannot execute query.'); 
                
                
                //$numrow = mysql_num_rows($result); 
                
                
                while ($rows = mysql_fetch_assoc($result)) { 
                    echo '<table width="100%" border="0"><tr><td height="68"> 
                              <table width="100%" height="47" border="0"> 
                               ---------------------------------------------------------------------------------------------------------<br>'; 
                
                    echo '<strong>' . $rows['name'] . ' (' . $rows['classification'] . ') 
                             <br></strong>'; 
                    foreach (explode(',', $rows['screeningTime']) as $time) { ?> 
                        <label> 
                        <input type="radio"  name="time[<?php echo $rows['name']; ?>]" 
                          title ="screening time" value="<?php echo $time; ?>"> 
                         <input type="hidden" name="selected_date" 
                            value="<?php echo $_GET['selected_date']; ?>"> 
                
                        <?php echo $time; ?>&nbsp;&nbsp;&nbsp; 
                        </label> 
                        <?php } ?> 
                <? 
                    } 
                
                } 
                ?>
                

                  First, you have duplicated posts.
                  Secondly, it does appear as if your query went through OK since the result is TRUE.
                  Do you have command line access to the database.
                  If so I would try to enter your query manually at the prompt and then inspect the table.
                  Something is definitely amiss.

                    Sorry for duplication posts.Well,I have changed again,

                    
                    <?php 
                    if (isset($_GET['confirm'])){ 
                        $selected_date = mysql_real_escape_string($_GET['selected_date']); 
                        $reserve_time = mysql_real_escape_string($_GET['showtime']); 
                        $noOfSeat = (int) $_GET['noOfSeat']; 
                        $refCode = (int) $_GET['refCode']; 
                        $sql = <<<SQLSTM
                            INSERT 
                                INTO `reserve` 
                                SET 
                                    reserve_date = '$selected_date', 
                                    reserve_time = '$reserve_time', 
                                    seat_qty = $noOfSeat, 
                                    ref_code = $refCode, 
                                    movie_id = 1,
                                    member_id = 1
                    SQLSTM;
                        if($res = mysql_query($sql)) 
                        { 
                            echo('Record inserted successfully blah blah blah'); 
                        } 
                        else 
                        { 
                            die($sql.' '.mysql_error()); // For debugging only 
                            // handle error gracefully in production 
                        } 
                    }
                    ?>
                    
                    

                    When having action=reservation3.php,it displayed Record inserted successfully blah blah blah,however why mt_rand() always remain the same value in multiple records?

                    if (!isset($_SESSION['refCode'])) { 
                        $_SESSION['refCode'] = mt_rand(100000, 999999); 
                    } 
                    $refCode = $_SESSION['refCode'];
                    

                    And also what actually want the form to submitted and display all the value,including refCode in NEXT PAGE(reservation4.php).However,when putting action=reservation4.php, old problem raise back,no new record found in database.

                      I don't think I can help you.
                      I am too much of a newbie in this environment anyway.
                      But for the next person along.

                      Are you saying that your databased is updated in certain circumstances?

                      I was under the assumption that even if your sql query ran through, your database was not updated which is a totally different scenario.

                      It is also not obvious what the difference between reservation3.php and reservation4.php is.

                      If I understand you right the database is updated using reservation3.php but not reservation4.php.
                      Is that right?

                      Since it appears that your queries are correct i would focus on the conditions where you are evaluating if the script should execute the queries or not.
                      Maybe a simple diff is enough.

                        Ya.reservation3.php responsible for updating database with 3 values which get from previous page(reservation2.php)+generated refCode.

                        reservation4.php just display the updated record in the screen.

                        Any idea on it?

                          Write a Reply...