Jump to content


Photo

Notice: Undefined Index......


  • Please log in to reply
6 replies to this topic

#1 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 04 August 2006 - 06:57 PM

I keep getting this message in all of my fields that I have used the $_REQUEST['name'] call function in.  I have this piece of code, but I don't know where I need to put this in to make it work:

$cd1 = isSet($_REQUEST['cd1']) ? $_REQUEST['cd1'] : '';
$cf1 = isSet($_REQUEST['cf1']) ? $_REQUEST['cf1'] : '';
$cd1 = isSet($_REQUEST['ct1']) ? $_REQUEST['ct1'] : '';
$crc1 = isSet($_REQUEST['crc1']) ? $_REQUEST['crc1'] : '';
$ncb1 = isSet($_REQUEST['ncb1']) ? $_REQUEST['ncb1'] : '';
$cd2 = isSet($_REQUEST['cd2']) ? $_REQUEST['cd2'] : '';
$cf2 = isSet($_REQUEST['cf2']) ? $_REQUEST['cf2'] : '';
$ct2 = isSet($_REQUEST['ct2']) ? $_REQUEST['ct2'] : '';
$crc2 = isSet($_REQUEST['crc2']) ? $_REQUEST['crc2'] : '';
$ncb2 = isSet($_REQUEST['ncb2']) ? $_REQUEST['ncb2'] : '';

This is for all my form inputs that are supposed to print into the fields in a table in my SQL database.

Could anyone help please??

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 04 August 2006 - 07:33 PM

From your question it is not clear how you passed those variables to the $_REQUEST array. Did you use the $_GET or the $_POST methods?

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 04 August 2006 - 07:41 PM

Well it is a notice...

Not that this is the best way to resolve this problem but it works.. :D

$cd1 = isset(@$_REQUEST['cd1']) ? $_REQUEST['cd1'] : '';
...

however i always just use:
$cd1 = @$_REQUEST['cd1'] ? $_REQUEST['cd1'] : '';

but we are using a dynamic language, and $cd1, $cd2, .. is a real waste of time so I would suggest you use:
if (@$_REQUEST) {
   while (false !== (list($Key, $Value) = each($_REQUEST))) {
      ${$Key} = $Value;
   }
}

and you would still have all your desired variables ($cd1, $cd2, ..)

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 04 August 2006 - 10:55 PM

Ignace's way is a lot easier,BUT you'd better validate the stuff before using it! Especially since you draw them from the $_REQUEST array!

RTFM is an almost extinct art form, it should be subsidized.

#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 04 August 2006 - 10:57 PM

alternatively, you can just shut notices off.  search the PHP manual for error reporting, there's a world of info on it.  while it isn't the best solution (as notices point out sloppy programming), if you just don't care whether your programming is sloppy this alleviates the notice conscience.

#6 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 06 August 2006 - 03:50 PM

This is the code I am using to get my page to work, but I am still getting these notices:

<META content="MSHTML 6.00.2800.1458" name=GENERATOR></HEAD>
<BODY bgColor=#99ccff>
<DIV><B>GSI Insurance LTD</B> (Claim History Page)</DIV>
<HR>
<STYLE>.req {
COLOR: #ff0033
}
</STYLE>

<?

if ($_POST) {

  $msg = "";

  foreach($_POST as $k => $v) {
        $v = trim($v);
        $$k = $v;

        if ($v=="") {
          $msg = "Please fill in all fields";
        }
  }

  if ($msg=="") {
      $insert = "INSERT INTO claim_history
        (claim_1_date, claim_1_fault, claim_1_type,
        claim_1_repair_cost, claim_1_effect_on_NCB, claim_2_date, claim_2_fault,
        claim_2_type, claim_2_repair_cost, claim_2_effect_on_NCB)
        VALUES ( '$cd1', '$cf1', '$ct1', '$crc1','$ncb1', '$cd2', '$cf2', '$ct2',
        '$crc2', '$ncb2')";
       
        include 'includes/db_conn.txt';
       
        $cd1 = isSet($_REQUEST['cd1']) ? $_REQUEST['cd1'] : '';
        $cf1 = isSet($_REQUEST['cf1']) ? $_REQUEST['cf1'] : '';
        $cd1 = isSet($_REQUEST['ct1']) ? $_REQUEST['ct1'] : '';
        $crc1 = isSet($_REQUEST['crc1']) ? $_REQUEST['crc1'] : '';
        $ncb1 = isSet($_REQUEST['ncb1']) ? $_REQUEST['ncb1'] : '';
        $cd2 = isSet($_REQUEST['cd2']) ? $_REQUEST['cd2'] : '';
        $cf2 = isSet($_REQUEST['cf2']) ? $_REQUEST['cf2'] : '';
        $ct2 = isSet($_REQUEST['ct2']) ? $_REQUEST['ct2'] : '';
        $crc2 = isSet($_REQUEST['crc2']) ? $_REQUEST['crc2'] : '';
        $ncb2 = isSet($_REQUEST['ncb2']) ? $_REQUEST['ncb2'] : '';



        if (!mysqli_query($link, $insert)) {
          $msg = "Error inserting data";
        } else {
          $msg = "Record succesfully added";

        $cd1= $cf1 = $ct1 = $crc1 = $ncb1 = $cd2 = $cf2 = $ct2 = $crc2 = $ncb2 = "";
        }
        mysqli_close($link);
  }     

echo "<div class=\"error\">$msg</div>";

} else {
      $cd1= $cf1 = $ct1 = $crc1 = $ncb1 = $cd2 = $cf2 = $ct2 = $crc2 = $ncb2 = "";
}
?>

