Jump to content

Recommended Posts

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??
Link to comment
https://forums.phpfreaks.com/topic/16579-notice-undefined-index/
Share on other sites

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, ..)
Link to comment
https://forums.phpfreaks.com/topic/16579-notice-undefined-index/#findComment-69398
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/16579-notice-undefined-index/#findComment-69510
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/16579-notice-undefined-index/#findComment-70255
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/16579-notice-undefined-index/#findComment-70267
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.