2

This is the code for saving the information into mysql database from a FORM.
In the HTML section the form is being handled i.e. retrieving required data from user.
In the PHP section storing data has been handled.
But the problem is it doesn't store data.
I'm using XAMPP server.

<html>
<head>
    <title>signup</title>
    <link rel="stylesheet" href="css/insert.css" />
</head>
<body>
    <div class="maindiv">
    <!--HTML form -->
        <div class="form_div">
            <div class="title"><h2>Insert Data In Database Using PHP.</h2>      </div>

            <form action="signup.php" method="post">    <!-- method can be set POST for hiding values in URL-->
                <h2>Form</h2>

                <label>Name:</label>
                <br />
                <input class="input" type="text" name="name" value=""  />
                <br />
                <label>Email:</label><br />        
                <input class="input" type="text" name="mail"  value=""  />
                <br />

                <label>Phone:</label><br />        
                <input class="input" type="text" name="phone"  value=""  />
                <br />
                <label>Password:</label><br />        
                <input class="input" type="text" name="pass"  value=""  />
                <br />

                <label>Address:</label><br />
                <textarea rows="5" cols="25" name="add"></textarea>
                <br />

                <input class="submit" type="submit" name="submit"    value="Insert" />  

<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");

//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){

//Fetching variables of the form which travels in URL

$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";   
}

}
//Closing Connection with Server
mysql_close($connection);
?>                  
            </form>
        </div>

    </div>
</body>

I don't understand what can be the problem.

Thanks all. I got the problem.
Actually the sequence of the column in my database was not matching with the query in php code.

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
sat
  • 352
  • 2
  • 11
  • What is $query. If it is FALSE the query did not go well .. .. so make a if($query) to test – DTH Mar 18 '16 at 06:56
  • My Problem is SOLVED! thanks all !! – sat Mar 18 '16 at 07:35
  • Please make sure to post your own answer for others to use in the future – DTH Mar 18 '16 at 07:37
  • **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 **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 18 '16 at 08:58

2 Answers2

2

I have solved this by changing the variable sequence in the query which is maintained in the database.

 $query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");
sat
  • 352
  • 2
  • 11
-1

Here is the code and it will work for your.. I have passed connection link in your mysql_query. and used PHP_SELF for current page.

<html>
<head>
    <title>signup</title>
    <link rel="stylesheet" href="css/insert.css" />
</head>
<body>
    <div class="maindiv">
    <!--HTML form -->
        <div class="form_div">
            <div class="title"><h2>Insert Data In Database Using PHP.</h2>      </div>

            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">    <!-- method can be set POST for hiding values in URL-->
                <h2>Form</h2>

                <label>Name:</label>
                <br />
                <input class="input" type="text" name="name" value=""  />
                <br />
                <label>Email:</label><br />        
                <input class="input" type="text" name="mail"  value=""  />
                <br />

                <label>Phone:</label><br />        
                <input class="input" type="text" name="phone"  value=""  />
                <br />
                <label>Password:</label><br />        
                <input class="input" type="text" name="pass"  value=""  />
                <br />

                <label>Address:</label><br />
                <textarea rows="5" cols="25" name="add"></textarea>
                <br />

                <input class="submit" type="submit" name="submit"    value="Insert" />  

<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");

//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){

//Fetching variables of the form which travels in URL

$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')",$connection);
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";   
}

}
//Closing Connection with Server
mysql_close($connection);
?>                  
            </form>
        </div>

    </div>
</body>
  • Still not working. The query is not working . I have put an if before echo it is not true . – sat Mar 18 '16 at 07:10