0

Hi I have a drop down list that has values from one db table and I want on button click to save the row of the specific id in another table I have this form

<table class="table table-bordered table-responsive">
    <tr>
    <td><label class="control-label">Drop down list</label></td>
    <td class="col-xs-4">
        <?php
            $stmt01 = $DB_con->prepare('SELECT * FROM dbtable WHERE chart in (458,459,461) ORDER BY id ASC');
            $stmt01->execute();
            if($stmt01->rowCount() > 0)
            {
            ?>
                <select class="form-control" name="value01">
                    <?php
                        while($row=$stmt01->fetch(PDO::FETCH_ASSOC))
                        {
                            extract($row);
                            echo '<option value="'.$row['id'].'">'.$row['id'].' '.$row['lastName'].' '.$row['firstName'].'</option>';
                        }
                    ?>
                </select>
            <?php
            }
            ?>
        </td>
        <td colspan="2" align="right" class="col-md-2"><button type="submit" name="btnsave01" class="btn btn-default">
            <span class="glyphicon glyphicon-save"></span> &nbsp; Insert
            </button>
        </td>
    </tr>

And my php is

require_once 'dbconfig.php';
if (isset($_POST['btnsave01']))
{
    if (isset($_POST['value01']))
    {
        $chart = $_GET['chart'];
        $chartDescription = $_GET['chartDescription'];
        $lastName = $_GET['lastName'];
        $firstName = $_GET['firstName'];
        $location = $_GET['location'];
        $empPic = $_GET['empPic'];

        $q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, uempPic)');
        $q01->bindParam(':uchart',$chart);
        $q01->bindParam(':uchartDescription',$chartDescription);
        $q01->bindParam(':uregNo',$regNo);
        $q01->bindParam(':ulastName',$lastName);
        $q01->bindParam(':ufirstName',$firstName);
        $q01->bindParam(':ulocation',$location);
        $q01->bindParam(':uempPic',$empPic);
    }
}

Can you help me fix this? The button is working fine but the value is not stored inside db table

Thank you

2
  • @ManojSharma All edited and fixed by your answers but I am getting this error Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'chart' cannot be null' ( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'chart' cannot be null Commented Dec 9, 2016 at 7:30
  • Thank you all for helping I fixed it by another thought I came up Thanks again Commented Dec 9, 2016 at 12:57

3 Answers 3

1

2 corrections in your PHP

  1. Missing : in VALUES of INSERT query. Replace uempPic with :uempPic.

  2. INSERT query is not executed. Add below line after last bindParam statement.

    $q01->execute();
    
Sign up to request clarification or add additional context in comments.

4 Comments

I printed all php variable $chart etc and all of them are null SQLSTATE[23000] Why my code doesn't get the selected id from dropdown columns?
Can you help why I am getting null values ?
That's because, you have submitted values using POST and are fetched with $_GET. Use $_POST instead to retrieve values submitted.
I changed that and now I am using $_POST but have null values from the dropdown to insert to the other table on submit
0

Error at this line please check it...

 $q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, :uempPic)');

Comments

0

Please have a look on your prepare statement:

$q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, uempPic)');

You have forgot to add ":" with "uempPic", Please use below statement.

$q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, :uempPic)');

You have not included execute statement. Add below statement after bindParam.

$q01->execute();

I think your insert issue will get resolved.

9 Comments

It means , you should check the value of field 'chart', It should have some value. Please try to print your query and check the value of $chart variable. when $chart will fix then it will work.
The 1st db table is full of values and represented correctly in other page views The thing is that I cannot copy these from the dropdown to the other empty db table with the same columns The $chart variable should contain the chart row column value Something I am not doint correct in my code
All of the fields values are null I var dumped them and they did not get value from dropdown
Actually you have not posted whole Form code at here, But I will ask you to verify this points. 1. Please let me know form method. If you are not able to do please change $_GET to $_REQUEST. 1 more thing to remember I am not able to see name of this drop-down, Please set the name of this drop-down then definitely you will get the value of $chart.
1. form method = post 2. name of the dropdown <select class="form-control" name="value01"> I've change the GET to REQUEST and getting null values same SQL error I want to take all data from dbtable (as I picked the id from the dropdown) to resultsdbtable with same columns (empty at first and as I press button insert will copy the row to the result table)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.