Jump to content

Error Inserting Data into database from web form


benjamin_boothe

Recommended Posts

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

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

That is the If statment I have used to check for successful entry.  Right at the top of my form it has the following statement: Error inserting data! which is written in my If statement.  I have not input anything into the form and it displays this message.

I am thinking it has something to do with my db connection.

db connection - include 'includes/db_conn.txt';

In the .txt I have written this:
<?
$link =
        mysqli_connect('host','username','password','db_name');

In that format!
?>

But no joy!! it still wont insert into my db.

Help!!
Link to comment
Share on other sites

Well i dont know a lot about mysqli, but:

You are going to need to change your included database connection file to a php file otherwise it isn't going to pass the information.

Also, if you think you are having troubles with your database connection, try the examples on the php website:
http://uk.php.net/mysqli_connect
Link to comment
Share on other sites

My db connetion is fine but I am still unable to insert data into my db.

Here is the code, Can you spot any discrepencies?

<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>

    <?

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

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
        (policy_holder_code, 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 ( '$phc', '$cd1', '$cf1', '$ct1', '$crc1','$ncb1', '$cd2', '$cf2', '$ct2',
        '$crc2', '$ncb2')";

        include 'includes/db_conn.php';



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

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

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

} else {
      $phc = $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>Policy Holder Code</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="phc" VALUE="<? echo $phc ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Date of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="cd1" VALUE="<? echo $cd1 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Type of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="ct1" VALUE="<? echo $ct1 ?>"/> <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Fault Claim</TD>
    <TD vAlign=top align=left><SELECT size=1 name="cf1" VALUE="<? echo $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="crc1" VALUE="<? echo $crc1 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Bonus Affected</TD>
    <TD vAlign=top align=left><SELECT size=1 name="ncb1" VALUE="<? echo $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>Date of Claim</TD>
    <TD vAlign=top align=left><INPUT size=45 name="cd2" VALUE="<? echo $cd2 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Type of Claim</TD>
    <TD vAlign=top align=left><INPUT TYPE="text" name="ct2" VALUE="<? echo $ct2 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Fault Claim</TD>
    <TD vAlign=top align=left><SELECT size=1 name="cf2" VALUE="<? echo $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="crc2" VALUE="<? echo $crc2 ?>"/>
    <BR></TD></TR>
  <TR>
    <TD vAlign=top align=left>Bonus Affected</TD>
    <TD vAlign=top align=left><SELECT size=1 name="ncb2" VALUE="<? echo $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>
   
Link to comment
Share on other sites

if you do this
[code]
if ($_POST) {

  $msg = "";

  foreach($_POST as $k => $v) {
        $v = trim($v);
        ${$k} = $v;// to create a variable from a variable you'll need the { and } around it, as of php5 i guess, it worked when i still got php4

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

then you don't have to do this:

[code]
$phc = isSet($_POST['phc']) ? $_POST['phc'] : '';
$cd1 = isSet($_POST['cd1']) ? $_POST['cd1'] : '';
$cf1 = isSet($_POST['cf1']) ? $_POST['cf1'] : '';
$ct1 = isSet($_POST['ct1']) ? $_POST['ct1'] : '';
$crc1 = isSet($_POST['crc1']) ? $_POST['crc1'] : '';
$ncb1 = isSet($_POST['ncb1']) ? $_POST['ncb1'] : '';
$cd2 = isSet($_POST['cd2']) ? $_POST['cd2'] : '';
$cf2 = isSet($_POST['cf2']) ? $_POST['cf2'] : '';
$ct2 = isSet($_POST['ct2']) ? $_POST['ct2'] : '';
$crc2 = isSet($_POST['crc2']) ? $_POST['crc2'] : '';
$ncb2 = isSet($_POST['ncb2']) ? $_POST['ncb2'] : '';
[/code]
Link to comment
Share on other sites

I'm no pro :) but the db connection code seems to be off. Here's yours:

[code]I am thinking it has something to do with my db connection.

db connection - include 'includes/db_conn.txt';

In the .txt I have written this:
<?
$link =
        mysqli_connect('host','username','password','db_name');

In that format!
?>[/code]

First, there's a misspell at 'mysqli' which should be 'mysql' without the 'i' in there.

Second, typically it's supposed to be this way:

[code]mysql_connect('host', 'username', 'password') or die("Where is that dang database");
mysql_select_db('db_name') or die("Where is that dang database!");
[/code]

I'm not 100% sure on this also but the included file should probably be a .php extension instead of the .txt.

UPDATE: I just noticed another thread pertaining to the 'mysqli' issue. Apparently that's a hybrid style of MySQL with 'Improved' tacked on. My apologies for mentioning the misspell. :)
Link to comment
Share on other sites

The problem and reason you're having to use the isset function is due to the form stating 'values' for the various fields that are dependent upon the form being parsed first. In other words, the form ACTION is pointed to PHP_SELF which would require the submit button hit before the actual values you have indicated can be displayed in the form fields. Not sure why you want to do that but hey.... :)

[code]VALUE="<? echo $phc ?>"[/code]

You're asking the form to display something that hasn't been invented yet until they fill out the form and hit submit. Then it will parse these via the $_POST command and display them. But, you're asking them to be displayed right back in the form fields. That's cool if you want to show them the form again with their entries placed in the respective fields.

For the data insert error, this is a wild guess, but try moving your include/db_conn.db line [i]ahead[/i] of your query statement. It needs that information present before executing your query command.
Link to comment
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.