FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank
Google

Reply to topic Page 1 of 1
Need help with simple Form to Database script.
Message  

Reply with quote
Post Need help with simple Form to Database script. 
I am trying to create a simple form that can submit both an e-mail and send it?s results to a database.  The e-mail part was relatively easy, but the database submission is giving me trouble.  The following script is what I have at this point:

<?
//session_start();

             $username = "myform";
             $password = "password";
           $hostname = "localhost";
          $dbh = mysql_connect($hostname, $username, $password)
                       or die("Unable to connect to mysql");
             print "connected to mysql<br>";


     $selected = mysql_select_db("myform",$dbh)
                or die("Could not select myform");



$cansend=$HTTP_POST_VARS['cansend'];
if($cansend==1) {
     $name=$HTTP_POST_VARS['txtName'];
 $cname=$HTTP_POST_VARS['txtCname'];
       $email=$HTTP_POST_VARS['txtEmail'];
       $type=$HTTP_POST_VARS['cboType'];
 $cn = fopen("feedtype.txt","r");
        $x=0;
       while(!feof($cn))
   {

               $str = fgets($cn);
          if(trim($str) != "")
              {
                   $x++;
                       $str1 = trim(substr($str,0,strpos($str,"-")-1));
                  $str2 = trim(substr($str,strpos($str,"-")+1));
                    $arr1[$x] = $str1;
                  $arr2[$x] = $str2;
          }

       }

       settype($type, "integer");
        $type2=$arr1[$type];
        $subject=$HTTP_POST_VARS['txtSubject'];
   $message2=$HTTP_POST_VARS['txtMessage'];
  $mailto=$arr2[$type];
       $mailfrom=$email;
   $headers  = "";

       $headers .= "From: ". $mailfrom."\r\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $mailbody= '<table width="50%" border="0" align="center" cellpadding="5" cellspacing="2">';
       $mailbody.='<tr> <td colspan="2"><strong><font size="+1">Feed Back</font></strong></td></tr>';
        $mailbody.='<tr><td width="23%">Name</td><td width="77%">'.$name.'</td></tr>';
  $mailbody.='<tr><td>Company Name</td><td>'.$cname.'</td></tr>';
     $mailbody.='<tr><td>Email</td><td>'.$email.'</td></tr>';
    $mailbody.='<tr><td>Type</td><td>'.$type2.'</td></tr>';
      $mailbody.='<tr><td>Message</td><td>'.$message2.'</td></tr>';
      if(mail($mailto,$subject,$mailbody,$headers)) $message=" Thank You for your Feedback";
    else $message="There is problem in sending your Feedback this time try again later";


      //Database insertion
        mysql_query("insert into form_responses values('$name','$cname','$email','$type2','$subject','$message2')");
  mysql_close($dbh);

}


?>




Does anyone have an idea what I am doing wrong?

Thanks,

Brook Hurd

View user's profile Send private message

Reply with quote
Post  
You're not telling MySQL what fields in the table to fill with data.  I don't know what the fields are called in your table but try using this:
Code:
mysql_query("insert into `form_responses` (`name`,`cname`,`email`,`type`,`subject`,`message`) values('$name','$cname','$email','$type2','$subject','$message2')");
Of course you'll have to change the field names to whatever you've used.

View user's profile Send private message Visit poster's website

Reply with quote
Post It didn't seem to work. 
The problem is if I use the following code alone it works:

        $username = "myform";
     $password = "password";
   $hostname = "localhost";
  $dbh = mysql_connect($hostname, $username, $password)
               or die("Unable to connect to mysql");
    print "connected to mysql<br>";

  $selected = mysql_select_db("myform",$dbh)
                or die("Could not select myform");

    if (mysql_query("INSERT INTO form_responses VALUES ('','$name','$cname','$email','$type2','$subject','$message2')"))
        {
                             print "successfully inserted record";
                   }
                   else {
                                        print "Failed to insert record";
        }

mysql_close($dbh);


I Tried your alteration on its owh and in the form and it didn't work.  Here is the alteration I used:


      $username = "myform";
     $password = "password";
   $hostname = "localhost";
  $dbh = mysql_connect($hostname, $username, $password)
               or die("Unable to connect to mysql");
    print "connected to mysql<br>";

  $selected = mysql_select_db("myform",$dbh)
                or die("Could not select myform");

    if (mysql_query("INSERT INTO 'form_responses' ('id','name','company','email','type','subject','message') VALUES ('','$name','$cname','$email','$type2','$subject','$message2')"))
   {
                             print "successfully inserted record";
                   }
                   else {
                                        print "Failed to insert record";
        }

mysql_close($dbh);


If I try to add it in to the page it will not run.

Here is the full page code:

<?
session_start();

$cansend=$HTTP_POST_VARS['cansend'];
if($cansend==1) {
    $name=$HTTP_POST_VARS['txtName'];
 $cname=$HTTP_POST_VARS['txtCname'];
       $email=$HTTP_POST_VARS['txtEmail'];
       $type=$HTTP_POST_VARS['cboType'];
 $cn = fopen("feedtype.txt","r");
        $x=0;
       while(!feof($cn))
   {

               $str = fgets($cn);
          if(trim($str) != "")
              {
                   $x++;
                       $str1 = trim(substr($str,0,strpos($str,"-")-1));
                  $str2 = trim(substr($str,strpos($str,"-")+1));
                    $arr1[$x] = $str1;
                  $arr2[$x] = $str2;
          }

       }

       settype($type, "integer");
        $type2=$arr1[$type];
        $subject=$HTTP_POST_VARS['txtSubject'];
   $message2=$HTTP_POST_VARS['txtMessage'];
  $mailto=$arr2[$type];
       $mailfrom=$email;
   $headers  = "";

       $headers .= "From: ". $mailfrom."\r\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $mailbody= '<table width="50%" border="0" align="center" cellpadding="5" cellspacing="2">';
       $mailbody.='<tr> <td colspan="2"><strong><font size="+1">Feed Back</font></strong></td></tr>';
        $mailbody.='<tr><td width="23%">Name</td><td width="77%">'.$name.'</td></tr>';
  $mailbody.='<tr><td>Company Name</td><td>'.$cname.'</td></tr>';
     $mailbody.='<tr><td>Email</td><td>'.$email.'</td></tr>';
    $mailbody.='<tr><td>Type</td><td>'.$type2.'</td></tr>';
      $mailbody.='<tr><td>Message</td><td>'.$message2.'</td></tr>';
      if(mail($mailto,$subject,$mailbody,$headers)) $message=" Thank You for your Feedback";
    else $message="There is problem in sending your Feedback this time try again later";


      //Database insertion

    $username = "myform";
     $password = "password";
   $hostname = "localhost";
  $dbh = mysql_connect($hostname, $username, $password)
               or die("Unable to connect to mysql");
    print "connected to mysql<br>";

  $selected = mysql_select_db("myform",$dbh)
                or die("Could not select myform");

    if (mysql_query("INSERT INTO form_responses VALUES ('','$name','$cname','$email','$type2','$subject','$message2')"))
        {
                             print "successfully inserted record";
                   }
                   else {
                                        print "Failed to insert record";
        }

mysql_close($dbh);

}

?>
<html>
<head>
<title>::: Feedback :::</title>
</head>
<body bgcolor="#f7f7f7" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>

<table width="770" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
          <tr>
          <td valign=top width=80%> <br>
                <form name="form1" method="post" action="feedback.php" onSubmit="return validate()">
              <table width="75%" border="1" bordercolordark="white" align="center" cellpadding="5" cellspacing="2">
                <?php
            if($message) {
              ?>
                <tr bgcolor="#f7f7f7">
                  <td colspan="2"><font size="+1"><B><?php echo $message;?></B></font></td>
                </tr>
                <?php
             }
           else {
             ?>
                <tr>
                  <td colspan="2" bgcolor="#e9e9e9"><strong><font size="+1">Feed
                    Back</font></strong></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td width="23%"><strong>Name</strong></td>
                  <td width="77%"><input name="txtName" type="text" size="25" maxlength="30"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Company Name</strong></td>
                  <td><input name="txtCname" type="text" size="50" maxlength="100" style="width:250"></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td><strong>Email id</strong></td>
                  <td><input name="txtEmail" type="text" size="30" maxlength="40"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Type</strong></td>
                  <td><select name="cboType" id="cboType">
                      <option value="">Select</option>
<!--
                      <option value="service">Service</option>
                      <option value="support">Support</option>
                      <option value="sales">Sales</option>
                    </select>
-->
             <?
               $cn = fopen("feedtype.txt","r");
        $x=0;
               while(!feof($cn))
           {

                       $str = fgets($cn);
                  if(trim($str) != "")
                      {
                           $x++;

                           $str1 = trim(substr($str,0,strpos($str,"-")-1));
                          $str2 = trim(substr($str,strpos($str,"-")+1));
                            $arr1[$x] = $str1;
                          $arr2[$x] = $str2;
                          echo "<option value=\"$x\">$arr1[$x]</option>";

                       }

               }
           ?>
</td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td><strong>Subject</strong></td>
                  <td><input name="txtSubject" type="text" maxlength="100"  style="width:250"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Message</strong></td>
                  <td><textarea name="txtMessage" cols="30" rows="5"></textarea></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td colspan="2" align="center"><input type="hidden" name="cansend" value="0">
                    <input type="submit" name="btnSend" value="Send" class="redbutton">
                    <input type="reset" name="btnCancel" value="Reset" class="redbutton"></td>
                </tr>
                <?php
                      }
                   ?>
              </table>
           </form> <br><b><center>
            </center>
