-3

I am using this code. To insert name and rollno in table named 'tablename' in database 'dbname'.

db.php

<html>
    <body>
        <form name="db" action="db.php" method=post>
            Enter name <input type=text name="nam"/><br><br>
            Enter RollNo. <input type=text name="rno"/><br><br>
            <input type=submit value="Click me"/>
        </form>

<?php
$nam = $_POST['nam'];
$rno = $_POST['rno'];
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());
$sql="insert into tablename values('$nam','$rno')";
 mysql_query($sql);
?>
</body>
</html>

It show the error, that
Notice: Undefined index: nam in C:\wamp\www\DB Connectivity\db.php on line 10
Notice: Undefined index: rno in C:\wamp\www\DB Connectivity\db.php on line 11

Plz give solution, how to use html and php in the same page without this error.

Mdumanoj
  • 517
  • 3
  • 9
  • 27
  • 1
    **Danger**: 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). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 23 '14 at 08:00

2 Answers2

0

You're not checking if form is submitted or not so just give name to your submit element & have to check using isset,

    <input type=submit name="submit" value="Click me"/>
    </form>

<?php
if(isset($_POST['submit'])){
     $nam = isset($_POST['nam']) ? $_POST['nam'] : '';
     $rno = isset($_POST['rno']) ? $_POST['rno'] : '';
     //And rest of your php operation you need to perform after submit.
?>

Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • thanks for your answer. But still here is some problem. It show error in this line. $sql="insert into tablename values('$nam','$rno')"; – Mdumanoj Mar 23 '14 at 10:12
0

Try This

    <?php 
    $connection = mysql_connect("localhost","root","") or die(mysql_error());
    $db = mysql_select_db("dbname") or die(mysql_error());
    $nam = $_POST['nam'];
    $rno = $_POST['rno'];
    if(!empty($_POST['submit'])){
       $insert_qry = mysql_query("INSERT INTO `table_name` (`nam`, `rno`) VALUES ('".$nam."','".$rno."') ") or die(mysql_error());
    }
   ?>
<html>
<body>
   <form name="db" action="" method="post">
      Enter name
      <input type="text" name="nam"/>
      <br>
      <br>
      Enter RollNo.
      <input type="text" name="rno"/>
      <br>
      <br>  
      <input type="submit" name="submit" value="Click me"/>
  </form>
</body>
</html>
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29
Dipesh
  • 387
  • 6
  • 16