Problem in inserting partial data from dropdown to mysql using PHP

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 30
Joined: Mon Mar 07, 2011 11:38 pm

Sat Sep 16, 2017 6:34 pm

I have an issue to insert data from dropdown to mysql.
It is inserting the partial data to database
Example: if selection is Account Dept, but inserted only Account. missing Dept
if selection is Non Active, inserted only Non. missing Active.
whatever coming after space, it cannot be inserted. It happens only in dropdown control.

In Database, using varchar(30) for column

Pls advice where I did wrong.

Thank you

Here is my HTML code

Code: Select all

                                <select class="form-control" data-placeholder="Choose a Category" name="status">
                                          $sql  = "select * from tbl_parameter where paramhead ='STATUS' order by paramdetails";
                                          $stmt = $pdo->prepare($sql);
                                              while ($row = $stmt->fetch())
                                                $optvalue = $row['paramdetails'];
                                                print '<option value ='.$optvalue.'>' .$row['paramdetails']. '</option>'; 

my PHP code

Code: Select all

      $staffid =$_POST['staffid'];
      $staffname =$_POST['staffname'];
      $gender =$_POST['gender'];
      $time = strftime("%X");
      $date = strftime("%B %d,%Y");
      $createdon = date("Y-m-d H:i:s");

      $bool = true;
      $sql="insert into tbl_staff_master(staffid,staffname,gender,department,emailid,hpno,address,city,state,country,status,createdby,createdon,terminal) 
                                values ('$staffid','$staffname','$gender','$department','$emailid','$hpno','$address','$city','$state','$country','$status','$createdby','$createdon','$terminal')";
      $pdo = null;
      print '<script>alert("Saved");</script>';

User avatar
php-forum Active User
php-forum Active User
Posts: 498
Joined: Mon Feb 22, 2016 5:52 pm

Sun Sep 17, 2017 6:45 am

You have a few problems:

Your sql prepare and execute should be more like this:

Code: Select all

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
although there are other ways, you must keep it consistent.


You cannot do this:

Code: Select all

print '<script>alert("Saved");</script>';
You should have an error stating headers have already been sent, you must not use echo or print or any other method of sending output if you intend to send headers - headers must be set first.

In any case that snippet makes no sense, i.e. sending a page out to have an alert at the same time as changing the page location.

Turn error reporting on and you will see most of your problems, put this at the top of your script (first script executed):

Code: Select all

ini_set('display_errors', 1);

Post Reply