0

I collect data using client form and purchase form and then store it on db_clientData.php and db_purchaseData.php where I write it on the database tables clients and purchases.

Then on the db_salesReport.php file I access these two table but it does not show the new stored information but only the old rows that I imputed manually (not by using a form).

Why does it not show the new information?
Can someone tell me what am I missing?

New client form:

<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" />

Purchase form:

    <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>

Storing new client:

<?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>

Storing new purchase:

<?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>

Calling data on 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>
Rudie
  • 52,220
  • 42
  • 131
  • 173
  • So do you want us to complete a coding project for you from scratch? http://stackoverflow.com/questions/23595390/storing-data-from-form-into-a-database/23595441#comment36219197_23595441 – Giacomo1968 May 11 '14 at 21:02
  • a little debugging on your part would go a long way: http://stackoverflow.com/help/mcve –  May 11 '14 at 21:03
  • Hmm O'Brien, who lives in 's-Hertogenbosch would love to be your client. What problem will you have? – VMai May 11 '14 at 21:08
  • JakeGould what those guys help me gave me a push to almost complete my project. All I am left now is with this problem as I tried everything I know but is not working. Thus, I thought some suggestions might be helpful. – user3355976 May 11 '14 at 21:09

1 Answers1

0

It's because your query to fetch data assumes there is a relationship between clients and purchases:

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

However I do not see you adding anything about book_id in either your client or purchases forms.

Additionally please ensure your data is sanitized so that you do not fall victim to mysql injections.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • I used this query: $query = "SELECT * from clients NATURAL JOIN purchases"; and this tutorial to sanitize: http://element-80.com/2010/11/php-tutorial-how-to-sanitize-form-data/ But it is not working. – user3355976 May 11 '14 at 21:36
  • it doesn't matter - you are not storing relations - you need to add something such as the client ID when storing each purchases else there is no way to know which purchase goes with which customer. How you do it depends on your scenario but in your purchase form/query there needs to be a field for the client ID which would be the mysql unique ID field of the clients table. Then you can join. You can obtain the ID of the client just after insert with last_insert_id() and use that to add to your purchase query somehow. – cyberwombat May 11 '14 at 21:52