2017-12-29 28 views
0

どのように私は入力から値を取ることができますSQL文に追加するときにページがPHPでリロード?

<div> 
    <label>From</label> 
    <input type=date name=from value="12-12-2017"> 
    <label>To</label> 
    <input type=date name=to value="29-12-2017"> 
</div> 


$query = $conn->prepare("SELECT * FROM Users WHERE age BETWEEN **From** and **To**") 
$query->execute() 

、どのように入力から値を取得し、ページが最初にリロードしたときに、SQL文に追加することができますか? ありがとうございます

+0

日付をvarcharとして保存しないでください。 –

答えて

1

これは初回のみに送信されるコードの実例です。

PHP/MySQLiにのみタグが設定されているので、日付の入力が完了したらボタンを押す必要がある例を紹介します。

<?php 
// session_start() has to be the first line of code 
session_start(); 

$con = new mysqli($servername, $username, $password, $dbname); 

// if there is NOT a valid POST request (means it is like a first visit) 
if (!$_POST) 
{ 
    // if the session variable is set unset it 
    if(isset($_SESSION['beenHere'])) unset($_SESSION['beenHere']); 

    echo 'Status: This is a fresh start.<br/>'; 
} 
else if(isset($_POST['from']) && isset($_POST['to']) && 
    !empty($_POST['from']) && !empty($_POST['to']) && 
    !isset($_SESSION['beenHere'])) 
{ 
    // set the session variable to an arbitrary value, it's important it is set 
    $_SESSION['beenHere'] = 'This is the first submit'; 
    // prepare the statement 
    $stmt = $con->prepare("SELECT * FROM Users WHERE age > ? AND age < ?"); 
    // turn the input variables into proper date format and bind the to the ? 
    // dates are considered strings in prepared statements 
    $stmt->bind_param("ss", date('Y-m-d',strtotime($_POST['from'])), date('Y-m-d',strtotime($_POST['to']))); 
    $stmt->execute(); 

    // Here you list all the output columns that you have. I only listed two example columns. 
    $stmt->bind_result($name, $age); 
    // loop for each returned row 
    while ($stmt->fetch()) 
    { 
     // print the results 
     printf ("%s %s\n", $name, $age); 
    } 
} 
?> 

<!-- Leave the action empty to POST to self --> 
<form action="" method="POST"> 
    <div> 
    <label>From</label><input type="date" name="from" value="" /> 
    <label>To</label><input type="date" name="to" value="" /> 
    </div> 
    <input type="submit" value="clickToEnter" /> 
</form> 

注:より高度な例についてあなたはセッション変数のクライアント側を設定できるようにjavascript/jQueryに頼ることができます。あなたは$_SESSIONに貴重な情報を保存し、ページの読み込みをチェックできるので、POSTは必要ありません。

関連する問題