0

enter image description here

I have code to retrieve data from a database into a form but it doesnt seem to be working. The code below is my attempt but it doesnt work. Currently, when I click the submit button 'retrieve rose' it does nothing...

//if we have no errors, do the SQL
if (!$errors) {   

$latin_name = $_POST['latin_name'];

$stmt = $conn2->prepare("SELECT common_name, variety_name, colour, season_of_interest, hardiness, situation, soil_type,
price, stock_level, fragrance, ultimate_height FROM rosename WHERE latin_name = ?");

$stmt->bind_param('ssssssssdiss', $latin_name);

if ($result = $stmt->get_result()) {
    /* fetch associative array */
    echo "<form><input type='text' value='" . $row["common_name"] . "' name='latin_name' />";
    echo "<input type='text' value='" . $row["variety_name"] . "' name='soil_type' /></form>";
    } // i no I need to add more here...
    exit;
}

//put out the footer and then stop the rest of the script from running, so we don't display the rest of the form (this is after the form has been submitted)
require_once('footer.php');
exit;
}

//if we do have errors, show the error message
else {
  echo "<p>".$error_msg."</p>";
}}
?>

And here is my form:

<h1>Update Rose Item</h1>
  <ul class='register'>
      <li>
  <form action="updaterose.php" id="updaterose" method="post">
  <fieldset id="register">
        <label>Latin Name:<span class="small">Enter a Latin Name</span></label><input      name='latin_name' id='latin_name' type='text' value="<?=(isset($_POST['latin_name'])?  $_POST['latin_name']:"");?>" />
    <input type="submit" value="Retrieve Rose" name='retrieverose' /></br></br></br>
</form>

Code requested by mariogl

  //connect to database
$conn2 = DB2();

  require_once('header_admin.php');

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

//detect if we have errors or not
$errors = false;
$error_msg = "Error, please try again";
5
  • bind_param('ssssssssdiss', $latin_name); looks VERY strange. what are you trying to do here? Commented Feb 28, 2012 at 10:02
  • This bracket doesn't match with any: } // i no I need to add more here... Commented Feb 28, 2012 at 10:03
  • bind_param('ssssssssdiss', $latin_name); is using prepared statements, do you know about prepared statements? Commented Feb 28, 2012 at 10:19
  • I haven't used mysqli, but seems like you should only pass one parameter (as you have only one question mark), and you're passing 12. Commented Feb 28, 2012 at 10:37
  • When you click the submit button, it does nothing or does it load the page and then does nothing? Commented Feb 28, 2012 at 10:39

1 Answer 1

1

Your problem is the first condition, you're asking for a variable named "updaterose", that doesn't exist. Try this:

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

    //detect if we have errors or not
    $errors = false;
    $error_msg = "Error, please try again";

    //if we have no errors, do the SQL
    if (!$errors) {   

        $latin_name = $_POST['latin_name'];

        $stmt = $conn2->prepare("SELECT common_name, variety_name, colour, season_of_interest, hardiness, situation, soil_type, price, stock_level, fragrance, ultimate_height FROM rosename WHERE latin_name = ?");

        $stmt->bind_param('s', $latin_name);
        $stmt->execute();

        if ($result = $stmt->get_result()) {
            /* fetch associative array */
            echo "<form><input type='text' value='" . $result["common_name"] . "' name='common_name' />";
            echo "<input type='text' value='" . $result["variety_name"] . "' name='variety_name' /></form>";
            // i no I need to add more here..
            exit;
        }

        //put out the footer and then stop the rest of the script from running, so we don't display the rest of the form (this is after the form has been submitted)
        require_once('footer.php');
        exit;
    }

    //if we do have errors, show the error message
    else {
        echo "<p>".$error_msg."</p>";
    }}

}

Corrections on brackets and bind_param().

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for those corrections. Even though those corrections are correct, it still does nothing after I click the submit button.
Does it load the page at least?
yes the page loads fine and shows the input for latin name. But when you enter in a latin name and click the submit button to retrieve the record, it does nothing, just refreshes the same page
Maybe has something to do with other pieces of code that you haven't post. This piece of php is in the same file that your form? Could you post the php code previous to if (!$errors) {?
lol were getting there... When I click the submit button now, the form disappears and just shows the green background. I want it to display each field with the retrieved record values for each column
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.