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
maideen
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

Hi
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
Maideen

Here is my HTML code

Code: Select all

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

my PHP code

Code: Select all

if(isset($_POST['add']))
{
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
      $staffid =$_POST['staffid'];
      $staffname =$_POST['staffname'];
      $gender =$_POST['gender'];
      $department=$_POST['department'];
      $emailid=$_POST['emailid'];
      $hpno=$_POST['hpno'];
      $address=$_POST['address'];
      $city=$_POST['city'];
      $state=$_POST['state'];
      $country=$_POST['country'];
      $status=$_POST['status'];
      $createdby=$_COOKIE['username'];
      $time = strftime("%X");
      $date = strftime("%B %d,%Y");
      $createdon = date("Y-m-d H:i:s");
      $terminal=$_COOKIE['terminal'];

      $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')";
      $stmt=$pdo->prepare($sql);
      $stmt->execute();
      $pdo = null;
      print '<script>alert("Saved");</script>';
      header("location:staff_add.php");  
    }
}  


User avatar
hyper
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;
$stmt->execute();
although there are other ways, you must keep it consistent.

--------------------------------------------------------------------------------------------

You cannot do this:

Code: Select all

print '<script>alert("Saved");</script>';
header("location:staff_add.php");
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

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

Post Reply