2

Alright so I have a form and i'm trying to get it to submit data into my database but it's not working... i'm not sure if it's my form that is causing the issue or if it's a problem with connecting to the database?

My table names are correct, value fields, log-in info... but whenever I hit submit it is not inserting any data.

Could it be my form that is having the issue?? Would you guys mind taking a look?

<form action="form.php" method="POST"> 

<div class="row"> 
<div class="large-4 columns"> 


<span id="spryfirstname">
<input name="firstname" type="text" class="text" placeholder="First Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div> 

<div class="large-4 columns"> 

<span id="sprylastname">
<input name="lastname" type="text" class="text" placeholder="Last Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>

 <div class="large-4 columns">
  <div class="row collapse"> 

   <div class="small-9 columns"><span id="spryemail">
     <input name="email" type="text" placeholder="email@example.com"/>
    <span class="textfieldRequiredMsg">A value is required.</span></span></div>
  </div> 
  </div> 
   </div>

<div class="row">
             <div class="large-12 columns">
             <label>Check all Products that you're interested in</label>
              <div>
              <input name="products[]" type="checkbox" value="all">
               ALL PRODUCTS/SERVICES

              <input name="products[]" type="checkbox" vallue="trade">Trade-in
              <input name="products[]" type="checkbox" value="layaway">Layaway products
              <input name="products[]" type="checkbox" value="theatre">Home Theatre Systems
              <input name="products[]" type="checkbox" value="TV">HD TVs
               <input name="products[]" type="checkbox" value="Games">Video Game Consoles</label> <br>
               <input name="products[]" type="checkbox" value="laptops"> Laptops</label>
               <input name="products[]" type="checkbox" value="monitors"> Monitors</label>
               <input name="products[]" type="checkbox" value="phones"> Phones</label>
               <input name="products[]" type="checkbox" value="cameras"> Cameras</label>
               <input name="products[]" type="checkbox" value="acoustic"> Acoustic Guitars</label>
               <input name="products[]" type="checkbox" value="electric"> Electric Guitars</label>
               <input name="products[]" type="checkbox" value="drums"> Drums</label>
               <input name="products[]" type="checkbox" value="wind"> Wind Instruments</label> <br>
             <input name="products[]" type="checkbox" value="pianos"> Pianos</label>
             <input name="products[]" type="checkbox" value="violins"> Violins</label>
             <input name="products[]" type="checkbox" value="diamonds"> Diamonds 
             <input name="products[]" type="checkbox" value="neck"> Necklaces
             <input name="products[]" type="checkbox" value="rings"> Rings
             <input name="products[]" type="checkbox" value="ear"> Ear Rings</label>
             <input name="products[]" type="checkbox" value="gold"> Gold Jewelry
             <input name="products[]" type="checkbox" value="silver"> Silver Jewelry
             <hr>

                </div>
                 </div>
        <div class="row">
        <div class="large-12 columns">
         <label>How often would you like to have product updates? <select>
          <option value="daily" name"Updates">Daily</option>
          <option value="weekly" name"Updates">Weekly</option>
           <option value="monthly" name"Updates">Monthly</option>
             </select> 
             </label>
              </div> 
              </div>
                   <div class="row">
                    <div class="large-12 columns">
                    <label>Tell us a little about yourself <textarea placeholder="Type here">
                     </textarea> 
                      </label>
                     </div> 
                     </div> 
             <div class="row">

             <input class="button small large-3" type="submit" name"submit" />
                             </div>
                             </form>

Here's my connecting to the database part:

     <?php
    if (isset($_POST['submit'])){ 

 $con = mysql_connect("localhost","dxh6110","******");
 if(!$con){
 die("Can not connect: " . mysql_error());
}

mysql_select_db("dxh6110",$con);

$sql = "INSERT INTO Signup (Firstname,Lastname,Email) VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";

mysql_query($sql,$con);
mysql_close($con);

}
?>

