MintY
MintY

Reputation: 643

Values not inserting into database table

I am trying to insert the values from drop-down and text box throug web page.

first there will be one drop down box that has numbers from 1 to 25..

when i select the number from the dropdown, the corresponding textboxes and dropdown boxes are created.

But when i enter the data and click the submit button,the values are not going to database, instead only the last row values are inserting into database.

for example:

say i have selected 4 from number dropdown, now 4 rows with textbox and dropdown box appears.

when i enter the data in the textbox and select value from dropdown and click submit. the last row values are getting inserted 4 times.

i want it to insert the values correctly...How can i solve this.?

here is the code i am using..

code:

<html>
<head>
<link rel="stylesheet" href="css/common.css" type="text/css">
<link rel="stylesheet" type="text/css" href="css/page.css">
<link rel="stylesheet" type="text/css" href="css/button.css">
<link href="css/loginmodule.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="js/jquery.js"></script>
<script type='text/javascript' src='js/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />

<script type="text/javascript">
$().ready(function() {
    $("#Fname").autocomplete("get_course_list.php", {
        width: 260,
        matchContains: true,
        //mustMatch: true,
        //minChars: 0,
        //multiple: true,
        //highlight: false,
        //multipleSeparator: ",",
        selectFirst: false
               });
               });
          </script>

          <script>
            function showUser(str) {
                if(str=="") {
                    document.getElementById("txtHint").innerHTML="";
                    return;
                }

                if(window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                } else {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange = function() {
                    if(xmlhttp.readyState==4 && xmlhttp.status==200) {
                        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","gettheater.php?q="+str,true);
                xmlhttp.send();
                }
        </script>


       <script type="text/javascript">


    function create(param) {
    'use strict';
    var i, target = document.getElementById('screens');
    target.innerHTML = '';
    target.innerHTML = '<input name="RowCount" value="' + param + '" hidden />';
    for(i = 0; i < param; i += 1) {

       target.innerHTML +='</br>';
       target.innerHTML +='New Movie '+i+'  ';
       target.innerHTML += '<input type="text" name="Fname">';
       target.innerHTML +='  '+'Language '+'  ';
       target.innerHTML += "<?php 
        try {
            $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }

        $sql = 'SELECT language FROM languages;';

        $sth = $dbh->prepare($sql);
        $sth->execute();

        echo "<select name='language' id='course'>";
        echo "<option>----Select Language----</option>"; 
        while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            echo "<option value='" . $row['language'] ."'>" . $row['language']. "        </option>";
        }
        echo "</select>";
?>";

       target.innerHTML +='</br>';
       target.innerHTML +='</br>';
    }
}


</script> 

<style>

#screens{
color:black;
}

</style>



</head>
<body>


<h1>Welcome <?php echo $_SESSION['SESS_FIRST_NAME'];?></h1>
<a href="member-profile.php" class="log">My Profile</a> | <a href="logout.php" class="log">Logout</a>
<p>This is a password protected area only accessible to Admins. </p>

<center>



<div class="pan"><br><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" autocomplete="off">

<div class="head">Update Theater Information</div>
<table>
<tr>
<td><label for="screens">New Movies Released</label></td>
<td>

<select id="select" onchange='javascript:create(this.value);' name="range">
    <option>--Select Number Of New Movies Released--</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
</select>

</td>
</tr>


<tr>
<td></td>
<td>
<div id="screens">

</div>
</td>
<td></td>
</tr>

<tr>
<td></td>
<td><input type="submit" class="button" name='submit' value="Submit" /></td>

</tr>
</table> 
</form><br><br>
</div>

<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
}
?>

</center>
</body>
</html>

Upvotes: 1

Views: 2028

Answers (5)

sidneydobber
sidneydobber

Reputation: 2910

I don't want to be an ass, but I would suggest cleaning things up and seperating the JavaScript, jQuery and PHP even loose JavaScript AJAX and use jQuery AJAX. It's no surprise code like this is giving you bugs.

Upvotes: 0

MintY
MintY

Reputation: 643

Thank god....I resolved My issue.....i used the same logic that was applied to first text box... and now the dropdown values are also getting inserted....

i added array to language[]

echo "<select name='language[]' id='course'>";

and added the $i in loop

.$_POST['language'][$i]

Thanks all for helping me.....

HaPpY CoDiNg....

Upvotes: 0

Md. Sahadat Hossain
Md. Sahadat Hossain

Reputation: 3236

Use this

$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('".$_POST['Fname']."','".$_POST['language']."') ") or die(mysql_error());

instead of

$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
  1. You must keep php code outside of quote and Concate them

2.If The index of $_POST Array is string then it must be with quote. (e.x: $_POST[language] it must be $_POST['language']

Upvotes: 0

Dinesh
Dinesh

Reputation: 4110

1.Use mysqli_* function instead of mysql_* function... 2. What is error showing up.

If you have insertion query on same page then try it:.....

<?php
if(isset($_POST['submit'])
{
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
for ($i=0; $i < $_POST["range"] ; $i++)
{
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
}
}
?>

It will triger your query when submit button will hit.... may this help

Upvotes: 1

Shafeeque
Shafeeque

Reputation: 2069

You should change you name to array like

target.innerHTML += '<input type="text" name="Fname[]">';

And also in insert query should be

$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('".$_POST['Fname'][$i]."','".$_POST['language']."') ") or die(mysql_error());

Upvotes: 2

Related Questions