2016-10-08 17 views
0

私はphpとsqlを初めて利用しています。 PHP検索結果の同じページで価格を更新する方法について質問したいと思います。検索結果に書籍の価格を更新するには、inputsubmitボタンを追加する必要があります。提出すると、書籍の新しい価格がデータベースに更新され、同じページに再表示されます。php検索結果の同じページで価格を更新

検索のhtmlページと結果のPHPページの既存のコードです。検索結果は既にページに表示されますが、価格を更新するにはどうすればよいですか?ボタンをクリックすると、ページにエラーが表示されます。検索の詳細を入力していません。戻ってもう一度やり直してください。私を助けてください。

Search page

検索ページ:

<html> 
<head> 
    <title>Book-O-Rama Catalog Search</title> 
</head> 

<body> 
    <h1>Book-O-Rama Catalog Search</h1> 

    <form action="results.php" method="post"> 
    Choose Search Type:<br /> 
    <select name="searchtype"> 
     <option value="author">Author 
     <option value="title">Title 
     <option value="isbn">ISBN 
    </select> 
    <br /> 
    Enter Search Term:<br /> 
    <input name="searchterm" type="text" size="40"> 
    <br /> 
    <input type="submit" name="submit" value="Search"> 
    </form> 

</body> 
</html> 

Result page with submit button

アップデートページ:

<html> 
 
<head> 
 
    <title>Book-O-Rama Search Results</title> 
 
</head> 
 
<body> 
 
<h1>Book-O-Rama Search Results</h1> 
 
<?php 
 
    // create short variable names 
 
    $searchtype=$_POST['searchtype']; 
 
    $searchterm=trim($_POST['searchterm']); 
 

 
    if (!$searchtype || !$searchterm) { 
 
    echo 'You have not entered search details. Please go back and try again.'; 
 
    exit; 
 
    } 
 

 
    if (!get_magic_quotes_gpc()){ 
 
    $searchtype = addslashes($searchtype); 
 
    $searchterm = addslashes($searchterm); 
 
    } 
 

 
    @ $db = new mysqli('localhost', 'pf31ee', 'pf31ee', 'pf31ee'); 
 

 
    if (mysqli_connect_errno()) { 
 
    echo 'Error: Could not connect to database. Please try again later.'; 
 
    exit; 
 
    } 
 

 
    $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; 
 
    $result = $db->query($query); 
 

 
    $num_results = $result->num_rows; 
 

 
    echo "<p>Number of books found: ".$num_results."</p>"; 
 

 
    for ($i=0; $i <$num_results; $i++) { 
 
    $row = $result->fetch_assoc(); 
 
    echo "<p><strong>".($i+1).". Title: "; 
 
    echo htmlspecialchars(stripslashes($row['title'])); 
 
    echo "</strong><br />Author: "; 
 
    echo stripslashes($row['author']); 
 
    echo "<br />ISBN: "; 
 
    echo stripslashes($row['isbn']); 
 
    echo "<br />Price: "; 
 
    echo stripslashes($row['price']); 
 
    echo "</p>"; 
 
    } 
 

 
    $result->free(); 
 
    $db->close(); 
 

 
?> 
 

 
<form action="results.php" method="post"> 
 
<input type="submit" name="submit" value="Update price">&nbsp;<input type="text" name="price" maxlength="7" size="7"> 
 
<input type="hidden" name="searchtype" value="<?php echo htmlspecialchars($_POST['searchtype']);?>" /> 
 
<input type="hidden" name="searchterm" value="<?php echo htmlspecialchars($_POST['searchterm']);?>" /> 
 
<?php 
 
$updateprice=$_POST['price']; 
 

 
@ $db = new mysqli('localhost', 'pf31ee', 'pf31ee', 'pf31ee'); 
 
mysql_select_db('books'); 
 

 
    $query1 = "UPDATE books SET price = ".$updateprice." WHERE ".$searchtype." like '%".$searchterm."%'"; 
 
    $result1 = $db->query($query1); 
 
    $result->free(); 
 
    $db->close(); 
 

 
?> 
 

 
</form> 
 
</body> 
 
</html>

create table customers 
 
(customerid int unsigned not null auto_increment primary key, 
 
    name char(50) not null, 
 
    address char(100) not null, 
 
    city char(30) not null 
 
); 
 

 
create table orders 
 
(orderid int unsigned not null auto_increment primary key, 
 
    customerid int unsigned not null, 
 
    amount float(6,2), 
 
    date date not null 
 
); 
 

 
create table books 
 
( isbn char(13) not null primary key, 
 
    author char(50), 
 
    title char(100), 
 
    price float(4,2) 
 
); 
 

 
create table order_items 
 
(orderid int unsigned not null, 
 
    isbn char(13) not null, 
 
    quantity tinyint unsigned, 
 

 
    primary key (orderid, isbn) 
 

 
); 
 
create table book_reviews 
 
(
 
    isbn char(13) not null primary key, 
 
    review text 
 
);

