2017-10-25 17 views
-2

データベースにデータを挿入するクエリまで、すべてが機能します。私はデータベースに挿入するために1つの変数だけを使用しようとしましたが、私はまだ正しく実行するためのクエリを取得することはできません。それはちょうど私が行方不明だが、私はそれを見つけることができない単純なタイプミスかもしれません。クエリを使用してPHPを使用してデータベースにデータを挿入できません。

HTML選択ページ

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title></title> 
 
\t <style> 
 
\t \t * { 
 
\t \t \t margin: 0; 
 
\t \t \t padding: 0; 
 
\t \t } 
 

 
\t \t #container { 
 
\t \t \t margin: 15px auto; 
 
\t \t \t width: 700px; 
 
\t \t \t border: 1px solid #cccccc; 
 
\t \t \t border-radius: 3px; 
 
\t \t } 
 

 
\t \t .title-container { 
 
\t \t \t padding: 20px; 
 
\t \t } 
 

 
\t \t .title { 
 
\t \t \t font-size: 28px; 
 
\t \t \t margin: 20px; 
 
\t \t } 
 

 
\t \t .price { 
 
\t \t \t color: red; 
 
\t \t } 
 

 
\t \t img { 
 
\t \t \t width: 100%; 
 
\t \t \t height: auto; 
 
\t \t \t margin: 0; 
 
\t \t \t padding: 0; 
 
\t \t } 
 

 
\t \t #submit { 
 
\t \t \t width: 100%; 
 
\t \t \t text-align: center; 
 
\t \t \t background-color: red; 
 
\t \t \t color: white; 
 
\t \t \t padding: 15px; 
 
\t \t \t border: 0; 
 
\t \t \t margin-bottom: 20px; 
 
\t \t \t font-size: 20px; 
 
\t \t } 
 
\t </style> 
 
</head> 
 
<body> 
 
\t <div id="container"> 
 
\t \t <form action="checkout.php" method="post"> 
 
\t \t \t <div class="product"> 
 
\t \t \t \t <div class="title-container"> 
 
\t \t \t \t \t <input type="radio" name="game" value="Assassin's Creed II"><span class="title">Assassin's Creed II - <span class="price">$15.99</span><br /> 
 
\t \t \t \t </div> 
 
\t \t \t \t <img src="assassin2.png"> 
 
\t \t \t </div> 
 
\t \t \t <div class="product"> 
 
\t \t \t \t <div class="title-container"> 
 
\t \t \t \t \t <input type="radio" name="game" value="Assassin's Creed Brotherhood"><span class="title">Assassin's Creed Brotherhood - <span class="price">$19.99</span><br /> 
 
\t \t \t \t </div> 
 
\t \t \t \t <img src="brotherhood.jpg"> 
 
\t \t \t </div> 
 
\t \t \t <div class="product"> 
 
\t \t \t \t <div class="title-container"> 
 
\t \t \t \t \t <input type="radio" name="game" value="Assassin's Creed Revelations"><span class="title">Assassin's Creed Revelations - <span class="price">$24.99</span><br /> 
 
\t \t \t \t </div> 
 
\t \t \t \t <img src="revelations.jpg"> 
 
\t \t \t </div> 
 
\t \t \t <h4>Enter quantity: <input type="number" size="2" name="qty"></h4> 
 
\t \t \t <input type="submit" value="Checkout" id="submit"> 
 
\t \t </form> 
 
\t </div> 
 
</body> 
 
</html>

PHPオーダーの概要ページ

<?php 
 
\t session_start(); 
 
\t $game = $_POST['game']; 
 
\t $qty = $_POST['qty']; 
 
\t $_SESSION['sale_game'] = $game; 
 
\t $_SESSION['sale_qty'] = $qty; 
 
\t $price; 
 
\t $subtotal; 
 
\t if ($game == "Assassin's Creed II") { 
 
\t \t $price = 15.99; 
 
\t } elseif ($game == "Assassin's Creed Brotherhood") { 
 
\t \t $price = 19.99; 
 
\t } elseif ($game == "Assassin's Creed Revelations") { 
 
\t \t $price = 24.99; 
 
\t } 
 

 
\t $_SESSION['sale_price'] = $price; 
 
\t $subtotal = $price * $qty; 
 
\t $_SESSION['sale_subtotal'] = $subtotal; 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title></title> 
 
\t <style> 
 
\t \t * { 
 
\t \t \t margin: 0; 
 
\t \t \t padding: 0; 
 
\t \t } 
 

 
\t \t #container { 
 
