Question

<form method="POST">
<input type="submit" name="submit" value="submit" />
<?php
if (isset($_POST['submit']))
    {      
    include '_includes/db.php';

     mysql_query("INSERT INTO `en_stud` (en_subj, en_desc, en_code) 
     SELECT subj_code, subj_name, subj_deg FROM `subjects` WHERE subj_deg='PREP'"); 

            }

?>
</form>

Hi, Ok, so i used the INSERT SELECT for this, in order to insert all regular subjects for prep inside the database, but what if i wanted to add something. Like

Adding

Input Box and another value separated into the select table command, how would i be able to add it there?

<form method="POST">
    <input type="text" name="f" value="1"
    <input type="submit" name="submit" value="submit" />
    <?php
    if (isset($_POST['submit']))
        {      
        include '_includes/db.php';

            $f = "$_POST['f']";

         mysql_query("INSERT INTO `en_stud` (en_fname, en_subj, en_desc, en_code) 
         SELECT subj_code, subj_name, subj_deg FROM `subjects` WHERE subj_deg='PREP'"); 

                }


    ?>
    </form>

As you can see the "en_fname" doesn't have any corresponding values, does this have any corresponding code to make it work?

<?php
if (isset($_POST['submit']))
    {      
    include '_includes/db.php';

        $f = "$_POST['f']";

     mysql_query("INSERT INTO `en_stud` (en_fname, en_subj, en_desc, en_code) VALUES ('$f')
     SELECT subj_code, subj_name, subj_deg FROM `subjects` WHERE subj_deg='PREP'"); 

            }


?>
</form>
Was it helpful?

Solution

Try with

mysql_query("INSERT INTO `en_stud` (en_fname, en_subj, en_desc, en_code) 
     SELECT '$f', subj_code, subj_name, subj_deg FROM `subjects` WHERE subj_deg='PREP'"); 

Basically you have to put all values in SELECT list. So instead of trying to join own values with select values, just put additional values in select list. They are constant, so it's like

Select 1,2,'three', some_field FROM some_table
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top