maideen Posted September 9, 2017 Share Posted September 9, 2017 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>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/ Share on other sites More sharing options...
PravinS Posted September 9, 2017 Share Posted September 9, 2017 You haven't set value in dropdown options, check below line echo '<option value>' .$row['paramhead']. '</option>'; use it as echo '<option value="'.SOME_VALUE.'">' .$row['paramhead']. '</option>'; Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551008 Share on other sites More sharing options...
maideen Posted September 9, 2017 Author Share Posted September 9, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551010 Share on other sites More sharing options...
PravinS Posted September 9, 2017 Share Posted September 9, 2017 (edited) print_r() the $_POST array at top and check what you are getting in post data, also check the view source of the page to check the drop down values Edited September 9, 2017 by PravinS Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551014 Share on other sites More sharing options...
maideen Posted September 9, 2017 Author Share Posted September 9, 2017 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> Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551015 Share on other sites More sharing options...
PravinS Posted September 9, 2017 Share Posted September 9, 2017 does database table "tbl_paramhead" have data in it? have you checked the view source to check whether the drop down is generated properly...if yes, then if should work Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551017 Share on other sites More sharing options...
Solution mac_gyver Posted September 9, 2017 Solution Share Posted September 9, 2017 (edited) 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 September 9, 2017 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/304917-issue-in-inserting-data-from-dropdown-into-mysql-table/#findComment-1551027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.