将数据库中的数据检索为html表单

时间:2012-02-28 09:52:00

标签: php html sql prepared-statement

enter image description here

我有代码将数据从数据库检索到一个表单但它似乎没有工作。下面的代码是我的尝试,但它不起作用。目前,当我点击提交按钮'检索玫瑰'时,它什么也没做......

//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>";
}}
?>

这是我的表格:

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

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

1 个答案:

答案 0 :(得分:1)

你的问题是第一个条件,你要求一个名为“updaterose”的变量,它不存在。试试这个:

  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>";
    }}

}

括号和bind_param()的更正。