无法使用模式内的PhP将数据插入数据库

时间:2015-03-09 13:42:15

标签: php twitter-bootstrap modal-dialog

这个模式一旦提交就会将所有数据都插入到我的数据库中,但是我很难搞清楚如何解决这个问题。正如你所看到的,当我点击“保存更改”按钮时,没有任何作用。我尝试检查我的数据库,看看是否已插入某些内容但无效但没有。有人能帮我吗。感谢。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog ">
    <div class="modal-content">
      <div class="modal-header" id="bg">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" >Sign Up</h4>
      </div>
      <div class="modal-body" id="bg">

      <?php
      require('config/db_conn.php');
      if(isset($_POST['save']))
      {
         $fname=$_POST['firstName'];
         $lname=$_POST['lastName'];
         $add= $_POST['address'];
         $cont=$_POST['contact'];
         $user=$_POST['userName'];
         $pass=$_POST['paasWord'];
         $type=$_POST['selectType'];

         mysql_query("INSERT INTO customer (c_fname,c_lname,c_address,c_con,c_user,c_pass,c_type) VALUES ('$fname','$lname','$add','$cont','$user','$pass','$type')") or die(mysql_error());

            header("location: login.php");

      }
      ?>

        <form method="post" action="">
            <div class="form-group">
                <label for="exampleInputFirstName">First Name</label>
                <input type="text" class="form-control" id="exampleInputFirstName" placeholder="Username" name="firstName">
            </div>
            <div class="form-group">
                <label for="exampleInputLastName">Last Name</label>
                <input type="text" class="form-control" id="exampleInputLastName" placeholder="Username" name="lastName">
            </div>
            <div class="form-group">
                <label for="exampleInputAddress">Address</label>
                <textarea class="form-control" id="exampleInputAddress" placeholder="Address" name="address"></textarea>
            </div>
            <div class="form-group">
                <label for="exampleInputContact">Contact</label>
                <input type="text" class="form-control" id="exampleInputContact" placeholder="Contact" name="contact">
            </div>
            <div class="form-group">
                <label for="exampleInputUsername">Username</label>
                <input type="text" class="form-control" id="exampleInputUsername" placeholder="Username" name="userName">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword">Password</label>
                <input type="password" class="form-control" id="exampleInputPassword" placeholder="Password" name="passWord">
            </div>
            <div class="form-group">
                <label for="examoleInputType">Type</label>
                    <select id="target" class="size form-control" name="selectType">
                        <option value="default" selected="selected">--SELECT--</option>
                        <option value="User">User</option>
                        <option value="Admin">Admin</option>    
                    </select>
            </div>

            <div class="well modal-footer" id="bg">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" name="save">Save changes</button>
        </div>
        </form>
      </div>

    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
PS:我把我的模态中的所有php代码和我的数据库连接的url一起放了。模态位于此代码的结束标记html下方。顺便说一下,我正在使用getbootstrap.com的bootstrap。

<?php
session_start();

if(isset($_SESSION['username'])!="" ) {
    header("location: home.php");
} 

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Login</title>


<link rel = "stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>

<?php
require('config/db_conn.php');

?>

</head>
<style type="text/css">
body
{
background-color: #6BBEE4;

}

.createwrapper
{
width: 500px;
height: 300px;
margin: 0 auto;
margin-top: 118px;
}

a:hover
{
text-decoration: none
}

.size
{
width: 150px;
}

#bg
{
background-color: #6BBEE4;  

}

</style>
<body>


<?php
if(isset($_POST['submit']))
{
    if($_POST['type'] == "User")
    {
    $user = $_POST['username'];
    $pass = $_POST['password'];
    $type = $_POST['type'];


    $query = mysql_query("SELECT * FROM customer where c_user = '$user' && c_pass = '$pass' && c_type = '$type'");

    $data = mysql_fetch_array($query);

    $_SESSION['username'] =  $data['c_user'];
    $_SESSION['password'] =  $data['c_pass'];
    $_SESSION['name'] = $data['c_fname'];

    header("location: home.php");
    }

    elseif($_POST['type'] == "Admin")
    {
        echo "Were still working!!";
    }

    else
    {
    header("location: login.php");
    }
}
?>

<script type="text/javascript">

$(document).ready(function() {
    $('#signup').click (function() {
        $('#myModal').modal(show);

    });


})


</script>

