0

I have a form that allows me to input data into a database. When the information in the form is submitted it is also returned to let the user know the data has been successfully uploaded. At the moment all of the data in the database is being returned. Ideally I would like only the data being submitted at the time to be returned (which should return one result). Is there a mysql query that will allow this? This is what I currently have:

    $query = "SELECT * FROM residents";

    $result = mysql_query($query);

    while($person = mysql_fetch_array($result)) {
        echo "<h3>" . $person['FirstName'] . "</h3>";
        echo "<p>" . $person['LastName'] . "</p>"; 
        echo "<p>" . $person['Address'] . "</p>";
        echo "<p>" . $person['Birthday'] . "</p>"; 
        echo "<p>" . $person['FormerResidence'] . "</p>";
        echo "<p>" . $person['Career'] . "</p>";
        echo "<p>" . $person['Education'] . "</p>";
        echo "<p>" . $person['SpecialInterests'] . "</p>";
        echo '<p><img src="upload/' . $person['Picture'].'" width="70" height="70"/></p>';
    }
    ?>
    <?php

I have also tried: while($person = mysql_fetch_assoc($result)) which still returns all of the data in the database instead of just one result.

IGrowBeards
  • 75
  • 13

2 Answers2

2

Why not just take it from the $_POST or $_GET array that you are loading it into when you submit the form?

Example:

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>">
    <input type="text" name="FirstName">
    <input type="text" name="LastName">
    etc...
    <input type="submit" name="form_btn">
</form>

And then in your PHP, you could echo out the $_POST array variables you acquired when you submitted the form:

<?php
    //You can put your code that inserts all the form values into the database here.
    //And then you can echo out the form data:

    if (isset($_POST['form_btn'])) {
        echo "<h3>" . $_POST['FirstName'] . "</h3>";
        echo "<p>" . $_POST['LastName'] . "</h3>";
    }

?>

Also, it would be a good idea to hop on the mysqli train. The original mysql functions were deprecated.

Graham S.
  • 163
  • 2
  • 9
1

You're about to get an influx of responses that say this, but you should be using a non-deprecated database method like mysqli. One of the advantages that this will also bring you is the ability to get the insert ID back from the last query by using ->insertid. Then, you can use this to query the database for that exact ID, guaranteeing the correct entry.

WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21