使用PHP提交到数据库

时间:2018-09-01 09:23:50

标签: php html forms html-form

我找到了第一部分并试图对其进行扩展,但是代码无法正常工作。我有一个索引部分和一个创建部分,但是当我在代码中按“提交”时,没有任何提交。两者看起来一样,不知道为什么这行不通。我使用xampp设置了一个数据库,它与原始代码完美配合

在原始代码中,当我按“提交”时,数据将存储在数据库中,并且用户将被带回到索引页,在我的代码中,表格将重置,并且数据不会存储。我没有收到错误消息。

这是原始代码:

索引部分;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
            <div class="row">
                <h3>PHP CRUD Grid</h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                </p>

                <table class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Name</th>
                          <th>Email Address</th>
                          <th>Mobile Number</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
                       include 'database.php';
                       $pdo = Database::connect();
                       $sql = 'SELECT * FROM customers ORDER BY id DESC';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['name'] . '</td>';
                                echo '<td>'. $row['email'] . '</td>';
                                echo '<td>'. $row['mobile'] . '</td>';
                                echo '<td width=250>';
                                echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                                echo ' ';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                                echo ' ';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';
                       }
                       Database::disconnect();
                      ?>
                      </tbody>
                </table>
        </div>
    </div> <!-- /container -->
  </body>
</html>

创建部分:

<?php

    require 'database.php';

    if ( !empty($_POST)) {
        // keep track validation errors
       // $nameError = null;
        $emailError = null;
        $mobileError = null;

        // keep track post values
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];

        // validate input
        $valid = true;
        /*if (empty($name)) {
            $nameError = 'Please enter Name';
            $valid = false;
        }*/

        if (empty($email)) {
            $emailError = 'Please enter Email Address';
            $valid = false;
        } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
            $emailError = 'Please enter a valid Email Address';
            $valid = false;
        }

        if (empty($mobile)) {
            $mobileError = 'Please enter Mobile Number';
            $valid = false;
        }

        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO customers (name,email,mobile) values(?, ?, ?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($name,$email,$mobile));
            Database::disconnect();
            header("Location: index.php");
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

                <div class="span10 offset1">
                    <div class="row">
                        <h3>Create a Customer</h3>
                    </div>

                    <form class="form-horizontal" action="create.php" method="post">
                      <div class="control-group <?php echo !empty($nameError)?'error':'';?>">
                        <label class="control-label">Name</label>
                        <div class="controls">
                            <input name="name" type="text"  placeholder="Name" value="<?php echo !empty($name)?$name:'';?>">
                            <?php if (!empty($nameError)): ?>
                                <span class="help-inline"><?php echo $nameError;?></span>
                            <?php endif; ?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($emailError)?'error':'';?>">
                        <label class="control-label">Email Address</label>
                        <div class="controls">
                            <input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email)?$email:'';?>">
                            <?php if (!empty($emailError)): ?>
                                <span class="help-inline"><?php echo $emailError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($mobileError)?'error':'';?>">
                        <label class="control-label">Mobile Number</label>
                        <div class="controls">
                            <input name="mobile" type="text"  placeholder="Mobile Number" value="<?php echo !empty($mobile)?$mobile:'';?>">
                            <?php if (!empty($mobileError)): ?>
                                <span class="help-inline"><?php echo $mobileError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Create</button>
                          <a class="btn" href="index.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

这就是我做的:

索引部分;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
            <div class="row">
                <h3>PHP CRUD Grid</h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                </p>

                <table class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>titel</th>
                          <th>prog</th>
                          <th>tekst</th>
                          <th>alt1</th>
                             <th>res1</th>
                          <th>alt2</th>
                          <th>res2</th>
                          <th>alt3</th>
                               <th>res3</th>
                          <th>alt4</th>
                          <th>res4</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
                       include 'database.php';
                       $pdo = Database::connect();
                       $sql = 'SELECT * FROM sider ORDER BY id DESC';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['titel'] . '</td>';
                                echo '<td>'. $row['progresjon'] . '</td>';
                                echo '<td>'. $row['tekst'] . '</td>';
                                      echo '<td>'. $row['alt1'] . '</td>';
                                echo '<td>'. $row['res1'] . '</td>';
                                  echo '<td>'. $row['alt2'] . '</td>';
                                echo '<td>'. $row['res2'] . '</td>';
                                        echo '<td>'. $row['alt3'] . '</td>';
                                echo '<td>'. $row['res3'] . '</td>';
                                  echo '<td>'. $row['alt4'] . '</td>';
                                echo '<td>'. $row['res4'] . '</td>';
                                echo '<td width=250>';
                                echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                                echo ' ';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                                echo ' ';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';
                       }
                       Database::disconnect();
                      ?>
                      </tbody>
                </table>
        </div>
    </div> <!-- /container -->
  </body>
</html>

创建零件:

