-1

This is the registration form, I want its data to be stored in MySQL database

enter image description here

It prints else part always i.e. failed to submit

enter image description here

My Php file below: I made a simple registration form. I want its data to be stored in database I mentioned in $db variable. I already created database in phpmyadmin which I mentioned in the file. But I don't know why this code is not working. Please tell me what line of code is wrong and how should I correct it.

<?php
 $host="localhost";
 $user="root";
 $pwd="";
 $db="mohita";

$conn=mysqli_connect($host,$user,$pwd,$db) or die("unable to connect");
?>

<html>
   <head>
      <title>Try here</title>
   </head>

   <body>
      <form method="post">
       <input type="text" name="yourname"/>
       <input type="email" name="mailid"/>       
       <input type="password" name="mypass"/>
       <input type="number" name="phn"/>
       <textarea rows="5" cols="20" name="address"/></textarea>
       <input type="submit" name="reg"/>
      </form>
   </body>
</html>

<?php
  if(isset($_POST['reg'])){
       $yourname=$_POST['yourname'];
       $mailid=$_POST['mailid'];
       $mypass=$_POST['mypass'];
       $phn=$_POST['phn'];
       $address=$_POST['address'];

     $query="insert into login(Name,Email,Pass,Mobile,Address) values('$yourname','$mailid','$mypass','$phn','$address')";

    $run=mysqli_query($conn,$query);

    if($run){
       echo "<h1>Data submitted successfully</h1>";
     }

    else{
       echo "<h1>Failed to submit data</h1>";
     }
}
?>
Cid
  • 13,273
  • 4
  • 22
  • 42
  • Seems to be
    you have not mentioned action attribute. Action attribute help the form to send the values on desire location
    in your case leave this as blank.
    – navjot singh Feb 01 '19 at 08:28
  • @navjotsingh: the default action is the current page. You don't need an empty action value. –  Feb 01 '19 at 08:34
  • You can't *"Connect php file to phpmyadmin"*. phpMyAdmin is a tool (like MySQL Workbench) used to manage MySQL databases. – Cid Feb 01 '19 at 08:36
  • Your query is vulnerable to [SQL Injections](https://en.wikipedia.org/wiki/SQL_injection) consider using prepared statements – Cid Feb 01 '19 at 08:42
  • Any error message by the way? – Cid Feb 01 '19 at 08:43

1 Answers1

-2

Try this and let me know if this works!

  <html>
  <head>
  <title>Try here</title>
 </head>

 <body>
  <form method="post" action="">
   <input type="text" name="yourname"/>
   <input type="email" name="mailid"/>       
   <input type="password" name="mypass"/>
   <input type="number" name="phn"/>
   <textarea rows="5" cols="20" name="address"/></textarea>
   <input type="submit" name="reg"/>
  </form>
<?php
if(isset($_POST['reg'])){
   $host="localhost";
    $user="root";
    $pwd="";
   $db="mohita";
   $yourname=$_POST['yourname'];
   $mailid=$_POST['mailid'];
   $mypass=$_POST['mypass'];
   $phn=$_POST['phn'];
   $address=$_POST['address'];

  $conn=mysqli_connect($host,$user,$pwd,$db) or die("unable to connect");
 $query="insert into login(Name,Email,Pass,Mobile,Address) values('$yourname','$mailid','$mypass','$phn','$address')";

 $run=mysqli_query($conn,$query);
   if($run){
   echo "<h1>Data submitted successfully</h1>";
 }

else{
   echo "<h1>Failed to submit data</h1>";
 }
}
?>

  </body>
 </html>
navjot singh
  • 149
  • 3
  • 13