2017-04-25 8 views
0

mysqliでmysql btの新しい部分をコーディングする必要があります。私はこれらの挿入クエリをデータベースに実行できません。私は多くを検索しましたが、簡単な提案を見つけることができなかった、または私が理解していない可能性があります。OOP mysqli拡張クラスのデータベースとの接続

未定義変数:Cにmysqliの:\ WAMP \ WWWライン上の新しいフォルダ\ PHPの\のmsqliconnect.php \ 32

致命的なエラー:Cにおける非オブジェクトにメンバ関数mysqli_query()の呼び出し: \ wamp \ www \新しいフォルダ\ php \ msqliconnect.php on line 32

何か助けていただければ幸いです。あなたのconnect()insertdata()方法の両方で

<?php 
class connection 

    { 
    public $mysqli; 

    function connect() 
     { 
     $hostname = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $database = "demodatabase"; 
     $mysqli = new mysqli($hostname, $username, $password, $database); 
     /* check connection */ 
     if (mysqli_connect_errno()) 
      { 
      printf("Connect failed: %s\n", mysqli_connect_error()); 
      exit(); 
      } 

     return true; 
     } 
    } 

class Index extends connection 

    { 
    function __construct() 
     { 
     parent::connect(); 
     } 

    function insertdata($a, $b) 
     { 

     // echo $a. ' ' .$b; 
     // MySqli Insert Query 

     $status = 0; 
     $insert_row = $mysqli->mysqli_query("INSERT INTO tb_user (id, user, password, status) VALUES('','" . $a . "', '" . $b . "', '')"); 
     if ($insert_row) 
      { 
      print 'saved'; 
      } 
      else 
      { 
      die('Error : (' . $mysqli->errno . ') ' . $mysqli->error); 
      } 
     } 
    } 

?> 
+0

あなたのコードは[** SQLインジェクション攻撃**](https://en.wikipedia.org/wiki/SQL_injection)に脆弱です。 [** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https://secure.php.net/)を使用してください。 manual/en/pdo.prepared-statements.php)は、[** this post **](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql - インジェクション - イン - php)。 –

+0

@AlexHowanskyまだ、それが一度発射されれば、ええ。 –

答えて

1

、あなたはローカル変数$mysqli、ないインスタンス変数 を使用しています。インスタンスメソッドで$mysqliの代わりに$this->mysqliを使用する必要があります。だからあなたのconnect()insertdata()方法は次のようになります:

function connect(){ 
    $hostname = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $database = "demodatabase"; 
    $this->mysqli = new mysqli($hostname, $username, $password, $database); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
    return true;    
} 

function insertdata($a, $b){ 
    $insert_row = $this->mysqli->query("INSERT INTO tb_user (id, user, password, status) VALUES('','".$a."', '".$b."', '')"); 
    if($insert_row){ 
     print 'saved'; 
    }else{ 
     die('Error : ('. $this->mysqli->errno .') '. $this->mysqli->error); 
    } 
} 


追記:今、あなたのクエリがSQLインジェクション攻撃を受けやすいので、についてprepared statementこちらをご覧ください。また、how you can prevent SQL injection in PHPを参照してください。

+0

ありがとうalot :)と提案のあまりにも。 – Krixnaas

関連する問題