<?php

    require 'database.php';

    if ( !empty($_POST)) {
        // keep track validation errors
        $titelError = null;
        $progresjonError = null;
        $tekstError = null;

        // keep track post values
        $titel = $_POST['titel'];
        $progresjon = $_POST['progresjon'];
        $tekst = $_POST['tekst'];
        $alt1 = $_POST['alt1'];
        $res1 = $_POST['res1'];
        $alt2 = $_POST['alt2'];
        $res2 = $_POST['res2'];
        $alt3 = $_POST['alt3'];
        $res3 = $_POST['res3'];
        $alt4 = $_POST['alt4'];
        $res4 = $_POST['res4'];



        // validate input
        $valid = true;
        if (empty($titel)) {
            $titelError = 'Please enter titel';
            $valid = false;
        }

        if (empty($progresjon)) {
            $progresjonError = 'Please enter progresjon';
            $valid = false;
        }

        if (empty($tekst)) {
            $tekstError = 'Please enter tekst';
            $valid = false;
        }

        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO sider (titel,progresjon,tekst,alt1,res1,alt2,res2,alt3,res3,alt4,res4) values (?,?,?,?,?,?,?,?,?,?,?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($titel,$progresjon,$tekst,$alt1,$res1,$alt2,$res2,$alt3,$res3,$alt4,$res4));
            Database::disconnect();
            header("Location: index.php");
        }
    }
?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="utf-8">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="js/bootstrap.min.js"></script>
    </head>

    <body>
        <div class="container">

            <div class="span10 offset1">
                <div class="row">
                    <h3>Create a Customer</h3>
                </div>

                <form class="form-horizontal" action="create.php" method="post">
                    <div class="control-group <?php echo !empty($titelError)?'error':'';?>">
                        <label class="control-label">titel</label>
                        <div class="controls">
                            <input titel="titel" type="text" placeholder="titel" value="<?php echo !empty($titel)?$titel:'';?>">
                            <?php if (!empty($titelError)): ?>
                                <span class="help-inline"><?php echo $titelError;?></span>
                                <?php endif; ?>
                        </div>
                    </div>
                    <div class="control-group <?php echo !empty($progError)?'error':'';?>">
                        <label class="control-label">prog Address</label>
                        <div class="controls">
                            <input titel="progresjon" type="text" placeholder="progresjon" value="<?php echo !empty($progresjon)?$progresjon:'';?>">
                            <?php if (!empty($progresjonError)): ?>
                                <span class="help-inline"><?php echo $progresjonError;?></span>
                                <?php endif;?>
                        </div>
                    </div>
                    <div class="control-group <?php echo !empty($tekstError)?'error':'';?>">
                        <label class="control-label">tekst </label>
                        <div class="controls">
                            <input titel="tekst" type="text" placeholder="tekst Number" value="<?php echo !empty($tekst)?$tekst:'';?>">
                            <?php if (!empty($tekstError)): ?>
                                <span class="help-inline"><?php echo $tekstError;?></span>
                                <?php endif;?>
                        </div>
                    </div>

                        <div class="control-group">
                            <label class="control-label">alt1</label>
                            <div class="controls">
                                <input titel="alt1" type="text" placeholder="Alt1" value="<?php echo !empty($alt1)?$alt1:'';?>" </div>


                            </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">res1</label>
                                <div class="controls">
                                    <input titel="res1" type="text" placeholder="res1" value="<?php echo !empty($res1)?$res1:'';?>" </div>


                                </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">alt2</label>
                                    <div class="controls">
                                        <input titel="al1" type="text" placeholder="Alt2" value="<?php echo !empty($alt2)?$alt2:'';?>" </div>


                                    </div>
                                </div>
                                    <div class="control-group">
                                        <label class="control-label">res2</label>
                                        <div class="controls">
                                            <input titel="res2" type="text" placeholder="res2" value="<?php echo !empty($res2)?$res2:'';?>" </div>


                                        </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label">alt3</label>
                                            <div class="controls">
                                                <input titel="al3" type="text" placeholder="Alt3" value="<?php echo !empty($alt3)?$alt3:'';?>" </div>


                                            </div>
                                            </div>
                                            <div class="control-group">
                                                <label class="control-label">res3</label>
                                                <div class="controls">
                                                    <input titel="res3" type="text" placeholder="res3" value="<?php echo !empty($res3)?$res3:'';?>" </div>


                                                </div>
                                                </div>
                                                <div class="control-group">
                                                    <label class="control-label">alt4</label>
                                                    <div class="controls">
                                                        <input titel="alt4" type="text" placeholder="Alt4" value="<?php echo !empty($alt4)?$alt4:'';?>" </div>


                                                    </div>
                                                    </div>

                                                    <div class="control-group">
                                                        <label class="control-label">res4</label>
                                                        <div class="controls">
                                                            <input titel="res4" type="text" placeholder="res4" value="<?php echo !empty($res4)?$res4:'';?>" </div>

                                                             </div>
                                                            </div>
                                                                                                                <div class="form-actions">
                                                            <button type="submit" class="btn btn-success">Create</button>
                                                            <a class="btn" href="index.php">Back</a>
                                                        </div>

                </form>
            </div>

                </div>
                <!-- /container -->
    </body>

    </html>

1 个答案:

答案 0 :(得分:1)

如果仔细查看表单和示例之间的差异,您会发现<input字段都不具有“ name”属性。如果您希望服务器接收该值,则在HTML表单输入字段中是必需的-在发送HTTP请求时,它将用作参数名称,然后在PHP中,该名称将与匹配的值一起添加到$ _POST数组中相关。

例如,您需要更改

<input titel="titel" type="text" placeholder="titel" value="<?php echo !empty($titel)?$titel:'';?>">

<input name="titel" titel="titel" type="text" placeholder="titel" value="<?php echo !empty($titel)?$titel:'';?>">

,并且对于所有其他字段也是如此,请确保您在“ name”属性中输入的内容与您的PHP代码所期望的相符(例如,在本示例中为$_POST["titel"])。

如果您想做进一步的研究,大多数HTML表单教程都会解释这个概念。