从表单提交插入到数据库

时间:2017-01-11 07:46:03

标签: php mysql database

我的HTML代码:

<div class="form-group">
                                <label for="name">Total Duration in Second</label>
                                <input type="text" class="form-control" id="duration" name="total_duration">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="name">Description Duration</label>
                                <input type="text" class="form-control" id="duration" name="desc_duration">
                            </div>

php:

<?php

                    include('lib/conn_sqli.php');
                    include ('lib/conn.php');
                    $total_duration = $_POST['total_duration'];
                    $desc_duration = $_POST['desc_duration'];



                    $sql = "INSERT INTO vote.duration (id_duration, total_duration, desc_duration) VALUES (null, $total_duration, $desc_duration)";
                    mysql_query($sql);
                    if ($sql){
                        echo ("succeed");
                    }
                    ?>

conn_sqli.php:

<?php
    // connect database -------------------------------------------->
    $db = new mysqli ( "localhost" , "abc" , "123" , "vote" );
    echo $db->connect_errno?'Connection failed : '.$db->connect_error:'';
    //<--------------------------------------------------------------
    ?>

conn.php:

<?php
$db_host    = "localhost";
$db_user    = "abc";
$db_pass    = "123";
$db_name    = "vote";
$konek  = mysql_connect($db_host,$db_user,$db_pass,$db_name) or die ("Gagal Koneksi ke Server");
mysql_select_db($db_name, $konek) or die ("Gagal Mengaktifkan Database".mysql_error());
?>

代码根本不起作用。 php将回显&#34; Succeed&#34;,但没有插入数据。我知道这很简单,但我似乎无法弄明白。感谢

2 个答案:

答案 0 :(得分:0)

说明:

  • 请勿合并deprecated mysql_*mysqli_* API
  • 在将原始数据绑定到查询之前至少使用 $_productCollection= Mage::getResourceModel('reports/product_collection'); $cache = Mage::app()->getCache(); //Let's get cache instance $cache->setLifetime(86400); //Here we set collection cache lifetime $_productCollection->initCache( $cache, 'Bestsellers_', //this is just custom prefix array('collections') ); } 函数
  • 使用单个勾号(*_real_escape_string())将变量绑定到您的查询,更好的是,使用prepared statement
  • 执行error reporting

以下是预编译语句中代码的简单示例(假设id_duration列是您的主键并且是自动增量):

'

答案 1 :(得分:0)

你完全是错误的。 HTML表单:

    <form method="post" action="insert.php">
       <label for="total_duration">Total Duration in Second</label>
       <input type="text" class="form-control" id="total_duration" name="total_duration">
        <label for="desc_duration">Description Duration</label>
        <input type="text" class="form-control" id="desc_duration" name="desc_duration">
        <input type="submit" name="submit" />
    </form>

插入数据库,让我说一下insert.php:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$total_duration = $_POST['total_duration'];
$desc_duration = $_POST['desc_duration'];

$sql = "INSERT INTO vote.duration(total_duration, desc_duration) VALUES ($total_duration, $desc_duration)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

为什么要设置NULL id_duration?如果您不希望每次都AutoIncrement值,请将其设为insert