简体   繁体   中英

PHP and SQL one page insert into database

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM