提交表单后,信息不会保存在mysql数据库中

时间:2016-03-18 06:40:12

标签: php html

这是从FORM中将信息保存到 mysql 数据库的代码 在 HTML 部分中,正在处理表单,即从用户检索所需数据 在 PHP 部分中,已处理存储数据 但问题是它不存储数据 我正在使用 XAMPP 服务器。

<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>

我不明白可能是什么问题。

谢谢大家。我遇到了问题 实际上我的数据库中的列序列与php代码中的查询不匹配。

2 个答案:

答案 0 :(得分:2)

我通过更改数据库中维护的查询中的变量序列来解决这个问题。

 $query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");

答案 1 :(得分:-1)

这是代码,它将适用于您的.. 我在你的mysql_query中传递了连接链接。并使用PHP_SELF作为当前页面。

<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>