use myuser; 
 

 
insert into customers values 
 
    (3, "Julie Smith", "25 Oak Street", "Airport West"), 
 
    (4, "Alan Wong", "1/47 Haines Avenue", "Box Hill"), 
 
    (5, "Michelle Arthur", "357 North Road", "Yarraville"); 
 

 
insert into orders values 
 
    (NULL, 3, 69.98, "2007-04-02"), 
 
    (NULL, 1, 49.99, "2007-04-15"), 
 
    (NULL, 2, 74.98, "2007-04-19"), 
 
    (NULL, 3, 24.99, "2007-05-01"); 
 

 
insert into books values 
 
    ("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99), 
 
    ("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99), 
 
    ("0-672-31509-2", "Pruitt, et al.", "Teach Yourself GIMP in 24 Hours", 24.99), 
 
    ("0-672-31769-9", "Thomas Schenk", "Caldera OpenLinux System Administration Unleashed", 49.99); 
 

 
insert into order_items values 
 
    (1, "0-672-31697-8", 2), 
 
    (2, "0-672-31769-9", 1), 
 
    (3, "0-672-31769-9", 1), 
 
    (3, "0-672-31509-2", 1), 
 
    (4, "0-672-31745-1", 3); 
 

 
insert into book_reviews values 
 
    ("0-672-31697-8", "Morgan's book is clearly written and goes well beyond most of the basic Java books out there.");

+0

作成 '

'見つかった各書籍について、あなたは私がボタンをクリックしたときに私がクリックした後ので、検索タイプと検索語の値を保存する方法を、次に 'update'-クエリ –

+0

を使用することができ、フォームハンドラを作成し、私が検索ページに入力したものを失ってしまったwesbiteをリロードします。 @ chris85 – CorScorpii

答えて

0

あなたにも更新フォームで検索タイプと値を渡す必要があります。あなたはそれに隠れた入力フィールドを使うことができます。

<html> 
<head> 
    <title>Book-O-Rama Search Results</title> 
</head> 
<body> 
<h1>Book-O-Rama Search Results</h1> 
<?php 
    // create short variable names 
    $searchtype=$_POST['searchtype']; 
    $searchterm=trim($_POST['searchterm']); 

    if (!$searchtype || !$searchterm) { 
    echo 'You have not entered search details. Please go back and try again.'; 
    exit; 
    } 

    if (!get_magic_quotes_gpc()){ 
    $searchtype = addslashes($searchtype); 
    $searchterm = addslashes($searchterm); 
    } 

    @ $db = new mysqli('localhost', 'pf31ee', 'pf31ee', 'pf31ee'); 

    if (mysqli_connect_errno()) { 
    echo 'Error: Could not connect to database. Please try again later.'; 
    exit; 
    } 

    $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; 
    $result = $db->query($query); 

    $num_results = $result->num_rows; 

    echo "<p>Number of books found: ".$num_results."</p>"; 

    for ($i=0; $i <$num_results; $i++) { 
    $row = $result->fetch_assoc(); 
    echo "<p><strong>".($i+1).". Title: "; 
    echo htmlspecialchars(stripslashes($row['title'])); 
    echo "</strong><br />Author: "; 
    echo stripslashes($row['author']); 
    echo "<br />ISBN: "; 
    echo stripslashes($row['isbn']); 
    echo "<br />Price: "; 
    echo stripslashes($row['price']); 
    echo "</p>"; 
    } 

    $result->free(); 
    $db->close(); 

?> 
<form action="" method="post"> 
<input type="submit" name="submit" value="Update price">&nbsp;<input type="text" name="price" maxlength="7" size="7"> 
<input type="hidden" name="searchtype" value="<?php echo htmlspecialchars($_POST['searchtype']);?>" /> 
<input type="hidden" name="searchterm" value="<?php echo htmlspecialchars($_POST['searchterm']);?>" /> 
</form> 
</body> 
</html> 

このコードもSQLインジェクションに対応しています。あなたはparameterized queriesを使用する必要があり、列のためのホワイトリストは、渡されるaddslashes機能が不十分である、マニュアルごとに:。

は、データベース・パラメータ・エスケープ用にaddslashes()の使用を注意してくださいセキュリティ問題の原因となることができ、ほとんどの上データベース。

+0

ありがとう。私は別の問題を抱えています。私はdbを再度接続して価格を更新しようとしますが、動作しません。 $ query = "UPDATE books SET price ="。$ updateprice。 "、" $ searchtype。 "のように '%'と表示されます。それは間違っていますか? – CorScorpii

+0

はい、末尾にカンマがあります。 'SET price ="。$ updateprice。 "、' < - 。問題を解決する場合は、この回答を受け入れてください。 'UPDATE books SET price ="。$ updateprice "でなければなりません。 WHERE "。$ searchtype。" $ searchterm。 "%"はSQLインジェクションに再びオープンします。 – chris85

+0

ほぼ完了です。唯一の問題は、更新された価格を表示するためにページを2回クリックする必要がある理由です。私は最初に変更されたデータベースをクリックした後、サーバー側をチェックしました。 – CorScorpii

関連する問題