Also, if I want to enter data from the checkboxes and drop down how would I go about doing that? Will it be the same as textfields?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DLH
  • 67
  • 1
  • 10
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Dec 08 '14 at 03:03
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tyteen4a03 Dec 08 '14 at 03:05
  • okay so basically where I have mysql_ I need to change them to mysqli_? – DLH Dec 08 '14 at 03:19
  • @DLH Sort of. `mysqli_` requires DB connection for all functions and is the first parameter. Visit http://php.net/manual/en/book.mysqli.php for the full manual. – Funk Forty Niner Dec 08 '14 at 03:23

1 Answers1

5

This answer for your originally posted code before your edit and without marking it as an edit.

This is the reason:

You have no equal sign in name"submit" for your submit button.

Change it to name="submit" which your code's execution is based on your conditional statement:

if (isset($_POST['submit']))

It took me a while to spot that one after staring at your code, wondering "why" it wasn't working.

The rest of your code does check out, however I must also state that your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.


As for your checkboxes, you can base yourself on the following:

$checkbox = $_POST['products'];

  for($i=0;$i <sizeof($checkbox);$i++) {
  $query="INSERT INTO your_table (col) VALUES('".$checkbox[$i]."')";
    mysql_query($query,$con) or die(mysql_error());
}

For some added protection:

$firstname= stripslashes($_POST['firstname']);
$lastname= stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);

$firstname= mysql_real_escape_string($_POST['firstname']);
$lastname= mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);

$sql = "INSERT INTO Signup (Firstname,Lastname,Email)  
        VALUES('".$firstname."','".$lastname."','".$email."')";

or mysqli_ method:

<?php
$con = mysqli_connect("myhost","myuser","mypassw","mybd") 
       or die("Error " . mysqli_error($con)); 

$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);

$firstname = mysqli_real_escape_string($con,$_POST['firstname']);
$lastname = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);

$sql = "INSERT INTO Signup (Firstname,Lastname,Email)  
        VALUES('".$firstname."','".$lastname."','".$email."')";

mysqli_query($con,$sql);
mysqli_close($con);
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Oh wow, I see that now. I missed the = sign on some other stuff too. Thank you. So, it's submitting the form but still not adding the selected data to the database? Could it be because of what those guys said up there? the old sql statements? – DLH Dec 08 '14 at 03:16
  • 1
    @DLH You're welcome. If `mysql_` doesn't work, use `mysqli_` and preferably with prepared statements. – Funk Forty Niner Dec 08 '14 at 03:17
  • 1
    @DLH Reload my answer, I've added a `mysqli_` method for you to use (see near the bottom). It's a basic method. Do read up on prepared statements though. I've got to run. Keep me posted, I'll be back tomorrow, *cheers* – Funk Forty Niner Dec 08 '14 at 03:38
  • Wow, thank you. This actually entered information onto my database thank you a ton. It entered data for the email but first and last names are blank. Will keep looking! again Thank you a ton!!! – DLH Dec 08 '14 at 03:45
  • Hmm... i'm not sure why, but what I'm having an issue with now is that it will enter 2 entries to my table. One entry all fields are blank, and the second entry 1 of 3 fields are blank. first name, and last name are blank while email gets submitted correctly. I see no typos, everything looks correct. Any ideas?? – DLH Dec 08 '14 at 04:21
  • heh took me a while to figure this out, but it was because the variable wasn't defined as $firstname or $lastname it was defined simply as $first and $last... which is why $email worked. – DLH Dec 08 '14 at 05:50
  • @DLH Why did you update your question and overwrote your code with mine? People will see your question with my answer and see no difference. As for the two others not entering in DB make sure you've no typos in your column names and that it's the right types and lengths. – Funk Forty Niner Dec 08 '14 at 11:09
  • @DLH yeah I made an error in the variables. I've updated my answer. That's what happens when one's up too late coding. Should work now. If all is good then please mark the question as solved. – Funk Forty Niner Dec 08 '14 at 11:17