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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题