PHP submit not working

Posted on 2011-05-05
Last Modified: 2013-12-12
I have run into a problem with a PHP page I put together and after staring at it for a couple of hours, I am stumped.  I put together a pretty simple dbase review and submit page that I thought worked, but edits made aren't ending up in the database.

I attached the page code.

I have a similar page that updates a menu and it works fine, so I figure the dbase itself is fine.  The information shows up on the page out of the database correctly also.

Second part of my question - how would I insert error handling code on this page to show me any errors that may be getting returned from the query?  The hoster of the dbase does not keep the SQL logs on this particular shared server.  

Lastly - I am a newbie with PHP and MySQL - why is the param in this line roomName?  
$size = count($_POST['roomName']);
Should this be roomID?  Needless to say, I got the actual code snippet from another contributor on this site and it worked wonderfully on the menu page.  I thought I understood what the parameter did when I adapted the code to this page, but now I think I don't.

Thanks in advance!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Menu Maintenance :: Edit Rooms &amp; Cottages</title>
<link href="" rel="stylesheet" type="text/css" />
<meta name="keywords" content="lodging,accommodation,travel,tourism,Saint John,New Brunswick,NB,N.B.,Grand Manan,Bay of Fundy,private, housekeeping,cottage,rental,Inn,gourmet,restaurant,dining,room,whale,watching,bird,hiking,horseback,riding,kayaking,biking" />