<FORM METHOD="post" ACTION="<? echo $_SERVER['PHP_SELF'] ?>">
<TABLE BORDER="0" CELLPADDING="5">
  <TBODY>
  <TR>
    <TD colSpan=2><B>Personal Details Items in <SPAN class=req>red</SPAN> or
      marked with <SPAN class=req>*</SPAN> are required.</B></TD></TR>
  <TR>
    <TD colSpan=2>&nbsp;</TD></TR>
  <TR>

  <TD colSpan=2><B>Previous Claims(first) - Items in <SPAN
      class=req>red</SPAN> or marked with <SPAN class=req>*</SPAN> are
      required.</B></TD></TR>
  <TR>
    <TD colSpan=2>&nbsp;</TD></TR>
  <TR>
    <TD vAlign=top align=left>Claim 1 Details</TD>
    <TD vAlign=top align=left><INPUT size=45 name="">
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Date of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="claim_1_date" VALUE="<? echo $_REQUEST['cd1'] ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Type of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="claim_1_type" VALUE="<? echo $_REQUEST['ct1'] ?>"/> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Fault Claim</TD>
    <TD vAlign=top align=left><SELECT size=1 name="claim_1_fault" VALUE="<? echo $_REQUEST['cf1'] ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=No>No</OPTION> <OPTION value=Yes>Yes</OPTION>
    </SELECT> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Repair Cost</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="claim_1_repair_cost" VALUE="<? echo $_REQUEST['crc1'] ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Bonus Affected</TD>
    <TD vAlign=top align=left><SELECT size=1 name="claim_1_effect_on_NCB" VALUE="<? echo $_REQUEST['ncb1'] ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=No>No</OPTION> <OPTION value=Yes>Yes</OPTION>
    </SELECT> <BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=5 border=0>
  <TBODY>
  <TR>
    <TD colSpan=2><B>Previous Claims(second) - Items in <SPAN
      class=req>red</SPAN> or marked with <SPAN class=req>*</SPAN> are
      required.</B></TD></TR>
  <TR>
    <TD colSpan=2>&nbsp;</TD></TR>
  <TR>
    <TD vAlign=top align=left>Claim 2 Details</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name=""> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Date of Claim</TD>
    <TD vAlign=top align=left><INPUT size=45 name="claim_2_date" VALUE="<? echo $_REQUEST['cd2'] ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Type of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="claim_2_type" VALUE="<? echo $_REQUEST['ct2'] ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Fault Claim</TD>
    <TD vAlign=top align=left><SELECT size=1 name="claim_2_fault" VALUE="<? echo $_REQUEST['cf2'] ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=No>No</OPTION> <OPTION value=Yes>Yes</OPTION>
    </SELECT> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Repair Cost</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="claim_2_repair_cost" VALUE="<? echo $_REQUEST['crc2'] ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Bonus Affected</TD>
    <TD vAlign=top align=left><SELECT size=1 name="claim_2_effect_on_NCB" VALUE="<? echo $_REQUEST['ncb2'] ?>"/>
    <OPTION value="" selected>-- Please select --</OPTION>
    <OPTION value=No>No</OPTION> <OPTION value=Yes>Yes</OPTION></SELECT>
    <BR></TD></TR></TBODY></TABLE>

    <P><INPUT type="submit" value="Add to database"> <INPUT type=reset value="Cancel"> </P></FORM>

Apparently, I have to set the variables so that they can be identified when using $_REQUEST.

This is the code I have used to do so,
$cd1 = isSet($_REQUEST['cd1']) ? $_REQUEST['cd1'] : '';
        $cf1 = isSet($_REQUEST['cf1']) ? $_REQUEST['cf1'] : '';
        $cd1 = isSet($_REQUEST['ct1']) ? $_REQUEST['ct1'] : '';
        $crc1 = isSet($_REQUEST['crc1']) ? $_REQUEST['crc1'] : '';
        $ncb1 = isSet($_REQUEST['ncb1']) ? $_REQUEST['ncb1'] : '';
        $cd2 = isSet($_REQUEST['cd2']) ? $_REQUEST['cd2'] : '';
        $cf2 = isSet($_REQUEST['cf2']) ? $_REQUEST['cf2'] : '';
        $ct2 = isSet($_REQUEST['ct2']) ? $_REQUEST['ct2'] : '';
        $crc2 = isSet($_REQUEST['crc2']) ? $_REQUEST['crc2'] : '';
        $ncb2 = isSet($_REQUEST['ncb2']) ? $_REQUEST['ncb2'] : '';

but where do I put this code to remove those notices?

Please help.


#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 August 2006 - 04:21 PM

That chunk of code should go at the start of your script. Also use $_POST instead of $_REQUEST, unless your expecting these veriable to be sent via the URL or Posted vial a form. Since you've already set the local variables, you should change the 'value="' attribute in your form to reference them instead of $_REQUEST indices. (This is probably where the warnings are coming form)

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users