-2

Hi I am new to php. I am working on a form which creates a dynamic table when the page it opened the problem is that the table consist of the input text boxes which I don't have an idea to save it into my database I know the basic insert query but this one is a tricky part can anyone help me out here is my code

            <table border='0px'>
                <?php 
                $c = 1;
                $scomp = mysql_query("SELECT * FROM subject WHERE compulsory!='$c'")or die(mysql_error());
                $v =0;
                while($fsub = mysql_fetch_array($scomp)){

                $ycode = $fsub["code"];
                echo "<tr>";
                echo "<td width='200px'><font color='#FF6600'><strong>$fsub[name]</strong></td><td><input name='s_$ycode' type='text' size='1' maxlength='2'></td>";
                echo "</tr>";
                $v++;
                }
                 echo "<input id='hh' name='hh' type='text' value= '$v'/>";
                ?>
            </table> 
Ravichandran Jothi
  • 3,028
  • 11
  • 52
  • 82
kumail
  • 59
  • 2
  • 6
  • 16
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Feb 19 '13 at 10:29

1 Answers1

-1

You are not using the concatenation operator for joining the strings with the variables. The concatenation operator in PHP is (.) the dot operator. Even when you run mysql query you will not get any records.

Below is the correct syntax for your query

$scomp = mysql_query("SELECT * FROM subject WHERE compulsory!= ".$c);

below is your fixed code. Try it now

            $c = 1;
            $scomp = mysql_query("SELECT * FROM subject WHERE compulsory!=".$c)or die(mysql_error());
            $v =0;
            while($fsub = mysql_fetch_array($scomp)){

            $ycode = $fsub["code"];
            echo "<tr>";
            echo "<td width='200px'><font color='#FF6600'><strong>".$fsub[name]."</strong></td><td><input name='s_".$ycode."' type='text' size='1' maxlength='2'></td>";
            echo "</tr>";
            $v++;
            }
             echo "<input id='hh' name='hh' type='text' value= '$v'/>";
            ?>
        </table> 
  • sir thnks fr ur help but i dnt knw hw to save it into my database – kumail Feb 19 '13 at 10:40
  • "You are not using the concatenation operator for joining the strings with the variables." — Correct. He is using interpolation instead. This is both fine and somewhat cleaner. – Quentin Feb 19 '13 at 10:49
  • `$scomp = mysql_query("SELECT * FROM subject WHERE compulsory!= ".$c)` — Now the data isn't being quoted. Since, in this particular case, we can see that `$c` is an integer, that doesn't actually make a difference, but it isn't a good thing to do in general. – Quentin Feb 19 '13 at 10:50
  • So this answer "corrects" something that doesn't need to be corrected but doesn't address the question at all. – Quentin Feb 19 '13 at 10:50