0

I have written a PHP page with a form on the submit button I set the action to the PHP form page.

 <form id="form1" method="post" action="../control_lbs/lbs_trace.php">

The INSERT INTO is basic sql load information to the database.

The problem i have every time I open the page it sends blank information to the rows. Is there away I can prevent this from happening?

 $sql = "INSERT INTO lbs_trace_etrack (lbs_msisdn, lbs_req_by, lbs_date_req,     
 lbs_reason, lbs_station, lbs_cas, lbs_traced_by) 
       VALUES     
    ('$_POST[lbs_msisdn]','$_POST[lbs_req_by]','$_POST[lbs_date_req]','$_POST[lbs_reason]'
 ,'$_POST[lbs_station]','$_POST[lbs_cas]','$_POST[lbs_traced_by]')";

The above is my PHP action code

This is the new code and full code I use

 if ($con = mysql_connect($host, $username, $password)) {
    if ( !empty($_POST["send"])) {

       $sql = "INSERT INTO lbs_trace_etrack (lbs_msisdn, lbs_req_by, lbs_date_req,    lbs_reason, lbs_station, lbs_cas, lbs_traced_by) 
       VALUES    ('$_POST[lbs_msisdn]','$_POST[lbs_req_by]','$_POST[lbs_date_req]','$_POST[lbs_reason]','$_POST[lbs_station]','$_POST[lbs_cas]','$_POST[lbs_traced_by]')";
        if (mysql_query($sql, $con)) {
            $insertSuccessful = true;
        } else {
            echo $sql;
            echo "\n" . mysql_error($con);
            echo "mysql err no : " . mysql_errno($con);
        }

On refresh or page entry it still gives me blank info on Database

Maggie Ackermann
  • 253
  • 1
  • 4
  • 15

1 Answers1

0

You need to use isset() to see if the $_POST variables are set. I've use $_POST in the example below, I suggest you give the submitbutton a name (like example) and use isset($_POST['example']):

if( isset($_POST) ){
    $sql = "INSERT INTO lbs_trace_etrack (lbs_msisdn, lbs_req_by, lbs_date_req, lbs_reason, lbs_station, lbs_cas, lbs_traced_by)
            VALUES(
                '".$_POST['lbs_msisdn']."',
                '".$_POST['lbs_req_by']."',
                '".$_POST['lbs_date_req']."',
                '".$_POST['lbs_reason']."',
                '".$_POST['lbs_station']."',
                '".$_POST['lbs_cas']."',
                '".$_POST['lbs_traced_by']."'
            )";
  echo $sql; // echo it to see if it has any values
  // print_r($_POST); // in case the query is still empty, uncomment this. It will show you the values in the POST array
}
Martijn
  • 15,791
  • 4
  • 36
  • 68
  • I have changed as shown in my edit but it still sends blank to the database – Maggie Ackermann Aug 13 '13 at 13:03
  • It might have something to do with quotes, I've updated my answer to a more readable format. I've also added the echo, this way you can see what the actual query turns out to be. If it has empty values, the post array does not have any values. – Martijn Aug 13 '13 at 13:08
  • I suggest you find out what makes the difference instead of copy/pasting my code, for future reference – Martijn Aug 13 '13 at 13:14
  • I think I have puzzled that out if I am correct the way I was posting the values from the form the '".$_POST['lbs_traced_by']."' what you have shows the specific to look for and without the point there it did not look for the completed form in that text field? – Maggie Ackermann Aug 13 '13 at 13:21
  • I don't understand what you just posted. I suggest you look around for information about 'variables in strings' – Martijn Aug 13 '13 at 13:24