doupo2157 2015-02-06 14:28
浏览 94

防止重复的SQL条目并在PHP中将数据插入SQLite

On submit/refresh the same predefined data in my array is added. How do i stop it being added more than once. I'd like to get rid of the data in my array if possible and just catch user input from the form which then is submitted to the array but i'm not sure how. Also how can i get my form to submit to my SQLite database, i think i need to add an INSERT INTO statement somewhere. Any help would be nice as i need this finished soon. :(

Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>Input</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
try {
    $dbh = new PDO('sqlite:mydb.sqlite3');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $dbh->exec("CREATE TABLE IF NOT EXISTS test (
             name VARCHAR(30),
             gender VARCHAR(30),
             age INTEGER)"
             );

    $data = array( //Want to remove this prewritten data and store user input instead.
            array('name' => 'Daniel', 'gender' => 'Male', 'age' => '21')
            );

    $insert = "INSERT INTO test (name, gender, age)
           VALUES (:name, :gender, :age)";
    $stmt = $dbh->prepare($insert);


foreach ($data as $m) {
    $name = $m['name'];
    $gender = $m['gender'];
    $age = $m['age'];
    $stmt->bindParam('name', $name);
    $stmt->bindParam('gender', $gender);
    $stmt->bindParam('age', $age);

    $stmt->execute();

}
    $result = $dbh->query('SELECT * FROM test');

    $dbh = null;

}
    catch(PDOException $e) {

    echo $e->getMessage();
}       

?> 

<div id="wrapper">

   <div class="banner1">
        <h2>Input</h2>
   </div>

<form id="form" method="post">
    Name:<br>
    <input type="text" name="name[0]"/> <br>
    Gender:<br>
    <input type="text" name="gender[0]"/> <br>
    Age:<br>
    <input type="number" name="age[0]" min="1" max="99"/> <br>

    <input id="submit" type="submit" value="Input">
    </form>

</div>

<div id="results">

    <div class="banner2">
            <h2>Results</h2>
    </div>

    <div class="data">
        <?php
            unset($_POST['submit']);
            $data=$_POST;

            foreach ($result as $row) {
            echo $row['name']  . " ";
            echo $row['gender'] . " " ;
            echo $row['age']  . "<br>" . " ";
            }

        ?> 
    </div>


</div>

</body>
</html>

Here is a screenshot so you can grasp an idea of the form and how i'd like it to work. The results section is just generated from the array so i can see what is being inputted, but i'd like the input form to send data to the SQLite database that i have created/connected to above. Thank you. Screenshot of Form:

  • 写回答

1条回答 默认 最新

  • donglang6656 2015-02-06 15:07
    关注

    First you have to test if Post exists And second, check if the user already exist

    if (isset($_POST)) {
        // check for existing user
        // Save
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序