I want to get user-input by HTML form input field and then send this input into the SQL query. I have tried the method I googled but there's no value selected. The SQL query worked when I gave the value directly in the SQL query.
<body>
<div class="testbox">
<form action="train.php" method="POST">
<div class="item">
<div class="train-item">
<p>Departure Station </p>
<input type="text" name="depart" required />
</div>
<div class="train-item">
<p>Arrival Station</p>
<input type="text" name="arrive" required />
</div>
<div class="btn-block">
<button type="submit" name="search" href="/">Search</button>
</div>
</div>
</form>
</div>
<?php
if(isset($_POST['search'])){ // Fetching variables of the form which travels in URL
$depart = $_POST["depart"];
$arrive = $_POST["arrive"];
$sql = "SELECT ttype.TyName, train.TraNo, a.StaName dsta, c.Time dtime ,b.StaName asta , d.Time atime
FROM (((((ttype RIGHT JOIN train
ON ttype.TyNo=train.TyNo)
RIGHT JOIN pass c
ON train.TraNo=c.TraNo)
RIGHT JOIN station a
ON a.StaNo=c.StaNo)
RIGHT JOIN pass d
ON train.TraNo=d.TraNo)
RIGHT JOIN station b
ON b.StaNo=d.StaNo)
WHERE c.Time < d.Time
AND a.StaName='.$depart'
AND b.StaName='.$arrive' ";
$result = mysqli_query($link, $sql) or die("can't reach" . mysqli_error( ));
$rows = mysqli_num_rows($result);
$cols = mysqli_num_fields( $result);
$train_table = "";
$train_table .= "This query has ". $rows ." data";
$train_table .= ",and include". $cols ."columns";
?>
The above code showed "This query has 0 data, and includes 6 columns.
EDIT: Thanks for helping and informing me about the SQL injection. It's for a term-project for a course at university. The result will only be shown to my classmates and professor and I am the only one who will be access to the whole system. Thanks again.
The reason I posted my own question is that I am not familiar with PHP nor HTTP language and couldn't find the exact problem with my code. In other words, I am not sure the problem is located in the HTTP part or the PHP part.