Jump to content


Photo

Error Inserting Data into database from web form


  • Please log in to reply
6 replies to this topic

#1 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 06 August 2006 - 06:17 PM

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

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 06 August 2006 - 06:46 PM

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

#3 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 06 August 2006 - 07:36 PM

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>
   


#4 ignace

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

Posted 06 August 2006 - 08:21 PM

if you do this
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";
        }
   }

then you don't have to do this:

$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'] : '';


#5 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 07 August 2006 - 04:26 AM

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

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

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:

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

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. :)

#6 benjamin_boothe

benjamin_boothe
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 07 August 2006 - 10:05 AM

I have changed the extension on the included file to .php, but It still keeps coming up with the same message: 'Error inserting data'. I have to use the isSet function as it prints notices in my form inputs if I dont.

Any other suggestions??

#7 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 07 August 2006 - 04:35 PM

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.... :)

VALUE="<? echo $phc ?>"

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 ahead of your query statement. It needs that information present before executing your query command.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users