<script language = "Javascript">
 * DHTML check all/clear all links script. Courtesy of (

var form='form1' //Give the form name here

function SetChecked(val,chkName) {
len = dml.elements.length;
var i=0;
for( i=0 ; i<len ; i++) {
if (dml.elements[i].id==chkName) {

function ValidateForm(dml,chkName){
len = dml.elements.length;
var i=0;
for( i=0 ; i<len ; i++) {
if ((dml.elements[i].id==chkName) && (dml.elements[i].checked==1)) return true
alert("Please select at least one record to be deleted")
return false;
// -->


<body><table width="800" border="0" cellpadding="0">
    <td align="center" valign="top"><p><a href=""><img src="../pictures/logo.png" width="190" height="224" border="0" /></a></p>
      <?php include("../includes/leftmenu.html"); ?>
      <p><br />
        <br />
        <!--- weather addin --->
      <div style='width: 120px; height: 60px; background-image: url( ); background-repeat: no-repeat; background-color: #346797;' >
        <div id='NetweatherContainer' style='height: 48px;' >
          <script src=';tStyle=whteYell&amp;logo=0&amp;zipcode=NAM|CA|NB|GRAND MANAN|&amp;lang=eng&amp;size=7&amp;theme=blue&amp;metric=1&amp;target=_self'></script>
        <div style='text-align: center; font-family: arial, helvetica, verdana, sans-serif; font-size: 10px; line-height: 12px; color: #FFFFFF;' ><a style='color: #FFFFFF' href=';locCode=NAM|CA|NB|GRAND MANAN|&amp;metric=1' target='_blank' >Weather Forecast</a></div>
    <td align="center" valign="top">

   <!--- begin content  --->
 <table width="80%">
       <td valign="top"><center>
         <h1>Menu Maintenance: <br />
         </b></font><font size="3" color="#8e236b"><b>Edit Rooms &amp; Cottages</b></font></h1>
 <table width="610">
   <tbody><tr><td valign="top" align="left">
   <table width="100%" align="center"><tr><td valign="top" align="center"> 

// Connect to server and select databse.
@mysql_select_db($database) or die( "Unable to select database"); 
// Check if button name "Submit" is active, do this  
if(isset($_POST['Submit'])) { 
        $i = 0; 
        $size = count($_POST['roomName']); 
        while($i < $size) { 
                $roomID = $_POST['roomID'][$i]; 
                $roomName = $_POST['roomName'][$i]; 
                $roomPriceSing = $_POST['roomPriceSing'][$i]; 
                $roomPriceDouble = $_POST['roomPriceDouble'][$i]; 
                $roomPriceWeek = $_POST['roomPriceWeek'][$i]; 
		$roomPriceNight = $_POST['roomPriceNight'][$i]; 
		$roomPriceNightM_J = $_POST['roomPriceNightM_J'][$i]; 
		$roomPriceNightJ_S = $_POST['roomPriceNightJ_S'][$i]; 
                $sql1="UPDATE $tbl_name2 SET roomName='$roomName', roomPriceSing='$roomPriceSing', roomPriceDouble='$roomPriceDouble', roomPriceWeek='$roomPriceWeek', roomPriceNight='$roomPriceNight', roomPriceNightM_J='$roomPriceNightM_J', roomPriceNightJ_S='$roomPriceNightJ_S' WHERE roomID='$roomID'"; 
        echo 'Database Updated'; 
$sql="SELECT * FROM $tbl_name2 ORDER BY `roomName` ASC"; 
// Count table rows  


<form name="form1" method="post" action=""> 
     <table width="610" border="0" cellspacing="1" cellpadding="0"> 
          <p align="center"><b>You need to click Submit to make the changes!</b></p>
           <table width="100%" border="0" cellspacing="1" cellpadding="0"> 
               <td align="center">&nbsp;</td>
               <td align="center">&nbsp;</td>
               <td colspan="6" align="center" bgcolor="#CCCCCC"><h2>Pricing</h2></td>
               <td align="center"><strong></strong></td> 
               <td align="center"><strong>Room Name</strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Single </strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Double </strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Week</strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Night</strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Night<br /> <span style="font-size:.6em">(May-June)</span></strong></td> 
               <td align="center" bgcolor="#CCCCCC"><strong>Night<br /> <span style="font-size:.6em">(July-Sept)</span></strong></td> 
$i = 0; 
echo '<tr> 
<td align="center"><input type="hidden" name="roomID['.$i.']" value="'.$rows['roomID'].'" /></td> 
<td align="left" nowrap><label>'.$rows['roomName'].'</label>&nbsp;&nbsp;</td> 
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceSing['.$i.']" type="text" size="6" id="roomPriceSing" value="'.$rows['roomPriceSing'].'"></td>
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceDouble['.$i.']" type="text" size="6" id="roomPriceDouble" value="'.$rows['roomPriceDouble'].'"></td> 
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceWeek['.$i.']" type="text" size="6" id="roomPriceWeek" value="'.$rows['roomPriceWeek'].'"></td>
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceNight['.$i.']" type="text" size="6" id="roomPriceNight" value="'.$rows['roomPriceNight'].'"></td>
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceNightM_J['.$i.']" type="text" size="6" id="roomPriceNightM_J" value="'.$rows['roomPriceNightM_J'].'"></td>
<td align="center" bgcolor="#CCCCCC"><input name="roomPriceNightJ_S['.$i.']" type="text" size="6" id="roomPriceNightJ_S" value="'.$rows['roomPriceNightJ_S'].'"></td>

               <td colspan="8" align="center"><input type="submit" name="Submit" value="Submit"></td> 

<?php include("../includes/maintnav.html"); ?>

 <!-- end content -->
    <td><img src="../pictures/spacer.gif" width="190" height="1" /></td>
    <td><img src="../pictures/spacer.gif" width="610" height="1" /></td>


Open in new window

Question by:chipsterva69
    LVL 38

    Assisted Solution

    by:Aaron Tomosky
    It's just counting the number of roomname you have. Counting roomids would probably achieve the same thing as each room has both an I'd and a name right?
    LVL 82

    Accepted Solution

    Pu this at the top of the file and get rid of any '@' in front of PHP functions so they will report errors.

    Open in new window

    LVL 30

    Assisted Solution

    by:Marco Gasi
    If I'm not wrong, the other question you refer to is If so, look at john-formby code: he count the size of $_POST['course']. When he iterates in database to fill form values he wrote

    <td align="center"><input name="course['.$i.']" type="text" id="course" value="'.$rows['course'].'"></td>

    This means that he used one of the values indexed with $i You have to do the same thing so you can use for instance roomPriceSing

    $size = count($_POST['roomPriceSing']);

    So try this, then we'll go to examine how to manage errors.

    LVL 27

    Expert Comment

    by:Lukasz Chmielewski
    What you additionally can do is to put the cho to show your query in the loop between those lines like this:

    $sql1="UPDATE $tbl_name2 SET roomName='$roomName', roomPriceSing='$roomPriceSing', roomPriceDouble='$roomPriceDouble', roomPriceWeek='$roomPriceWeek', roomPriceNight='$roomPriceNight', roomPriceNightM_J='$roomPriceNightM_J', roomPriceNightJ_S='$roomPriceNightJ_S' WHERE roomID='$roomID'"; 
    echo $sql1."<br />";         

    Open in new window

    so you can see if the query looks ok. Maybe you can manually put it in phpmyadmin and see if this works ?
    LVL 15

    Expert Comment


    it should be count($roomid)
    secondly did u tried to fill the same data

    it  might be issue of sql injection
     use addslashes() where ever ur using insert or update
      $roomName = addslashes($_POST['roomName'][$i]);
                    $roomPriceSing = addslashes($_POST['roomPriceSing'][$i]);
                    $roomPriceDouble = addslashes($_POST['roomPriceDouble'][$i]);
                    $roomPriceWeek = addslashes($_POST['roomPriceWeek'][$i]);
                $roomPriceNight = addslashes($_POST['roomPriceNight'][$i]);
                $roomPriceNightM_J = addslashes($_POST['roomPriceNightM_J'][$i]);
                $roomPriceNightJ_S = addslashes($_POST['roomPriceNightJ_S'][$i]);
    LVL 107

    Assisted Solution

    by:Ray Paseur
    Maybe I can help with some parts of this...

    $size = count($_POST['roomName']);

    That says to PHP, "Look at the data that arrived in the $_POST array, which was created by posting an HTML form to this script.  Inside that array look for the field called roomName (case-sensitive) that was created by a form input control with name="roomName[]" in the HTML.  Assume that $_POST["roomName"] is an array.  Get the number of elements in the array and assign that value to a variable named $size."

    Rather than try to cut your teeth in PHP by copying someone else's code, and not understanding how it works, you might want to considered some more structured learning.  If you cannot find a local college that teaches PHP, this book is a good alternative:

    And do not use addslashes().  Instead learn about the recommended method.

    Best regards and good luck with your project, ~Ray

    Author Comment

    All are good points and suggestions - thanks.  I know I need to understand PHP better - but I only have one client using it so it always seems to be on the backburner.

    DaveBaldwin - thanks for the help in putting in the error code - that helped me in determine where the fail point was.

    Turns out the page was not reading the original code that had the room name as a label - the submit statement wasn't seeing it and the table would not take a null value in that field.  Changed that to a text input field and the submit works great now.

    Ray Paseur - agreed with the inherent risks involved with using someone else's code without truly understanding it, but I find it far easier to learn on the fly like this.  Thanks for the recommendation on the book.

    Author Closing Comment

    All comments were on target and very helpful - I had a hard time determining how to split the values up.  Ultimately, the biggest help in solving my actual issue was the bit about showing the errors.  However - understanding the ins and outs of my PHP code is paramount.  Thanks all!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now