duanhong4274 2014-09-17 19:25
浏览 36
已采纳

只有在创建数据库php pdo时才运行insert一次查询

from config.php

<?php 
global $dbh;
$dbname = 'memberdb';


try {
        $dbh = new PDO("mysql:host=localhost", "root", "");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbname = "`".str_replace("`","``",$dbname)."`";
        $dbh->query("CREATE DATABASE IF NOT EXISTS $dbname");
        $dbh->query("use $dbname"); 
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql ="CREATE TABLE IF NOT EXISTS $member ( 
        mem_id int(40) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(40) NOT NULL, 
        password VARCHAR(40) NOT NULL);" ;
        $dbh->exec($sql); 
$stmt = $dbh->prepare("INSERT INTO member (username, password) VALUES (?,?)")or die($db->errorInfo());
    $stmt->bindValue(1,"admin1",PDO::PARAM_STR);
    $stmt->bindValue(2,"password1",PDO::PARAM_STR);
    $stmt->execute();
    $stmt->bindValue(1,"admin2",PDO::PARAM_STR);
    $stmt->bindValue(2,"password2",PDO::PARAM_STR);
    $stmt->execute();
    $stmt->bindValue(1,"admin3",PDO::PARAM_STR);
    $stmt->bindValue(2,"password3",PDO::PARAM_STR);
    $stmt->execute();
} catch(PDOException $e) {

}
?>

This is my function of new user when the user is registered using a registered button. How to make this kind of function run only one, when the database is created and only. I will need to put defined value for each input but i didnt not change it yet

UPDATE

The code i used is above my prob is still the same when i reload the index.php the query runs again making double entry..what i want is that when the database is create the query will run and when loaded the database is not created again so i want the query to not run again to avoid double entry.

展开全部

  • 写回答

1条回答 默认 最新

  • doutangguali32556 2014-09-17 22:48
    关注
    $stmt = $dbh->prepare("SELECT * FROM member") ;
        $stmt->execute();
        $count = $stmt -> rowCount();
        echo $count;
        if( $count == 00 ){
            $stmt = $dbh->prepare("INSERT INTO member (username, password) VALUES (?,?)")or die($db->errorInfo());
            $stmt->bindValue(1,"admin1",PDO::PARAM_STR);
            $stmt->bindValue(2,"password1",PDO::PARAM_STR);
            $stmt->execute();
            $stmt->bindValue(1,"admin2",PDO::PARAM_STR);
            $stmt->bindValue(2,"password2",PDO::PARAM_STR);
            $stmt->execute();
            $stmt->bindValue(1,"admin3",PDO::PARAM_STR);
            $stmt->bindValue(2,"password3",PDO::PARAM_STR);
            $stmt->execute();
        }
    

    i only have one more question why is it sometimess the echo for count is 3 and sometimes its 33 its like the query is run twice please clear this out...this worked but maybe just maybe there are incorrect logic here please feel free to edit to make it perfect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部