\t \t \t width: 1000px; 
 
\t \t \t margin: 15px auto; 
 
\t \t \t border: 1px solid black; 
 
\t \t \t overflow: auto; 
 
\t \t } 
 

 
\t \t #image-container { 
 
\t \t \t float: left; 
 
\t \t \t padding: 10px; 
 
\t \t } 
 

 
\t \t #info-container { 
 
\t \t \t float: left; 
 
\t \t \t padding: 20px; 
 
\t \t } 
 

 
\t \t h3 { 
 
\t \t \t margin-left: 10px; 
 
\t \t } 
 

 
\t \t p { 
 
\t \t \t margin: 10px; 
 
\t \t } 
 

 
\t \t #price { 
 
\t \t \t color: red; 
 
\t \t } 
 

 
\t \t #user-info-container { 
 
\t \t \t clear: both; 
 
\t \t \t padding: 10px; 
 
\t \t } 
 

 
\t \t h2 { 
 
\t \t \t color: red; 
 
\t \t } 
 

 
\t \t table { 
 
\t \t \t margin: 20px auto; 
 
\t \t \t font-size: 24px; 
 
\t \t } 
 

 
\t \t input { 
 
\t \t \t font-size: 18px; 
 
\t \t } 
 

 
\t \t #submit { 
 
\t \t \t color: white; 
 
\t \t \t background-color: red; 
 
\t \t \t border: 0; 
 
\t \t \t padding: 10px; 
 
\t \t \t border-radius: 3px; 
 
\t \t } 
 
\t </style> 
 
</head> 
 
<body> 
 
\t <div id="container"> 
 
\t \t <div id="image-container"> 
 
\t \t <?php 
 
\t \t \t if ($game == "Assassin's Creed II") { 
 
\t \t \t \t echo "<img src='assassin2.png' width='300' height='170'/>"; 
 
\t \t \t } elseif ($game == "Assassin's Creed Brotherhood") { 
 
\t \t \t \t echo "<img src='brotherhood.jpg' width='300' height='170'/>"; 
 
\t \t \t } elseif ($game == "Assassin's Creed Revelations") { 
 
\t \t \t \t echo "<img src='revelations.jpg' width='300' height='170' />"; 
 
\t \t \t } 
 
\t \t ?> 
 
\t \t </div> 
 
\t \t <div id="info-container"> 
 
\t \t \t <h3>Checkout Info</h3> 
 
\t \t \t <p><strong>Game:</strong> <?php echo $game; ?></p> 
 
\t \t \t <p><strong>Price:</strong> <span id="price">$<?php echo $price; ?></span></p> 
 
\t \t \t <p><strong>Quantity:</strong> <?php echo $qty; ?></p> 
 
\t \t \t <p><strong>Subtotal:</strong> $<?php echo $subtotal; ?></p> 
 
\t \t </div> 
 
\t \t <hr style="clear: both; margin: 10px"> 
 
\t \t <div id="user-info-container"> 
 
\t \t \t <h2>Enter your information</h2> 
 
\t \t \t <form action="insert.php" method="post"> 
 
\t \t \t \t <table cellspacing="10"> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td>First Name</td> 
 
\t \t \t \t \t \t <td>Last Name</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td><input type="text" name="fName"></td> 
 
\t \t \t \t \t \t <td><input type="text" name="lName"></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td>Address</td> 
 
\t \t \t \t \t \t <td></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td colspan="2"><input type="text" name="address" style="width: 99%"></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td>City</td> 
 
\t \t \t \t \t \t <td>State</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td><input type="text" name="city"></td> 
 
\t \t \t \t \t \t <td><input type="text" name="state"></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td>ZIP</td> 
 
\t \t \t \t \t \t <td>Email</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td><input type="number" name="zip"></td> 
 
\t \t \t \t \t \t <td><input type="text" name="email"></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <td colspan="2" style="text-align: center;"><input type="submit" value="Submit Order" id="submit"></td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t </table> 
 
\t \t \t </form> \t \t 
 
\t \t </div> 
 
\t </div> 
 
</body> 
 
</html>

PHPインサートページ

<?php 
 
\t session_start(); 
 
\t $game = $_SESSION['sale_game']; 
 
\t $qty = $_SESSION['sale_qty']; 
 
\t $price = $_SESSION['sale_price']; 
 
\t $subtotal = $_SESSION['sale_subtotal']; 
 
\t $fName = $_POST['fName']; 
 
