【问题标题】:Accessing new stored data on the database访问数据库中新存储的数据
【发布时间】:2014-05-11 21:00:42
【问题描述】:

我使用客户表格和采购表格收集数据,然后将其存储在db_clientData.phpdb_purchaseData.php 上,我将其写入数据库表客户和采购。

然后在db_salesReport.php 文件上,我访问这两个表,但它不显示新存储的信息,而只显示我手动估算的旧行(不是通过使用表单)。

为什么不显示新信息?
谁能告诉我我错过了什么?

新客户表格:

<form  action="db_clientData.php" method="post">
First Name: <input type='text' name='client_fname' /><br />
Last Name: <input type='text' name='client_lname' /><br />
City: <select name='client_city'>
    <option>Prishtine</option>
    <option>Mitrovice</option>
    <option>Peje</option>
    <option>Gjakove</option>
        <option>Ferizaj</option>
        <option>Prizren</option>
</select><br />
Gender: <select name='client_sex'>
    <option>F</option>
    <option>M</option>
</select><br />
Username(3-10 characters): <input type='text' name='client_username' /><br />
Password(3-10 characters): <input type='password' name='client_pass' /><br />
<input type='submit' value='Submit' />
<input type="reset" value="Clear" />

购买表格:

    <form  action="db_purchaseData.php" method="post">
Book: <select name='purchase_book'>
    <option>Darka e gabuar</option>
    <option>Populli i ndalur</option>
    <option>Bageti e Bujqesi</option>
    <option>Fjala gdhend gurin</option>
        <option>Shtiegje Poetike</option>
        <option>Bashkohesit</option>
        <option>Colored Water</option>
        <option>Selected Poems</option>
        <option>Olivion Favorites</option>
</select><br />
Amount: <input type='number' name='purchase_amount' /><br />

<input type='submit' value='Submit' />
<input type="reset" value="Clear" />
</form>

存储新客户:

<?php
        include('db_login.php');
            // Connect
    $connection = mysql_connect($db_host, $db_username, $db_password);
        if (!$connection){
            die("Could not connect to the database: <br />". mysql_error( ));
        }
            // Select the database
    $db_select = mysql_select_db($db_database);
        if (!$db_select){
            die ("Could not select the database: <br />". mysql_error( ));
        }


$fname = isset($_POST['client_fname']) ? $_POST['client_fname'] : null;
$lname = isset($_POST['client_lname']) ? $_POST['client_lname'] : null;
$city = isset ($_POST['client_city']) ? $_POST['client_city'] : null;
$sex = isset($_POST['client_sex']) ? $_POST['client_sex'] : null;
$username = isset ($_POST['client_username']) ? $_POST['client_username'] : null;
$pass = isset ($_POST['client_pass']) ? $_POST['client_pass'] : null;


$sql = "INSERT INTO clients (client_fname, client_lname, client_city, client_sex, client_username, client_pass) VALUES ('$fname','$lname','$city','$sex','$username',MD5('$pass'))";
mysql_query($sql, $connection);  


mysql_close();

echo "Data stored on database."; 

?>

<a href="db_testAuth.php"><br><input type='button' value='Log In'></a>

存储新购买:

<?php
        include('db_login.php');
            // Connect
    $connection = mysql_connect($db_host, $db_username, $db_password);
        if (!$connection){
            die("Could not connect to the database: <br />". mysql_error( ));
        }
            // Select the database
    $db_select = mysql_select_db($db_database);
        if (!$db_select){
            die ("Could not select the database: <br />". mysql_error( ));
        }


$bookname = isset($_POST['purchase_book']) ? $_POST['purchase_book'] : null;
$bookamount = isset($_POST['purchase_amount']) ? $_POST['purchase_amount'] : null;


$sql = "INSERT INTO purchases (purchase_book, purchase_amount) VALUES ('$bookname','$bookamount')";
mysql_query($sql, $connection);  


mysql_close();

echo "Data stored on database."; 

?>

<a href="db_salesReport.php"><br><input type='button' value='Sales Report'></a>

调用 db_salesReport.php 上的数据:

<body>
<p>Sales Report</p>
<table border="2">
    <tr>
        <th>Client ID</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Username</th>
        <th>Purchase ID</th>
    <th>Book title</th>
    <th>Amount</th>

    </tr>
   <?php
            //Include our login information
    include('db_login.php');
            // Connect
    $connection = mysql_connect($db_host, $db_username, $db_password);
        if (!$connection){
            die("Could not connect to the database: <br />". mysql_error( ));
        }
            // Select the database
    $db_select = mysql_select_db($db_database);
        if (!$db_select){
            die ("Could not select the database: <br />". mysql_error( ));
        }
            // Assign the query
    $query = "SELECT clients.client_id, clients.client_fname, clients.client_lname, clients.client_username, purchases.purchase_id, purchases.purchase_book, purchases.purchase_amount from clients,purchases where clients.book_id=purchases.book_id;";
            // Execute the query
        $result = mysql_query($query);
        if (!$result){
            die ("Could not query the database: <br />". mysql_error( ));
        }
            // Fetch and display the results
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $client_id = $row["client_id"];
        $client_fname = $row["client_fname"];
        $client_lname = $row["client_lname"];
        $client_username = $row["client_username"];
    $purchase_id = $row["purchase_id"];
        $purchase_book = $row["purchase_book"];
    $purchase_amount = $row["purchase_amount"];
            echo "<tr>";
            echo "<td>$client_id</td>";
                        echo "<td>$client_fname</td>";
            echo "<td>$client_lname</td>";
            echo "<td>$client_username</td>";
            echo "<td>$purchase_id</td>";
                        echo "<td>$purchase_book</td>";
            echo "<td>$purchase_amount</td>";
            echo "</tr>";
        }
                // Close the connection
    mysql_close($connection);
   ?>
</body>
</html>

【问题讨论】:

  • 那么您希望我们从头开始为您完成一个编码项目吗? stackoverflow.com/questions/23595390/…
  • 稍作调试会大有帮助:stackoverflow.com/help/mcve
  • Hmm O'Brien,住在 's-Hertogenbosch 很想成为您的客户。你会有什么问题?
  • JakeGould 那些人帮助我的东西给了我一个推动我几乎完成我的项目的动力。我现在剩下的就是这个问题,因为我尝试了我所知道的一切但没有工作。因此,我认为一些建议可能会有所帮助。

标签: php mysql database


【解决方案1】:

这是因为您获取数据的查询假设客户和购买之间存在关系:

from clients,purchases where clients.book_id=purchases.book_id

但是,我没有看到您在客户或购买表单中添加任何关于 book_id 的内容。

另外,请确保您的数据已经过清理,以免您成为 mysql 注入的受害者。

【讨论】:

  • 我使用了这个查询:$query = "SELECT * from clients NATURAL JOIN purchase";和本教程进行消毒:element-80.com/2010/11/php-tutorial-how-to-sanitize-form-data 但它不起作用。
  • 没关系-您不存储关系-您需要在存储每次购买时添加诸如客户ID之类的内容,否则无法知道哪个购买与哪个客户一起使用。你如何做取决于你的场景,但在你的购买表单/查询中需要有一个客户 ID 字段,这将是客户表的 mysql 唯一 ID 字段。然后就可以加入了。您可以在插入后使用 last_insert_id() 获取客户的 ID,并使用它以某种方式添加到您的购买查询中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 2020-07-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多