</center>
</body>
</html>
<script language="JavaScript">
function validate()
{
     if(trim(document.form1.txtName.value) == "")
      {
           alert("Please Enter Your Name");
          document.form1.txtName.focus();
             return false;
       }
   if(trim(document.form1.txtCname.value) == "")
     {
           alert("Please Enter Your Company Name");
          document.form1.txtCname.focus();
            return false;
       }

       if(trim(document.form1.txtEmail.value) == "")
     {
           alert("Please Enter Your Email Id");
              document.form1.txtEmail.focus();
            return false;
       }
   if( !isEmail(document.form1.txtEmail.value) )
       {
           alert("Please Enter the Valid Email Id");
         document.form1.txtEmail.focus();
            document.form1.txtEmail.select();
           return false;
       }
   if(document.form1.cboType.value == "")
    {
           alert("Please Select ypur Feedback Type");
                document.form1.cboType.focus();
             return false;
       }
   if(trim(document.form1.txtSubject.value)=="") {
           alert("Please Enter a Subject");
          document.form1.txtSubject.focus();
          return false;
       }
   if(trim(document.form1.txtMessage.value)=="") {
           alert("Please Enter a Message");
          document.form1.txtMessage.focus();
          return false;
       }

       document.form1.cansend.value=1;
     return true;
}
function isEmail(emailstr)
{
        dotchar = emailstr.indexOf(".");
  atchar = emailstr.indexOf("@");
   dotlast = emailstr.lastIndexOf(".");
      spacechar = emailstr.indexOf(" ");
        len = emailstr.length;
      if( (dotchar == -1) || (atchar == -1) || (spacechar != -1) || (dotlast < atchar) || (dotlast == len - 1) )
       {
           return false;
       }
   else
        {
           return true;
        }
}
function trim(str)
{
ch = '';
for(i=0;i<str.length;i++)
{
        cha = str.charAt(i);
        if(cha != ' ')
    {
           ch = ch + cha;
      }
}
return ch;
}

</script>

View user's profile Send private message

Reply with quote
Post What I am trying to do 
Basically I want the user to fill out a form, then after the submit button is selected the data is both e-mailed and sent to the Database.

View user's profile Send private message

Reply with quote
Post  
I think the easiest thing to do when you have a script that size is first to see what you're trying to put into the database.  Sometimes in the past I've found the problem lies with my variables containing something they shouldn't.

Add this immediately before the INSERT statement.
Code:
echo "name=$name, cname=$cname, email=$email, type2=$type2, subject=$subject, message2=$message2<br>";
Then run it again and see if they contain what they should contain.  If they don't, trace back to each assignment for each variable and use echo to show the variable's contents.  I use this method a lot to track down bugs.  This way you can slowly track the variable back to where the contents change.

View user's profile Send private message Visit poster's website

Reply with quote
Post I get nothing 
It seemes that all print and echo commands are ignored.

View user's profile Send private message

Reply with quote
Post  
Can you post a link to the code with the echo statements so I can check it out?

View user's profile Send private message Visit poster's website

Reply with quote
Post Test Page location 
http://www.perlmandesigngroup....backdb.php

View user's profile Send private message

