...

View Full Version : Problem with New Records from an Online Form



PHPaul
11-24-2006, 04:42 AM
I am new to PHP and MYSQL and coding in general. Infortunatley i am administering a bugy system that I inherited without documentation, but enough sob story. Here goes . .

The system is a hotel database, MYSQL, PHP, HTML, JAVASCRIPT.

There is a form to insert or update hotel information. Here is the code for that:


<td colspan="2" class="text">

<table align="left" cellpadding="0" cellspacing="0" border="0">
<tr><td width="30"><input type="checkbox" name="checkbox" id="need" <? if($_SESSION['need']=="need"){ ?> checked <? } ?> onClick="clicked()"></td><td class="text" width="160">Hotel Deposit Required</td>
<td width="120">
<? if($_SESSION['need']=="need") {?>
<DIV ID="MMDiv" style="visibility:visible">
<? }else {?>
<DIV ID="MMDiv" style="visibility:hidden">
<? } ?>


<LABEL FOR="checkin">
<table><tr><td>
<select name="select">
<? if ($option=="edithotel")
{
$arr=array("First Night","Entire Stay");
$obj=new general();
print $obj->createselect($arr,$depositreq);
}
else {?>
<option value=1>First Night</option>
<option value=2>Entire Stay</option>
<? } ?>
</select>
</td></tr></table>
</label></div>
</td>
<input type="hidden" name="need" id="sessionval" value="<? echo $_SESSION['need']?>">
</tr></table></td>

Depending wether you are adding a new hotel to the system or updating a pre-existing hotel, the submition acts differently. Here is the code if you are editing a a pre-existing hotel. This code works fine, by the way:



$deposit_type=$_POST[deposittype];
$deposit_value=$_POST[depositvalue];
if(empty($deposit_value))
$deposit_value=0;

$service_charge=$_POST[servicecharge];
$service_charge_value=$_POST[servicechargevalue];
$tax=$_POST[tax];
$tax_value=$_POST[taxvalue];
$currency=$_POST[currency];
$hotelrateid=$_POST[hotelrateid];
$_SESSION['need']=$_POST['need'];

if($_POST['checkbox']=="on")
{
$selecteddeposit=$_POST['select'];
if($selecteddeposit=='First Night'){
$deposit_req=1;
}else if($selecteddeposit=='Entire Stay'){
$deposit_req=2;
}
}else{
$deposit_req=0;
}
$insertq="update hotel_rate set deposit_type='$deposit_type',deposit_value=$deposit_value,service_charge=$service_charge,service_cha rge_value=$service_charge_value,tax=$tax,tax_value=$tax_value,currency='$currency',deposit_required= $deposit_req where id=$hotelrateid and hotel_id=$hotelid";
$obj->query($insertq);

Now here is the problem. The code that is used when adding a new hotel does not work fine. For some reason in the resulting MYSQL INSERT, the value for $deposit_req does not show up. Its preceeding comma does, so the ending of the SQL statement looks like this ,) and so fails. Here is that code, which I can't figure out what is wrong wiht:


$maxhotelrateid=$obj->query_singlevalue("select max(id) from hotel_rate");
if(empty($maxhotelrateid))
$maxhotelrateid=0;
$maxhotelrateid++;

$deposit_type=$_POST[deposittype];
$deposit_value=$_POST[depositvalue];
if(empty($deposit_value))
$deposit_value=0;
$service_charge=$_POST[servicecharge];
$service_charge_value=$_POST[servicechargevalue];
$tax=$_POST[tax];
$tax_value=$_POST[taxvalue];
$currency=$_POST[currency];
if($_POST['checkbox']=="on")
{
$selecteddeposit=$_POST['select'];
if($selecteddeposit=='First Night'){
$deposit_req=1;
}else if($selecteddeposit=='Entire Stay'){
$deposit_req=2;
}
}else{
$deposit_req=0;
}
$insertq="insert into hotel_rate values($maxhotelrateid,$hotelid,'$deposit_type',$deposit_value,$service_charge,$service_charge_value ,$tax,$tax_value,'$currency',$deposit_req)";
$obj->query($insertq);

Any help with this would be super duper appreciated big time! Thanks in advance for any advice!

YT
PHPaul

CFMaBiSmAd
11-24-2006, 05:06 AM
From the logic -

If $_POST['checkbox'] is not "on", then $deposit_req is set to 0.

If $_POST['checkbox'] is "on", $deposit_req is only set to 1 if $selecteddeposit is set to 'First Night'.

If $_POST['checkbox'] is "on", $deposit_req is only set to 2 if $selecteddeposit is set to 'Entire Stay'.

If $_POST['checkbox'] is "on", $deposit_req is not set to anything if $selecteddeposit is neither 'First Night' or 'Entire Stay'.

Edit: Upon further review, the form code is setting the option values to 1 and 2, not 'First Night' and 'Entire Stay'.

<option value=1>First Night</option>
<option value=2>Entire Stay</option>Either the form code needs to be changed to match the logic or visa versa.

PHPaul
11-24-2006, 07:00 AM
Yes, that is correct. The problem is just with the last section of code that I posted.

As you probably saw, the last line produces an insert statement:


insert into hotel_rate values($maxhotelrateid,$hotelid,'$deposit_type',$deposit_value,$service_charge,$service_charge_value ,$tax,$tax_value,'$currency',$deposit_req)

When that runs it returns a mysql error and according to the errorthe following is what is actaully being sent to the database:


insert into hotel_rates values(88,88,'Percentage',20,1,10,1,7,'baht',);

It complains of the problem being ",)" . There are ten columns being written to the database. From the error I can see that the last one $deposit_req is not making it into the statement (it should be 0,1 or 2 and it should be after 'baht', . it is missing) and that mysql is getting hung up on the resulting comma preceeding the close parens.

I can't figure out why the value of $deposit_req is not getting sent to the db and what is wrong with the preceeding code. I can only imaging it that $deposit_req is somehow not getting set properly before the insert runs cause the insert looks good to me.

If you compare the working code from the snippet that shows the udpate mysql statement for a preexisting hotel, the only difference my novice eyes can see is the following:


$_SESSION['need']=$_POST['need'];


I am not sure if that is related as I don't understand what that means. It is referenced in the form (snippet 1) but I don't understand it there either.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum