-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>";
     }
}
?>
5
  • Seems to be <form method="post"> you have not mentioned action attribute. Action attribute help the form to send the values on desire location <form method="post" action="name of your .PHP file where you have to send values"> in your case leave this as blank. <form method="post" action=""> Commented Feb 1, 2019 at 8:28
  • @navjotsingh: the default action is the current page. You don't need an empty action value. Commented Feb 1, 2019 at 8:34
  • You can't "Connect php file to phpmyadmin". phpMyAdmin is a tool (like MySQL Workbench) used to manage MySQL databases. Commented Feb 1, 2019 at 8:36
  • Your query is vulnerable to SQL Injections consider using prepared statements Commented Feb 1, 2019 at 8:42
  • Any error message by the way? Commented Feb 1, 2019 at 8:43

1 Answer 1

-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>
Sign up to request clarification or add additional context in comments.

9 Comments

If it's still not works then there is problem in syntax in your SQL query.
So, you want to echo some HTML after closing the body tag?
The provided query is vulnerable to SQL Injections
Why are you connecting to your database twice
"$conn make an issue with if(isset($_POST['reg']))" lol, don't say things you don't know about.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.