duanhong4274 2014-09-18 03: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-18 06: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.

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

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)