INSERT INTO wordt niet uitgevoerd

Status
Niet open voor verdere reacties.

jensiboyke

Gebruiker
Lid geworden
4 jul 2010
Berichten
43
Na aantal keer uitstekend geholpen te zijn door velen hier, kom ik nog eens af met nog een vraag.
Na meerdere malen uit te testen en doen ben ik tegengekomen dat m'n INSERT INTO query niet proper werkt. Na aantal uren proberen en bekijken vind ik de fout niet terug.

Dit is mijn snippet:
PHP:
$query = mysqli_query($con, "INSERT INTO matchs (ip,server_id,team_a_flag,team_a_name,team_b_flag,team_b_name,status,score_a,score_b,max_round,rules,overtime_startmoney,overtime_max_round,config_full_score,config_ot,config_streamer,config_knife_round,enable,map_selection_mode,created_at,updated_at, config_authkey) 
		VALUES ($server_ip, $server_id, NULL, $team_a, NULL, $team_b, 1, 0, 0, 15, 'esl5on5',16000, 0, 0, 0, 0, 0, 0, 'normal', NOW(), NOW(), $config_authkey)");

Indien volledige pagecode wilt bekeken worden gelieve dit in reactie achter te laten.

Met vriendelijke groet
 
Ja, maar in die file waar uw INSERT query is, moet u ergens voor dat query volgende plaatsen : " global $con; "
 
Ik ben tegenstander van " global $con; "
bekijk onderstaande iets complexer
PHP:
<?php
/**
 * @author Lieven Roegiers
 */
	abstract class Model{
	   protected $DB;
	   public function __construct() {
	       //$conn = new \DBmyConn();
           $this->DB = \DBConn::GET();
	   }
       protected function existTableLabel($label){
            return $this->existTable(\DBConn::getTableNameFor($label));
       }
       private function existTable($tablename){
            try{
                $database = \DBConn::GET();
                $res = $database->query("SHOW TABLES LIKE '".$tablename."'");
                $vals = $res->fetch();
                return($vals[0]==$tablename);
            }catch (PDOException $e) {
               die($e);
            }
       }
       protected function transaction($sqlarray){
            try{
               $commit = true;
               $this->DB->beginTransaction();
               foreach($sqlarray as $sql=>$param){
                    try {
                        if(!\ValidateAs::query($sql)||!($dbStmt = $this->DB->prepare($sql))||!$dbStmt->execute($param)){
                            $commit = false;
                        }
                    } catch (\PDOException $ex) {
                        print $ex;
                        $commit = false;
                    }
               }
               if($commit){
                    return  $this->DB->commit();;
               }else{
                    $this->DB->rollback();
                    return false;
               }
            }catch (PDOException $e) {
               @$this->DB->rollback();
               return false;
            }
       }
       public function killcon(){
             @$this->DB = null;
       }
}
class DBConn{
    static private $_obj = null;
    private $_dbConn = null;
    static protected $_DBprefix = '';
    static protected $_CharSet = "utf8";
    static public $_Collate = "utf8_unicode_ci";
    private $config ;
    /**
     * DBConn::__construct()
     * @return void
     */
    public function __construct() {
        $this->config = ".\uwdatabasegegevens.plusuwextentiebestphp";//.php dient voor meer beveiliging
        if(file_exists($this->config)&& is_readable($this->config)){
            try{
                if($config = parse_ini_file($this->config))
               //@TODO overwrite
                 $this->connect($config['HOST'],$config['DATABASE'],$config['USER'],$config['PASSWORD']);
            }catch(Exception $ex){
                die('OEPS... DATABASEVERBINDING GEFAALD');
            }
        }else{
            $this->install();
            //header('Location: http://'.$_SERVER['SELF'].$url);
            die('FATALE FOUT... '.' database config');
        }
    }
    /**
     * DBConn::GET()
     * @return connection
     */
    static public function GET(){
        if(!(self::$_obj instanceof self)){
            self::$_obj=new self();
        }
        return self::$_obj->_dbConn;
    }
    /**
     * DBConn::connect()
     * @param mixed $host
     * @param mixed $dbname
     * @param mixed $usr
     * @param mixed $pwd
     * @return return connection
     */
    private function connect($host,$dbname,$usr,$pwd){
       $conn = 'mysql:host='.$host.';dbname='.$dbname;
        //odbc_connect($conn,$usr,$pwd) or die(odbc_errormsg() );
        try{
            $this->_dbConn = new PDO($conn, $usr, $pwd,self::getoptions());
            return  $this->_dbConn;
        }catch(Exception $ex){
            return false;
        }
    }
     /**
      * DBConn::getoptions()
      * @return array()
      */
     private function getoptions(){
        return array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '".self::$_CharSet."'",PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    }
    /**
     * DBConn::PREFIX()
     * @return
     */
    static public function PREFIX(){
        return self::$_DBprefix;
    }
    /**
     * DBConn::getTableNameFor()
     * @param mixed $key
     * @return
     */
    static public function getTableNameFor($key){
        return self::PREFIX().$key;
    }
    /**
     * DBConn::getCHARSET()
     * @return
     */
    static public function getCHARSET(){
        return self::$_CharSet;
    }
    /**
     * DBConn::getCOLLATE()
     * @return
     */
    static public function getCOLLATE(){
        return self::$_Collate;
    }
    /**
     * DBConn::install()
     * @return
     */
    public function install(){
        $config['HOST']=(isset( $_POST['HOST'] )) ? addslashes($_POST['HOST'] ) : '' ;
        $config['DATABASE']=(isset($_POST['DATABASE'] )) ? addslashes($_POST['DATABASE'] ) : '' ;
        $config['USER']=(isset($_POST['USER'] )) ? addslashes($_POST['USER'] ) : '' ;
        $config['PASSWORD']=(isset($_POST['PASSWORD'] )) ? addslashes($_POST['PASSWORD'] ) : '' ;
        if(isset($_POST['save'])){
            if($this->connect($config['HOST'],$config['DATABASE'],$config['USER'],$config['PASSWORD'])){
                $this->write_file($config);
                return;
            }
        }
        print "database not exist please place her your installation form";
    }
    /**
     * DBConn::write_file()
     * @param mixed $assoc_arr
     * @return
     */
    function write_file($assoc_arr){
        $content = ""; 
        foreach ($assoc_arr as $key=>$elem){
            $content .= ''.$key.' = "'.$elem.'"'." \n";
        }
        print $content;
        if (!$handle = fopen($this->config, 'w')) { 
            return false;
        }
        $success = fwrite($handle, $content);
        fclose($handle);
        return $success; 
    }
    /**
     * DBConn::__destruct()
     * @return void
     */
    public function __destruct(){
        $this->_dbConn = null;
    }
    /**
     * DBConn::__processlist()
     * @param bool $kill
     * @return
     */
    public function __processlist($kill = false){
        $query = 'SHOW PROCESSLIST -- ' . uniqid('pdo_mysql_close ', 1);
        $list  = $connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
        foreach ($list as $thread) {
            if ($thread['Info'] === $query) {
                return ($kill == true)?$connection->query('KILL ' . $thread['Id']):false;
            }
        }
        return false;
    }
} 
?>
 
Mooi stukje code, maar wat moet de TS hiermee? Je dumpt hier de code (en veel spul dat niet eens nodig is) en hij moet het maar uitzoeken? Zet er dan op z'n minst een implementatie bij :rolleyes:

Laten we eerst eens beginnen met debug-regels toe te voegen zodat we kunnen achterhalen waar het mis gaat :)

PHP:
$query = mysqli_query($con, "INSERT INTO matchs (ip,server_id,team_a_flag,team_a_name,team_b_flag,team_b_name,status,score_a,score_b,max_round,rules,overtime_startmoney,overtime_max_round,config_full_score,config_ot,config_streamer,config_knife_round,enable,map_selection_mode,created_at,updated_at, config_authkey) 
        VALUES ($server_ip, $server_id, NULL, $team_a, NULL, $team_b, 1, 0, 0, 15, 'esl5on5',16000, 0, 0, 0, 0, 0, 0, 'normal', NOW(), NOW(), $config_authkey)") or die('Er is een fout opgetreden! MySQL zegt:'. mysqli_error($con));
Het zal mij niets verbazen wanneer er om een aantal variabelen in de query-string quotes moeten zodat ze als string de database in gaan.
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan