[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

PHP submit not working

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Menu Maintenance :: Edit Rooms &amp; Cottages</title>
<link href="http://www.whalecovecottages.ca/css/whalecove.css" 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 SmartWebby.com (http://www.smartwebby.com/dhtml/)

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="http://whalecovecottages.ca/"><img src="../pictures/logo.png" width="190" height="224" border="0" /></a></p>
      <?php include("../includes/leftmenu.html"); ?>
      <p><br />
        <br />
        <!--- accuweather.com weather addin --->
      <div style='width: 120px; height: 60px; background-image: url( http://vortex.accuweather.com/adcbin/netweather_v2/backgrounds/blue_120x60_bg.jpg ); background-repeat: no-repeat; background-color: #346797;' >
        <div id='NetweatherContainer' style='height: 48px;' >
          <script src='http://netweather.accuweather.com/adcbin/netweather_v2/netweatherV2ex.asp?partner=netweather&amp;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='http://www.accuweather.com/world-index-forecast.asp?partner=netweather&amp;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

4 Solutions
Aaron TomoskyTechnology ConsultantCommented:
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?
Dave BaldwinFixer of ProblemsCommented:
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

Marco GasiFreelancerCommented:
If I'm not wrong, the other question you refer to is http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_25664243.html#a29479534. 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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Lukasz ChmielewskiCommented:
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 ?

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]);
Ray PaseurCommented:
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
chipsterva69Author Commented:
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.
chipsterva69Author Commented:
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!

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now