\t $lName = $_POST['lName']; 
 
\t $address = $_POST['address']; 
 
\t $city = $_POST['city']; 
 
\t $state = $_POST['state']; 
 
\t $zip = $_POST['zip']; 
 
\t $email = $_POST['email']; 
 
\t $con = new mysqli('localhost', 'root', 'root', 'purchases'); 
 
\t if (!$con) { 
 
\t \t echo "Not connected to database"; 
 
\t } else { 
 
\t \t $query = "INSERT INTO orders (Game, Price, Quantity, Total, fName, lName, Address, City, State, Zip, Email) VALUES ('$game', '$price', '$qty', '$subtotal', '$fName', '$lName', '$address', '$city', '$state', '$zip', '$email')"; 
 
\t \t if ($con->query($query) === TRUE) { 
 
\t \t \t echo "Inserted"; 
 
\t \t } else { 
 
\t \t \t echo "Not Inserted"; 
 
\t \t } 
 
\t } 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title></title> 
 
\t <style> 
 
\t \t * { 
 
\t \t \t margin: 0; 
 
\t \t \t padding: 0; 
 
\t \t } 
 

 
\t \t #container { 
 
\t \t \t top: 50%; 
 
\t \t \t left: 50%; 
 
\t \t \t position: absolute; 
 
\t \t \t border: 1px solid black; 
 
\t \t } 
 
\t </style> 
 
</head> 
 
<body> 
 
\t <div id="container"> 
 
\t \t <p> 
 
\t \t </p> 
 
\t </div> 
 
</body> 
 
</html>

+4

プリペアドステートメントとプレースホルダを使用する必要がありますだけで、関連するコードを投稿してください。 Cssは明らかに不要です。 – Akintunde007

+0

echo $ query;死ぬ();クエリを実行します。 mysqlエラーを確認してください –

+1

あなたのクエリがエスケープされていない場合は、入力変数を引用する必要があります。あなたのゲームが一重引用符で照合された場合、文字列クエリは失敗します。 – Tobia

答えて

0

文字列をエスケープしてみてください:

<?php 
     session_start(); 
     $con = new mysqli('localhost', 'root', 'root', 'purchases'); 
     if (!$con) { 
      echo "Not connected to database"; 
     } else { 
      $game = mysqli_real_escape_string($con,$_SESSION['sale_game']); 
      $qty = mysqli_real_escape_string($con,$_SESSION['sale_qty']); 
      $price = mysqli_real_escape_string($con,$_SESSION['sale_price']); 
      $subtotal = mysqli_real_escape_string($con,$_SESSION['sale_subtotal']); 
      $fName = mysqli_real_escape_string($con,$_POST['fName']); 
      $lName = mysqli_real_escape_string($con,$_POST['lName']); 
      $address = mysqli_real_escape_string($con,$_POST['address']); 
      $city = mysqli_real_escape_string($con,$_POST['city']); 
      $state = mysqli_real_escape_string($con,$_POST['state']); 
      $zip = mysqli_real_escape_string($con,$_POST['zip']); 
      $email = mysqli_real_escape_string($con,$_POST['email']); 
      $query = "INSERT INTO orders (Game, Price, Quantity, Total, fName, lName, Address, City, State, Zip, Email) VALUES ('$game', '$price', '$qty', '$subtotal', '$fName', '$lName', '$address', '$city', '$state', '$zip', '$email')"; 
      if ($con->query($query) === TRUE) { 
       echo "Inserted"; 
      } else { 
       echo "Not Inserted"; 
      } 
     } 
    ?> 
0

あなたは

$con = new mysqli('localhost', 'root', 'root', 'purchases'); 
    if (!$con) { 
     echo "Not connected to database"; 
    } else { 
     $query = "INSERT INTO orders (Game, Price, Quantity, Total, fName, lName, Address, City, State, Zip, Email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
$stmt = $con->prepare($query); 
$stmt->bind_param("s", $game); 
$stmt->bind_param("d", $price); 
$stmt->bind_param("i", $qty); 
$stmt->bind_param("d", $subtotal); 
$stmt->bind_param("s", $fName); 
$stmt->bind_param("s", $lName); 
$stmt->bind_param("s", $address); 
$stmt->bind_param("s", $state); 
$stmt->bind_param("s", $zip); 
$stmt->bind_param("s", $email); 


     if ($stmt->execute() === TRUE) { 
      echo "Inserted"; 
     } else { 
      echo "Not Inserted"; 
     } 
    } 
関連する問題