Reply with quote
Post  
Now check if the data I entered is in the database.

View user's profile Send private message Visit poster's website

Reply with quote
Post  
Where are you checking to see if btnSend was clicked?

I can see when its clicked and the form is validated feedback.php is called.  Is code specified above feedback.php?

View user's profile Send private message Visit poster's website

Reply with quote
Post I think I missed that one. 
I don't have access to my database to see if it worked, but this might do it.

Here is my update:

<?
session_start();

$cansend=$HTTP_POST_VARS['cansend'];
if($cansend==1) {
 $name=$HTTP_POST_VARS['txtName'];
 $cname=$HTTP_POST_VARS['txtCname'];
       $email=$HTTP_POST_VARS['txtEmail'];
       $type=$HTTP_POST_VARS['cboType'];
 $sendform=$HTTP_POST_VARS['btnSend'];
     $cn = fopen("feedtype.txt","r");
        $x=0;
       while(!feof($cn))
   {

               $str = fgets($cn);
          if(trim($str) != "")
              {
                   $x++;
                       $str1 = trim(substr($str,0,strpos($str,"-")-1));
                  $str2 = trim(substr($str,strpos($str,"-")+1));
                    $arr1[$x] = $str1;
                  $arr2[$x] = $str2;
          }

       }

       settype($type, "integer");
        $type2=$arr1[$type];
        $subject=$HTTP_POST_VARS['txtSubject'];
   $message2=$HTTP_POST_VARS['txtMessage'];
  $mailto=$arr2[$type];
       $mailfrom=$email;
   $headers  = "";

       $headers .= "From: ". $mailfrom."\r\n";
       $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $mailbody= '<table width="50%" border="0" align="center" cellpadding="5" cellspacing="2">';
       $mailbody.='<tr> <td colspan="2"><strong><font size="+1">Feed Back</font></strong></td></tr>';
        $mailbody.='<tr><td width="23%">Name</td><td width="77%">'.$name.'</td></tr>';
  $mailbody.='<tr><td>Company Name</td><td>'.$cname.'</td></tr>';
     $mailbody.='<tr><td>Email</td><td>'.$email.'</td></tr>';
    $mailbody.='<tr><td>Type</td><td>'.$type2.'</td></tr>';
      $mailbody.='<tr><td>Message</td><td>'.$message2.'</td></tr>';
      if(mail($mailto,$subject,$mailbody,$headers)) {$message=" Thank You for your Feedback";




           //Database insertion

            if($sendform="Send") {

                $username = "myform";
             $password = "wbxnv275";
           $hostname = "localhost";
          $dbh = mysql_connect($hostname, $username, $password)
                       or die("Unable to connect to mysql");
         print "connected to mysql<br>";

             $selected = mysql_select_db("myform",$dbh)
                        or die("Could not select myform");
                if (mysql_query("INSERT INTO form_responses VALUES ('','$name','$cname','$email','$type2','$subject','$message2')"))
                {
                                     print "successfully inserted record";
                           }
                           else {
                                                print "Failed to insert record";
                }

               mysql_close($dbh);
          }

       //End Database Insertion





        }
   else {$message="There is problem in sending your Feedback this time try again later";}




}

?>
<html>
<head>
<title>::: Feedback :::</title>
</head>
<body bgcolor="#f7f7f7" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>

