2016-04-19 12 views
0

私のPHPフォームがフィールドのデータをmySQLデータベースに入れない理由を理解しようとしています。私はこれを理解しようとしてきたが、枯れてしまった。mySQLに何も表示されないPHPフォーム

私のinsert $ sqlは、各フィールドの値でハードコードされているが、PHPフォームから入力されたフィールドを使用しようとしたときにうまく動作しません。

提出をクリックするとエラーは表示されませんが、別の所有者が追加されたかどうかを確認するためにmySQLをチェックすると何も表示されません。

誰でも私がこれを解決するのを助けることができれば、本当にありがとう。

ところで、私の$ sqlの挿入文は引用符で正しくありますか?

<head> 
<style> 
table, th, td { 
    border: 1px solid black; 
    border-collapse: collapse; 
} 
th, td { 
    padding: 5px; 
} 
</style> 
</head> 
<body> 
<?php #index.php for Assignment 10 
$page_title = 'Assignment 10 for Marina Database'; 
include('header.html'); 
require('dbConn.php'); 
echo '<h1> Please enter the following fields:</h1>'; 


if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
$OwnerNum=$_POST['OwnerNum']; 
$LastName=$_POST['LastName']; 
$FirstName=$_POST['FirstName']; 
$Address=$_POST['Address']; 
$City=$_POST['City']; 
$State=$_POST['State']; 
$Zip=$_POST['Zip']; 

//echo test; 
try { 
$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')"; 

//this works when hard coded 
/*$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, `State, Zip) VALUES ('XR34', 'Patel', 'John', '342 Picardy lane', 'Wheeling', 'IL', '60018')"; */` 
$conn->exec($sql); 

//echo $OwnerNum, $LastName, $FirstName, $Address, $City, $State, $Zip; 
} 
catch (PDOException $e) 
{ 
echo 'Error: '.$e->getMessage(); 
} //end catch 



} 



if (isset($_POST['submit'])) 
{ 
    $stmt = $conn->prepare("select* from Owner"); 
    $stmt->execute(); 

    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

    echo "<table style='border: solid 1px black;'>"; 
    echo "<tr><th>OwnerNum</th><th>LastName</th><th>FirstName</th><th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>"; 


class TableRows extends RecursiveIteratorIterator 
{ 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 

    function current() { 
     return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; 
    } 

    function beginChildren() { 
     echo "<tr>"; 
    } 

    function endChildren() { 
     echo "</tr>" . "\n"; 
    } 

} 

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) 
    { 
     echo $v; 

    } 

    $conn = null; 
    echo "</table>"; 
} 

?> 
<form name="createOwner" action="Assignment10newowner.php" method="POST"> 
    <table style="width:100%"> 
    <tr> 
     <td>Owner Number:</td> 
     <td><input type="text" name="OwnerNum"></td> 
    </tr> 

     <td>Last Name:</td> 
     <td><input type="text" name="LastName"></td 


    </tr> 
    <tr> 
     <td>First Name:</td> 
     <td><input type="text" name="FirstName"></td 


    </tr> 
    <tr> 
     <td>Address:</td> 
     <td><input type="text" name="Address"></td 


    </tr> 
    <tr> 
     <td>City:</td> 
     <td><input type="text" name="City"></td 


    </tr> 
    <tr> 
     <td>State:</td> 
     <td><input type="text" name="State"></td 


    </tr> 
    <tr> 
     <td>Zip:</td> 
     <td><input type="text" name="Zip"></td 


    </tr> 
    </table> 
    <br> 
    <br> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
+0

ところ、これらの変数である ' "$ OwnerNum。"'、 ' "$姓。"'、 ' "$姓。"'、 ' "$アドレス。"'、「」。$シティ。" –

+0

これらの変数はどれも定義されていないからです。 '$ OwnerNum'ではなく、' $ _POST ['OwnerNum'] 'を探しています。また、変数に直接クエリを入れるのは悪い習慣です。 – larsAnders

+0

また、データベース接続を確認してください – rohitr

答えて

1

割り当て$ _POST値この

if (isset($_POST['OwnerNum])) 
{ 

    $OwnerNum=$_POST['OwnerNum']; 
    $LastName=$_POST['LastName']; 
    $FirstName=$_POST['FirstName']; 
    $Address=$_POST['Address']; 
    $City=$_POST['City']; 
    $State=$_POST['State']; 
    $Zip=$_POST['Zip']; 

    try { 
     $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
     ('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";  
     $conn->exec($sql); 
     $select_owner = "SELECT * FROM Owner"; 
     ?> 
      <table> 
       <tr> 
        <td>OwnerNum</td> 
        <td>FirstName</td> 
        <td>LastName</td> 
        <td>Address</td> 
        <td>City</td> 
       </tr> 
     <?php  
     $conn->prepare($select_owner); 
     $result = $conn->fetchAll(); 
     if (count($result)) { 

      foreach($result as $owner){ 

        ?> 
        <tr> 
         <td><?=$owner['OwnerNum'];?></td> 
         <td><?=$owner['FirstName'];?></td> 
         <td><?=$owner['LastName'];?></td> 
         <td><?=$owner['Address'];?></td> 
         <td><?=$owner['City'];?></td> 
        </tr>  
      <?php 
       } 
     } 
     else 
     { 
      ?> 
      <tr> 
       <td colspan="5">No Onwers Found</td>    
      </tr> 
      <?php 
     } 
     unset($result); 
     unset($select_owner); 
     unset($conn); 
    } 
    catch (PDOException $e) 
    { 
     echo 'Error: '.$e->getMessage(); 
    } //end catch 

} 
+0

ありがとう、もう1つ質問があります。ユーザーが[送信]ボタンをクリックしたときにのみ、同じページに結果を表示するにはどうすればよいですか?上記の変更を自分のコードで行い、if(isset)パーツを追加しましたが、フォームにデータを入力してsubmitをクリックすると、追加されたユーザーとmySQLテーブルの他の所有者のテーブルは表示されません。 – user3325133

1

のような変数何かにコードが私には正常に見えるが、データを挿入しているときは、最初にフォームデータをキャッチする必要があり、その後、それらの変数を使用しvariables.Thenに保存挿入クエリの内部にあります。

if (isset($_POST['submit1']))//set your submit btn name to submit1 
{  
     $username=$_POST['OwnerNum'];//likewise catch all data 

    try { 
     $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
    (?,?,?.....)";//put the placeholders here.the num of placeholders should be equal to number of bound values 
+0

私はmysqliをここで使用していません... pdo ....とよく似ているコードのちょっと離れていて、常にパラメータをバインドする必要はありません...プレースホルダ(?)はパラメータがバインドされていないときに使用されます...それをphpマニュアルで確認することができます。 –

関連する問題