OK, I have been banging my head against this for 2 days. I am new to PHP so please bear with me. I just want to have a user enter 5 fields in a form and post it to my db. I can already search the db and display the data that exists in the db. no problems there, so I know my connection string info is working.

But, whatever I try to do to enter the data in isn't working. here is my latest version of code:

<html>
<body>

<?php

if ($submit) {

// process form

$db = mysql_connect("XXXX", "XXXX", "XXXX");

//connect to the db
mysql_select_db("mydbname",$db)or die("Could not select the database '" . $db . "'. Are you sure it exists?");

//try to get the data from the URL
$SERVER['QUERY_STRING']; //possibly unneeded if $get does what it should
$country = ($GET['country']);
$state = ($
GET['state']);
$city = ($GET['city']);
$user = ($
GET['user']);
$email = ($_GET['email']);

$sql = "INSERT INTO users (country,state,city,user,email) VALUES ('$country','$state','$city','$user','$email')";

$result = mysql_query($sql) or die("Query failed");

echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="retrieve" action="<?php echo $PHP_SELF?>">

Country:<input type="Text" name="Country"><br>
State:<input type="Text" name="State"><br>
City:<input type="Text" name="City"><br>
Name:<input type="Text" name="user"><br>
E-Mail Address:<input type="Text" name="email"><br>
<input type="Submit" name="submit" value="Enter information">

</form>

<?php
} // end if
?>
</body>
</html>

So, please tell me why no data gets put into my DB. I am about to scream.

Thanks!

-Tony
SSG Anthony Damata - HHB 197th FA BDE

    1. in your html form tag, it should read method=get
      (on a side note, I think it would be better to use "post" instead. If you decide to use post, however, you need to change all your $GET[]s to $POST[].

    2. be consistent in your naming of the form elements. I would suggest you use all lower case, since that is what your php code is using (i.e., name="country" -- not name="Country").

    3. you won't need $_SERVER['QUERY_STRING'].

    4. when in doubt, use my favorite line of code (substitute $POST, if you decide to go that way):

      echo '<pre>'.print_r($_GET, true).'</pre>';

      This should let you see what the browser is returning.

    After you get these things in order, let me know if it is still not working.

    Good luck!

      Heres a simple example, I suggest you read up on form validation btw

      This can be done all in one script

      form.php

      <?php
      
      if(!isset($_POST['form_name']))
      
      {
      
      echo "Try submitting the form next time :)";
      
      }else{
      
      ?>
      
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form_name" method="post">
      
      # Be sure to use maxlength, to stop innocent mistakes
      <input type="text" name="input1" maxlength="20">
      
      # Post thier ip
      <input type="hidden" name="thier_ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
      
      <input type="submit" name="button" value="Submit">
      
      </form>
      
      <?php
      
      # This is where where the work begins
      }else{
      
      # Check if posted, If its zero, check if greater then 20, If so echo error
      
      if(!isset($_POST['input1']) || (strlen($_POST['input1']) < 1)|| (strlen($_POST['input1']) > 20))
      
      {
          echo "Invalid entry in input1";
      }else{
          $moo = trim($_POST['input1']);
      }
      if(!isset($_POST['thier_ip']) || (strlen($_POST['thier_ip']) < 1) || (strlen($_POST['thier_ip']) > 16))
      
      {
          echo "Sneaky...";
      }else{
          $ip = trim($_POST['thier_ip']);
      }
      
      require_once('db_inc.php');
      
      $query = "INSERT INTO table (column1, column2) VALUES ('$moo', '$ip')";
      
      $result = mysql_query($query);
      
      if($result)
      {
      
         echo "Database successfully updated";
      }else{
         echo "Query unsuccessful";
      }
      ?>
      
      

      Theres a basic idea of how it works

        Thank you guys. I will give this a try tonight. I really appreciate the help. 🙂

          OK I just gave it a try.

          I made the corrections corey suggested and it still isn't working. What happens now is that the data I enter in the form gets put into the URL after I click submit, but then nothing happens. It just goes back to the form with enter fields.

          where does this line go in the script?

          echo '<pre>'.print_r($_GET, true).'</pre>';

          Nothing happens when I put it in (I had it under the _GET statements)

          Are my _get statements in the right place to pull the data from the URL?

          (I have used these before in doing a search script, so getting data out of the DB with this method has worked.) Its just killing me that I cannot get it in the DB.

          I thank you again for your help.

          -Tony

            Well, I finally figured it out.

            It was a bit of teh POST/GET stuff. plus, i decided to make it two pages. an HTML page that sent the data to the php script. this worked like a charm.

            Thanks for all your help. It was really appreciated. 🙂

            -Tony

              Write a Reply...