“插入重复”两张票以执行脚本

时间:2014-02-26 22:54:51

标签: php mysql

再次,我的英语道歉,我用谷歌翻译写这个。 我正在创建一个小脚本来管理用户。 我碰巧有一些与主表相关的小表是" usuarios"。

重点是,在运行我的脚本时,我插入了两次相同的用户。 当你"回声"在查询函数中不重复,很奇怪因为在phpmyadmin中没有发生,只能用我的脚本。

我留下我的代码:

function ar($a, $d = true){ // function test only
    if($d){
        echo '<pre>';
        var_dump($a);
        echo '</pre>';  
    }else{
        echo '<pre>';
        print_r($a);    
        echo '</pre>';  
    }
}

function SecureData($data){//cleaning up the data that will be inserted
    global $conn;
    if(is_array($data)){
            foreach($data as $key=>$val){
                    if(!is_array($data[$key])){
                            $data[$key] = mysqli_real_escape_string($conn, $data[$key]);
                    }else{
                        $data[$key] = SecureData($data[$key]);
                    }
            }
    }else{
            $data = mysqli_real_escape_string($conn, $data);
    }
    return $data;
}
//---->
function comillas($data, $s = "'"){// Wrap the string with a character
    if(is_array($data)){
            foreach($data as $key=>$val){
                    if(!is_array($data[$key])){
                            $data[$key] = $s.$data[$key].$s;
                    }else{
                        $data[$key] = comillas($data[$key]);
                    }
            }
    }else{
            $data = $s.$data.$s;
    }
    return $data;
}
//---->
function error_mysql($conn = ''){// Evalua y returna un error en la consulta si la hay
    if( mysqli_errno($conn) ){
     return "<pre>Errror en la consulta \n \t".mysqli_error($conn)."</pre>";
    }
    return false;
}
//---->
function updateTable($data = null){ // insert, update or delete a row in a certain table
    global $conn;
    $data = SecureData( $data );
    if( $data["action"] == "new" && !empty( $data["table"] ) ){

        $rows = implode(", ", comillas(array_keys( $data["values"] ), "`") );
        $values = implode(", ", comillas($data["values"]) );
        $s = "INSERT INTO `". $data["table"]."`(". $rows .") VALUES(". $values .");";
//      echo $s.'<br>'; return true;
        $q = mysqli_query($conn, $s);
        if( !$q ){
            echo error_mysql( $conn );
            return false;
        } 
        return mysqli_insert_id($conn);

    }else if( $data["action"] == "delete" && !empty($data["id"]) ){

        return mysqli_query($conn, "DELETE FROM `". $data["table"] ."` WHERE id='". $data["id"] ."'");

    }else if( $data["action"] == "update" && !empty($data["table"]) && !empty($data["values"]) ){

        foreach(array_keys( $data["values"] ) as $v){
            $t[] = "`".$v."` = '" . $data["values"][$v] . "'";      
        }
        $t = implode(", ", $t );
        $s = "UPDATE `". $data["table"] ."` SET ". $t ." WHERE id = '". $data["id"] ."'";
        echo $s; return false;
        return mysqli_query($conn, $s);

    }else{
        return false;   
    }
}
// -->
function register_user($data = null){ // logs the user
    global $conn;
    if( !is_array( $data ) || empty( $data ) ){ return false; }
    $data = SecureData($data);
    $privacidad = updateTable( array("table"=> "privacidad", "action" => "new", "values" => array("busqueda" =>"publico", "visible" => "publico") ) );
    if( !$privacidad ){ return false; }
    $ubicacion = updateTable( array("table"=> "ubicacion", "action" => "new", "values" => array("pais" =>0, "estado" => 0, "municipio" => 0) ) );
    if( !$ubicacion){ 
        updateTable( array("table"=> "privacidad", "id" => $privacidad, "action" => "delete" ) );
    }
    $tour = updateTable( array("table"=> "tour", "action" => "new", "values" => array("foto" =>0, "verificado" => 0, "emailverificado" => 0) ) );
    if( !$tour){ 
        updateTable( array("table"=> "privacidad", "id" => $privacidad, "action" => "delete" ) ); 
        updateTable( array("table"=> "ubicacion", "id" => $ubicacion, "action" => "delete" ) ); 
        return false; 
    }

    $premium = updateTable( 
        array("table"=> "premium", "action" => "new", "values" => array("mensajes" =>0, "fotografias" => 0, "eventos" => 0, "reputacion" =>0, "busqueda" =>0, "visitas" => 0) ) );
    if( !$premium){ 
        updateTable( array("table"=> "privacidad", "id" => $privacidad, "action" => "delete" ) ); 
        updateTable( array("table"=> "ubicacion", "id" => $ubicacion, "action" => "delete" ) ); 
        updateTable( array("table"=> "tour", "id" => $tour, "action" => "delete" ) ); 
        return false; 
    }
    $new = updateTable(array(
                            "action" => "new",
                            "table" => "usuarios",
                            "values" => array(
                                            "nick" => $data["nick"],
                                            "email"  =>  $data["email"] ,
                                            "password"  =>  $data["password"] ,
                                            "usertype"  =>  $data["usertype"] ,
                                            "avatar"  =>  0 ,
                                            "status"  =>  1 ,
                                            "online"  => time() ,
                                            "privacidad"  =>  $privacidad ,
                                            "ubicacion"  =>  $ubicacion ,
                                            "tour"  =>  $tour ,
                                            "premium" =>  $premium ,
                                            "picpoints"  => 0
                                        )
                            )
                        );
        if( !$new ){
            echo $error;
            updateTable( array("table"=> "privacidad", "id" => $privacidad, "action" => "delete" ) ); 
            updateTable( array("table"=> "ubicacion", "id" => $ubicacion, "action" => "delete" ) ); 
            updateTable( array("table"=> "tour", "id" => $tour, "action" => "delete" ) ); 
            updateTable( array("table"=> "premium", "id" => $premium, "action" => "delete" ) );
            return false;
        } 
        return mysqli_insert_id($conn);

}
//---->
$conn = mysqli_connect(SDB, UDB, PDB, DB);// Connect to the database
if (!$conn) {
    printf("Conexi&oacute;n fallida: %s\n", mysqli_connect_error());
    exit();
}
mysqli_set_charset($conn, "utf8");// We define that we use utf8 as encoding
$register = register_user(array('nick'=>'admin',  'email'=>'king.jorhel@gmail.com',  'password'=>'asdasd',  'usertype'=>'user'));
ar( $register );
mysqli_close($conn);

