Insert values into database from form with drop do

2019-08-23 11:41发布

I am trying to insert values input from the user in a form into my database.

I have 2 drop down lists for a blood test and then category. E.g user first selects drop down 1 'Thyroid' (Category) and then drop down 2 displays 'FT4,FT3 TSH' (blood test) etc and the user makes their selection.

Then they input the date and the value.

In my insert I need to insert into my database the user_id(established after login with session variables, bloodtest_id(from drop down 2), date, value.

I can't get my SQL query right for the insert in general and need some help please. Drop down 2 (blood test) is in a seperate php file and I'm not sure how to tell the SQL query how to find that value to use either?

The addBlood.php is below. This is the page the form is on

  <?php
session_start();

include('dbConnect.php');

$queryStr=("SELECT * FROM category");
    $dbParams=array();
  // now send the query
  $results  = $db->prepare($queryStr);
  $results->execute($dbParams);

?>
<html>
<head>
<TITLE>Category and Test</TITLE>
<head>
<!-- Help for code to create dynamic drop downs -->
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function getTest(val) {
    $.ajax({
    type: "POST",
    url: "get_test.php",
    data:'category_id='+val,
    success: function(data){
        $("#test-list").html(data);
    }
    });
}

function selectCategory(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<div class="frmDronpDown">
<div class="row">
<label>Category:</label><br/>
<select name="category" id="category-list" class="demoInputBox" onChange="getTest(this.value);">
<option value="">Select Category</option>
<?php
foreach($results as $category) {
?>
<option value="<?php echo $category["category_id"]; ?>"><?php echo $category["category_name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<form action="addBlood.php" method="post">
<label>Test:</label><br/>
<select name="test" id="test-list" class="demoInputBox">
<option value="">Select Test</option>
</select>
</div>
</div>

<label>Result:</label><input class="input" name="result" type="text"><br>
<label>Date:</label><input class="input" name="date" type="date"><br>
<input class="submit" name="submit" type="submit" value="Submit">
</form>

<?php

if(isset($_POST['submit'])){
    //$currentBloodTest=$test["bloodtest_id"];
    $currentUser=$_SESSION["currentUserID"];

    $value = $_POST['result'];
    $date = $_POST['date'];

        //$query = $db->prepare("INSERT INTO results (user_id, bloodtest_id,date,value)values ($currentUser,$currentBloodTest, $date , $value)");
        $dbParams = array();
        $query->execute($dbParams);
    echo "<br><br><span>Data Inserted successfully...!!</span>";


}

?>

</body>
</html>

Below is the getTest.php which gets all the blood tests.

  <?php
include('dbConnect.php');


if(!empty($_POST["category_id"])) {
    $queryStr=("SELECT * FROM bloodtests WHERE category_id = '" . $_POST["category_id"] . "'");
    $dbParams=array();
  // now send the query
  $results  = $db->prepare($queryStr);
  $results->execute($dbParams);

?>
    <option value="">Select Test</option>
<?php
    foreach($results as $test) {
?>
    <option value="<?php echo $test["bloodtest_id"]; ?>"><?php echo $test["test_name"]; ?></option>
<?php
    }
}
?>

1条回答
Rolldiameter
2楼-- · 2019-08-23 12:36

If this is where you are having some troubles, and are unsure how to proceed:

//$query = $db->prepare("INSERT INTO results (user_id, bloodtest_id,date,value)
//                       VALUES ($currentUser,$currentBloodTest, $date , $value)");
$dbParams = array();
$query->execute($dbParams);

Then this may help you out (you were very close):

$currentUser      = $_SESSION["currentUserID"];
$currentBloodTest = $_POST['test']; // name of the 'select' element
$value            = $_POST['result'];
$date             = $_POST['date'];

$query = $db->prepare("INSERT INTO results (user_id, bloodtest_id, date, value)
                       VALUES (?, ?, ?, ?)");
$query->execute( array($currentUser, $currentBloodTest, $date, $value) );

Also, this part you have in the ajax lookup:

$queryStr=("SELECT * FROM bloodtests WHERE category_id = '" . $_POST["category_id"] . "'");
$dbParams=array();
// now send the query
$results  = $db->prepare($queryStr);
$results->execute($dbParams);

Really should be written as:

$queryStr = "SELECT * FROM bloodtests WHERE category_id = ?";
$results  = $db->prepare($queryStr);
$results->execute( array($_POST["category_id"]) );
查看更多
登录 后发表回答