2017-02-17 36 views
-2

古いファイルをmysqliに変換していて、mysql_real_escape_stringに達するまでうまくいっています。 2つのパラメータを渡さないというエラーメッセージが表示されていますが、1つしか与えていないが、2つ目のパラメータを追加する場所を特定できないことを理解しています(私はDb接続を探していますが、私はもはや確信していないたくさんのことを試しました)。 私は$ _POSTコマンドの前にDb接続を入れてもいいと思っていましたが、うまくいかず、2つのパラメータエラーが出ました。Mysqliと実際のエスケープ文字列エラー

私は

nclude "../connections/connect_mysqli.php"; 
    $conn = dbConnect('read'); 
    $sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; // query the person 
    $result = $conn->query($sql) or die(mysqli_error()); 
    // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- 
    $existCount = mysqli_num_rows($result); // count the row nums 
if ($existCount == 0) { // evaluate the count 
    echo "Your login session data is not on record in the database."; 
    exit(); 
} 
?> 
<?php 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 
<?php 
// Delete Item Question to Admin, and Delete Product if they choose 
if (isset($_GET['deleteid'])) { 
    echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="inventory_list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>'; 
    exit(); 
} 
if (isset($_GET['yesdelete'])) { 
    // remove item from system and delete its picture 
    // delete from database 
    $id_to_delete = $_GET['yesdelete']; 
    $sql = "DELETE FROM products WHERE id='$id_to_delete' LIMIT 1" or die (mysqli_error()); 
    // unlink the image from server 
    // Remove The Pic ------------------------------------------- 
    $pictodelete = ("../images/$id_to_delete.jpg"); 
    if (file_exists($pictodelete)) { 
       unlink($pictodelete); 
    } 
    header("location: inventory_list.php"); 
    exit(); 
} 
?> 
<?php 
// Parse the form data and add inventory item to the system 
if (isset($_POST['product_name'])) { 

    $product_name = mysqli_real_escape_string($conn, $_POST['product_name']); 
    $price = mysqli_real_escape_string($_POST['price']); 
    $details = mysqli_real_escape_string($_POST['details']); 
    $details2 = mysqli_real_escape_string($_POST['details2']); 
    $details3 = mysqli_real_escape_string($_POST['details3']); 
    // See if that product name is an identical match to another product in the system 
    $sql = "SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"; 
    $productMatch = mysqli_num_rows($result); // count the output amount 
    if ($productMatch > 0) { 
     echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>'; 
     exit(); 
    } 
    // Add this product into the database now 
    $sql = ("INSERT INTO products (product_name, price, details, details2, details3, date_added) 
     VALUES('$product_name','$price','$details','$details2','$details3',now())") or die (mysqli_error()); 
    $pid = mysqli_insert_id(); 
    // Place image in the folder 
    $newname = "$pid.jpg"; 
    move_uploaded_file($_FILES['fileField']['tmp_name'], "../images/$newname"); 
    header("location: inventory_list.php"); 
    exit(); 
} 
?> 
+0

* *「私はmysql_real_escape_stringのヒットまで」 - それを示唆しているコードがないし、あなたが 'i'バージョンを使用し、それに接続するとどのような接続が実際にあるを渡された場合。だから、それは誰のボールゲームだ。あなたが本当に使っているものについては、私は(推測ゲームでは)演奏しません。 –

+0

申し訳ありませんフレッド、私はそれがすべてコピーされたと思った。 – ribbonman

答えて

0

あり、あなたのコード例で使われたreal_escape_string方法はありませんが、あなたは、私はmysqliの接続だと思う$ connオブジェクトのqueryメソッドを呼び出しています。したがって、使用できるのは

$string = $conn->real_escape_string($string); 

mysql_real_escape_stringの代わりに使用できます。実際には、mysqli関数の代わりにオブジェクトメソッドとプロパティを使用することができます。たとえば、mysqli_num_rowsの代わりに

$result->num_rows 

を使用できます。

これが役に立ちます。

+0

ありがとうsajushko、私はそれを試してみましょう。 – ribbonman

0

かなりの問題があります。

はまず、

mysqli_real_escape_string()は、データベース接続を必要とし、最初の引数として、あなたは、あなたのコードでそれらのいずれかのためにそれを使用しPOST配列(または変数)、続きます。

$product_name = mysqli_real_escape_string($conn, $_POST['product_name']); 
              ^^^^^ 

となります。残りのすべての場合は、下のすべての操作を行う必要があります。

また、このクエリを実行しませんでした:

$sql = "DELETE FROM products .... 

も、この1:

$sql = ("INSERT INTO products .... 

も、この1:

$sql = "SELECT id FROM products ... 

私はまた、あなたがパスワードを保存することができる気づきましたプレーンテキストとして、これは実際の環境で使用することは安全ではありません。

password_hash()を使用してください。また、準備されたステートメントも良いことです。

mysqli_error($conn) 

と、すべてのPOSTアレイは値が含まれないことを確認してください:

プラス、mysqli_error()もDB接続が必要です。

+0

フレッドさん、ありがとうございました。私はかなりしばらくそれを叩いています。私はエスケープ文字列で$ connを持っていましたが、私はそれらを最初から始めて取り出しました。私は、ページ上部のDb接続が後で追加せずに実行され、間違っていると思った。また、何度も消去してからやり直しても、クエリの実行が見逃されました。私は通常のサイトでパスワードハッシュを使用していますが、これはチュートリアルでしたので、私はそれをそのまま残しました。私は本当に助けに感謝します。乾杯。 – ribbonman

+0

@ribbonmanよろしくお願いします。問題を解決済みとしたい場合は、それをしてください。そうすれば、他の回答は必要ないと誰にも知らせることができ、質問は終了します。*歓声* –

+0

@ribbonman http://meta.stackexchange.com/a/5235/231583これは、問題が解決されたbtwとしてマークされている方法です。 –

0

将来の初心者のために完成したコードを投稿して、私と同じボートに入ったら助けたいと思っています。

 include "../connections/connect_mysqli.php"; 
    $conn = dbConnect('read'); 
    $sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; // query the person 
    $result = $conn->query($sql) or die($conn->error); 
    // ------- MAKE SURE PERSON EXISTS IN DATABASE --------- 
    $existCount = $result->num_rows; // count the row nums 
if ($existCount == 0) { // evaluate the count 
    echo "Your login session data is not on record in the database."; 
    exit(); 
} 
?> 
<?php 
// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 
<?php 
// Delete Item Question to Admin, and Delete Product if they choose 
if (isset($_GET['deleteid'])) { 
    echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="inventory_list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>'; 
    exit(); 
} 
if (isset($_GET['yesdelete'])) { 
    // remove item from system and delete its picture 
    // delete from database 
    $id_to_delete = $_GET['yesdelete']; 
    $sql = "DELETE FROM products WHERE id='$id_to_delete' LIMIT 1"; 
    $result = $conn->query($sql) or die($conn->error); 
    // unlink the image from server 
    // Remove The Pic ------------------------------------------- 
    $pictodelete = ("../images/$id_to_delete.jpg"); 
    if (file_exists($pictodelete)) { 
       unlink($pictodelete); 
    } 
    header("location: inventory_list.php"); 
    exit(); 
} 
?> 
<?php 
// Parse the form data and add inventory item to the system 
if (isset($_POST['product_name'])) { 

    $product_name = $conn->real_escape_string($_POST['product_name']); 
    $price = $conn->real_escape_string($_POST['price']); 
    $details = $conn->real_escape_string($_POST['details']); 
    $details2 = $conn->real_escape_string($_POST['details2']); 
    $details3 = $conn->real_escape_string($_POST['details3']); 
    // See if that product name is an identical match to another product in the system 
    $sql = "SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"; 
    $result = $conn->query($sql) or die($conn->error); 
    $productMatch = $result->num_rows; // count the output amount 
    if ($productMatch > 0) { 
     echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>'; 
     exit(); 
    } 
    // Add this product into the database now 
    $sql = ("INSERT INTO products (product_name, price, details, details2, details3, date_added) 
     VALUES('$product_name','$price','$details','$details2','$details3',now())") or die ($conn->error); 
     $result = $conn->query($sql) or die($conn->error); 
    $pid = $conn->insert_id; 
    // Place image in the folder 
    $newname = "$pid.jpg"; 
    move_uploaded_file($_FILES['fileField']['tmp_name'], "../images/$newname"); 
    header("location: inventory_list.php"); 
    exit(); 
} 
?> 
関連する問題