1

There is a dropdown list in the form which are populated with values from mysql db. The table fields for the dropdown is a enum type. I am trying to insert the value selected by the user into the database. I am only getting as a result a NULL. How can I insert the selected value from the dropdown into the table? SITE

PHP

<?php

$db_con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

if(isset($_POST['submit'])) {

$db_insert  = $db_con->prepare("INSERT INTO academy (name, type, academy_id) VALUES (?,?,?,?)"); 
$db_insert->bind_param('ssi', $_POST['name'], $type_value, $_POST['acad_id']);
$db_insert->execute();

print_r($_POST);
}

?>
<form action="test7.php" method="POST">
        Name: <input type="text" name="name"></br>
        Type: 
            <?php

                $table_name = "academy";
                $column_name = "type";

                echo "<select id=\"$column_name\" name=\"$column_name\"><option>Select one</option>";
                $q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
                $r = mysqli_query($db_con, $q);

                $row = mysqli_fetch_array($r);
                //print_r($row);
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
                //print_r($enumList);
                foreach($enumList as $type_value){
                    echo "<option value='$type_value'>$type_value</option>";
                }
                echo "</select></br>";

            ?>
        Academy ID: <input type="text" id="acad_id" name="acad_id"></br>
    <input value="SAVE" name="submit" type="submit">
</form> 

TABLE

CREATE TABLE IF NOT EXISTS `academy` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) DEFAULT NULL,
  `academy_id` int(11) DEFAULT NULL,
  `type` enum('INACTIVE','ACTIVE') DEFAULT 'ACTIVE',
PRIMARY KEY (`id`)
);
1
  • There is no column 'TYPE' in your table, but you have used in your insert statement.. Commented Nov 18, 2013 at 6:08

2 Answers 2

2

Column mismatched ('ssi',),

 $db_insert  = $db_con->prepare("INSERT INTO academy (name, type, academy_id) VALUES (?,?,?)"); 
 $db_insert->bind_param($_POST['name'], $_POST['type'], $_POST['acad_id']);
Sign up to request clarification or add additional context in comments.

Comments

0
if(isset($_POST['submit'])) {

$db_insert  = $db_con->prepare("INSERT INTO academy (name, type, status, academy_id) VALUES (?,?,?,?)"); 
$db_insert->bind_param('ssi', $_POST['name'], $_POST['type'], $_POST['acad_id']);
$db_insert->execute();

print_r($_POST);
}

no need set value into status column??? if no need you should remove one question mark '?' that from your query

if(isset($_POST['submit'])) {

$db_insert  = $db_con->prepare("INSERT INTO academy (name, type, academy_id) VALUES (?,?,?)"); 
$db_insert->bind_param('ssi', $_POST['name'], $_POST['type'], $_POST['acad_id']);
$db_insert->execute();

print_r($_POST);
}

if need you should add 's' add your bind_param and value of status, as below

if(isset($_POST['submit'])) {

$db_insert  = $db_con->prepare("INSERT INTO academy (name, type, status, academy_id) VALUES (?,?,?,?)"); 
$db_insert->bind_param('sssi', $_POST['name'], $_POST['type'], $status, $_POST['acad_id']);
$db_insert->execute();

print_r($_POST);
}

Comments

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.