Jump to content

issue in inserting data from dropdown into mysql table


Go to solution Solved by mac_gyver,

Recommended Posts

Hi

I am new in PHP. I have used dropdown  to bind with my sql data. It is ok

But I could not insert data into table only from dropdown. Other controls like text can be inserted.

I have given code below. 

Pls advise me.

Maideen

 



<?php require_once '../inc/header.php'; ?>
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption">
<i class="icon-social-dribbble font-green"></i>
<span class="caption-subject font-green bold uppercase">Parameter</span>
</div>
</div>
<form class="forget-form" action="../classes/cls.parameter.php" method="POST">
<div class="portlet-body">
<div class="form-group">
<label for="default" class="control-label">Parameter Details</label>
<input id="default" type="text" class="form-control" placeholder="Parameter Details" name="paramdetails">
</div>
<div class="form-group">
<label for="single" class="control-label">Parameter head</label>
<select id="paramhead" class="form-control select2" name="paramhead">
<option>-- Select --</option>
<?php
$sql = "select * from tbl_paramhead order by paramhead";
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch())
{
echo '<option value>' .$row['paramhead']. '</option>';
}
?>
</select>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn green uppercase btn btn-danger mt-ladda-btn ladda-button" data-style="zoom-out" name="paramhead">Submit</button>
</div>
</form>
</div>
<?php require_once '../inc/footer.php'; ?>





<?php
require_once '../inc/config.php';
if(isset($_POST['paramhead']))
{
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$paramhead =$_POST['paramhead'];
$paramdetails =$_POST['paramdetails'];
$bool = true;
$sql="insert into tbl_parameter(paramhead,paramdetails) values ('$paramhead','$paramdetails')";
$stmt=$pdo->prepare($sql);
$stmt->execute();
$pdo = null;
print '<script>alert("Saved");</script>';
print '<script>window.location.assign("../admin/parameter.php");</script>';
}
}
?>

thanks for reply Mr.Pravin

 

I have added

echo '<option value ='.$row['paramhead'].'>' .$row['paramhead']. '</option>';

But Still same, cannot insert into data table

 

Where did i do mistake>

Pls advice me

 

Thank you again

Mr.Pravin S

 

I have checked as per advice, data is not posting only from dropdown control and normal text control can be posted.

Dropdown control fetch data from database table. Have you any advice, pls.....

 

                    <select id="paramhead" name="paramhead" class="form-control select2" >
                        <option>-- Select --</option>
                        <?php
                              $sql  = "select * from tbl_paramhead order by paramhead";
                              $stmt   = $pdo->prepare($sql);
                              $stmt->execute();
                                  while ($row = $stmt->fetch())
                                  {
                                    $optvalue = $row['paramhead'];
                                    //print '<option value ='.$optvalue.'>' .$row['paramhead']. '</option>'; 
                                    echo '<option value ='.$row['paramhead'].'>' .$row['paramhead']. '</option>';
                                  } 
                            //$pdo = null;
                        ?>
                    </select>
  • Solution

Hi

I have used dropdown  to bind with my sql data. It is ok

 

actually, no, it's not. there are two problems with the form -

 

1) the value attributes, that have already been pointed out.

 

2) the name you have given the submit button is the same as the select field name and only the last field with the same names will be used, so, you are getting the submit button value, which there is none, rather than the select field value. use a different name for the submit button.

 

however, you need to do some other things -

 

1) the value you use for the options should be an id (auto-increment column) from tbl_paramhead, not the text/label string, because you should store an id in any table holding data related to a tbl_paramhead selection.

 

2) in your form code, the sql query does not have any external data being supplied to it, and using a prepared query here is wasting time typing and executing code that isn't doing anything useful. change the ->prepare() and ->execute() calls to just $stmt = $pdo->query($sql); the rest of the code using the result from the query will remain the same.

 

3) in your form processing code, you have missed a point of using a prepared query. you do not put data values directly into the sql query statement when using a prepared query. you put place-holders in the sql query statement for the values (without any single-quotes around the place-holders), then you supply the data values when you execute the query.

 

also, in your form processing code -

 

4) the if($_SERVER["REQUEST_METHOD"] == "POST") conditional test should come first and/or be the only test you need. if your form processing code will only handle a single form, there's no need for any additional logic.

 

5) you need to validate all submitted form data before using it. if the text field is empty or no select option was picked, you should not run the INSERT query. if you use an array to hold he validation error messages, you can just test if the array is empty to dected if there are no validation errors.

 

6) there's no good reason to write out line after line of code that is just copying variables to other variables. the original variables are perfectly fine to use in the rest of the code. what happens if your form has 10 fields? are going to write out 10 lines of code copying each of the $_POST variables to other variables? the answer to this question should be no.

 

if you do have a need to copy an array to other variables, such as if you are making a trimmed copy of the data, you can do it using a single statement,

 

7) if you put the form processing code and the form on the same page, you can display any validation errors when you display the form and you can populate the form fields with the submitted values when there is an error, so that the visitor doesn't need to keep filling in/selecting field values, they only need to correct the fields that have errors.

Edited by mac_gyver
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.