这里是我使用的表的结构:

    CREATE TABLE IF NOT EXISTS `premium` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `credito` float(6,2) DEFAULT '0.00',
  `mensajes` int(11) DEFAULT NULL,
  `fotografias` int(11) DEFAULT NULL,
  `eventos` int(11) DEFAULT NULL,
  `reputacion` int(11) DEFAULT NULL,
  `busqueda` int(11) DEFAULT NULL,
  `visitas` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `privacidad`
--

CREATE TABLE IF NOT EXISTS `privacidad` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `busqueda` varchar(45) DEFAULT NULL,
  `visible` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `tour`
--

CREATE TABLE IF NOT EXISTS `tour` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `foto` tinyint(1) DEFAULT NULL,
  `verificado` tinyint(1) DEFAULT NULL,
  `emailverificado` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `ubicacion`
--

CREATE TABLE IF NOT EXISTS `ubicacion` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pais` int(11) DEFAULT NULL,
  `estado` int(11) DEFAULT NULL,
  `municipio` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `usuarios`
--

CREATE TABLE IF NOT EXISTS `usuarios` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `nick` varchar(100) DEFAULT NULL,
  `email` varchar(320) DEFAULT NULL,
  `password` varchar(300) DEFAULT NULL,
  `usertype` varchar(45) DEFAULT NULL,
  `status` tinyint(1) DEFAULT '1',
  `online` int(20) DEFAULT NULL,
  `ubicacion` int(11) NOT NULL,
  `tour` bigint(20) NOT NULL,
  `privacidad` bigint(20) NOT NULL,
  `premium` int(11) NOT NULL,
  `avatar` bigint(20) NOT NULL,
  `picpoints` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

1 个答案:

答案 0 :(得分:0)

解决方案:在mysql中,将“email”字段添加为“unique”属性,这样只插入一次