<table width="770" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
          <tr>
          <td valign=top width=80%> <br>
               <form name="form1" method="post" action="feedback.php" onSubmit="return validate()">
              <table width="75%" border="1" bordercolordark="white" align="center" cellpadding="5" cellspacing="2">
                <?php
            if($message) {

          ?>
                <tr bgcolor="#f7f7f7">
                  <td colspan="2"><font size="+1"><B><?php echo $message;?></B></font></td>
                </tr>
                <?php
             }
           else {
             ?>
                <tr>
                  <td colspan="2" bgcolor="#e9e9e9"><strong><font size="+1">Feed
                    Back</font></strong></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td width="23%"><strong>Name</strong></td>
                  <td width="77%"><input name="txtName" type="text" size="25" maxlength="30"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Company Name</strong></td>
                  <td><input name="txtCname" type="text" size="50" maxlength="100" style="width:250"></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td><strong>Email id</strong></td>
                  <td><input name="txtEmail" type="text" size="30" maxlength="40"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Type</strong></td>
                  <td><select name="cboType" id="cboType">
                      <option value="">Select</option>
<!--
                      <option value="service">Service</option>
                      <option value="support">Support</option>
                      <option value="sales">Sales</option>
                    </select>
-->
             <?
               $cn = fopen("feedtype.txt","r");
        $x=0;
               while(!feof($cn))
           {

                       $str = fgets($cn);
                  if(trim($str) != "")
                      {
                           $x++;

                           $str1 = trim(substr($str,0,strpos($str,"-")-1));
                          $str2 = trim(substr($str,strpos($str,"-")+1));
                            $arr1[$x] = $str1;
                          $arr2[$x] = $str2;
                          echo "<option value=\"$x\">$arr1[$x]</option>";

                       }

               }
           ?>
</td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td><strong>Subject</strong></td>
                  <td><input name="txtSubject" type="text" maxlength="100"  style="width:250"></td>
                </tr>
                <tr bgcolor="#e9e9e9">
                  <td><strong>Message</strong></td>
                  <td><textarea name="txtMessage" cols="30" rows="5"></textarea></td>
                </tr>
                <tr bgcolor="#f7f7f7">
                  <td colspan="2" align="center"><input type="hidden" name="cansend" value="0">
                    <input type="submit" name="btnSend" value="Send" class="redbutton">
                    <input type="reset" name="btnCancel" value="Reset" class="redbutton"></td>
                </tr>
                <?php
                      }
                   ?>
              </table>
           </form> <br><b><center>
            </center>
</center>
</body>
</html>
<script language="JavaScript">
function validate()
{
     if(trim(document.form1.txtName.value) == "")
      {
           alert("Please Enter Your Name");
          document.form1.txtName.focus();
             return false;
       }
   if(trim(document.form1.txtCname.value) == "")
     {
           alert("Please Enter Your Company Name");
          document.form1.txtCname.focus();
            return false;
       }

       if(trim(document.form1.txtEmail.value) == "")
     {
           alert("Please Enter Your Email Id");
              document.form1.txtEmail.focus();
            return false;
       }
   if( !isEmail(document.form1.txtEmail.value) )
       {
           alert("Please Enter the Valid Email Id");
         document.form1.txtEmail.focus();
            document.form1.txtEmail.select();
           return false;
       }
   if(document.form1.cboType.value == "")
    {
           alert("Please Select ypur Feedback Type");
                document.form1.cboType.focus();
             return false;
       }
   if(trim(document.form1.txtSubject.value)=="") {
           alert("Please Enter a Subject");
          document.form1.txtSubject.focus();
          return false;
       }
   if(trim(document.form1.txtMessage.value)=="") {
           alert("Please Enter a Message");
          document.form1.txtMessage.focus();
          return false;
       }

       document.form1.cansend.value=1;
     return true;
}
function isEmail(emailstr)
{
        dotchar = emailstr.indexOf(".");
  atchar = emailstr.indexOf("@");
   dotlast = emailstr.lastIndexOf(".");
      spacechar = emailstr.indexOf(" ");
        len = emailstr.length;
      if( (dotchar == -1) || (atchar == -1) || (spacechar != -1) || (dotlast < atchar) || (dotlast == len - 1) )
       {
           return false;
       }
   else
        {
           return true;
        }
}
function trim(str)
{
ch = '';
for(i=0;i<str.length;i++)
{
        cha = str.charAt(i);
        if(cha != ' ')
    {
           ch = ch + cha;
      }
}
return ch;
}

</script>

View user's profile Send private message

Reply with quote
Post  
OK its late here (gone 1am) and I'm real tired.  Rather than give you wrong information here's something to have a look at - a little structure:

Code:
<?php
  connect to database;
  if ($_POST['btnSend']) {
    get form contents like: $var=$_POST['formname']);
  }
?>
<html>
  here's your HTML and form stuff
</html>
Have a go with that as I can't see where you're checking to see if the SEND button was clicked then reading the form data if it was and then act on it.  From taking a proper look at your code it seems you're trying to pull data from the form every time the script is called.



Last edited by Zeb on Fri Oct 07, 2005 6:16 pm; edited 1 time in total
View user's profile Send private message Visit poster's website

Reply with quote
Post Thanks 
I will try this out monday and tell you what happens.

Thanks again,

Brook

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com