<div class = "well createwrapper">
<form method="post" action="">
  <div class="form-group">
    <label for="exampleInputUsername">Username</label>
    <input type="text" class="form-control" id="exampleInputUsername" placeholder="Username" name="username">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password">
  </div>
    <div class="form-group">
    <label for="examoleInputType">Type</label>

    <select id="target" class="size form-control" name="type">
    <option value="default" selected="selected">--SELECT--</option>
    <option value="User">User</option>
    <option value="Admin">Admin</option>    
    </select>
  </div>
  <button class="btn btn-primary" name="submit">Submit</button>
  <div class="pull-right">
  <font color="428BCA"> Don't have an account? </font>
  <button class="btn btn-primary " name="signup" data-toggle="modal" data-target="#myModal" id="signup">Sign Up</button>
  </div>
</form>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

我认为有些事情需要在你脑海中清理。

首先要记住一个模型对话框不是一个单独的页面,它只是页面上的一个div,它巧妙地定位为看起来很性感。

此外,我不认为模型对话框中的表单实际上会在对话框中提交表单,即页面作为一个按钮,而不是<input type="submit"...>,除非您已经编写了一些javascript来注意按钮单击并提交来自javascript声明的表单。

我还建议您在调用时将PHP代码从模拟中的移动到,并将其移动到控制此页面的脚本的顶部。

请记住,当您将表单提交回服务器进行处理时,它会每次从第一行启动脚本,因此您应该在重建页面之前处理任何操作,这样您就知道要将哪个页面返回到浏览器,或者在页面上放置什么内容,即一切都好,或者有错误。

答案 1 :(得分:0)

首先,你永远不会提交你的表格,它只是按钮。变化:

<button type="button" class="btn btn-primary" name="save">Save changes</button>

<input type="submit" class="btn btn-primary" name="save" value="Save changes" />

其次,header("location: login.php");header("location: home.php");都不会成功,因为您在调用这些函数之前将html输出到浏览器。

答案 2 :(得分:0)

这是有效的,我正在使用PDO。

编辑:发现问题会在一秒内更新

更新:现在正在工作。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog ">
    <div class="modal-content">
      <div class="modal-header" id="bg">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" >Sign Up</h4>
      </div>
      <div class="modal-body" id="bg">


<form method="post" action="insert.php"> // change to yours
            <div class="form-group">
                <label for="exampleInputFirstName">First Name</label>
                <input type="text" class="form-control" id="exampleInputFirstName" placeholder="Username" name="firstName">
            </div>
            <div class="form-group">
                <label for="exampleInputLastName">Last Name</label>
                <input type="text" class="form-control" id="exampleInputLastName" placeholder="Username" name="lastName">
            </div>
            <div class="form-group">
                <label for="exampleInputAddress">Address</label>
                <input type="text" class="form-control" id="exampleInputContact" placeholder="address" name="address">
            </div>
            <div class="form-group">
                <label for="exampleInputContact">Contact</label>
                <input type="text" class="form-control" id="exampleInputContact" placeholder="Contact" name="contact">
            </div>
            <div class="form-group">
                <label for="exampleInputUsername">Username</label>
                <input type="text" class="form-control" id="exampleInputUsername" placeholder="Username" name="userName">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword">Password</label>
                <input type="password" class="form-control" id="exampleInputPassword" placeholder="Password" name="passWord">
            </div>
            <div class="form-group">
                <label for="examoleInputType">Type</label>
                    <select id="target" class="size form-control" name="selectType">
                        <option value="default" selected="selected">--SELECT--</option>
                        <option value="User">User</option>
                        <option value="Admin">Admin</option>    
                    </select>
            </div>

            <div class="well modal-footer" id="bg">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <input type="submit" class="btn btn-primary" name="save" value="Save changes" />
        </div>
        </form>
      </div>

    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

insert.php:

<?php
    require_once 'db_config.php'; 


    try {
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $sth = $db->prepare("INSERT INTO test (c_fname, c_lname, c_address, c_con, c_user, c_pass, c_type)
            VALUES
            (?,?,?,?,?,?,?)");
            $sth->execute(array($_POST['firstName'],$_POST['lastName'],$_POST['address'],$_POST['contact'],$_POST['userName'],$_POST['passWord'],$_POST['selectType']));
        } catch (PDOException $pe) {
            die("Could not connect to the database $db_name :" . $pe->getMessage());
        }
?>

db_config.php:

<?php 
    $db_host = "localhost";
    $db_name = "db";
    $db_user = "root";
    $db_pass = "root";
    $db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>