使用选择列表显示提交的数据以显示数据列

时间:2017-04-14 15:57:41

标签: php mysqli

使用PHP& MySQLi:我正在尝试使用选择列表,当用户使用PHP点击提交按钮时,我试图在用户选择特定选择时显示该列数据:

//database set up information to connect to SQLi:     $servername = "*******";     $username = "********";     $password = "******";     $dbname = "********";

`// Create connection`
`$conn = new mysqli($servername, $username, $password, $dbname);`

`// Check connection`
`if ($conn->connect_error) {`
`die("Connection failed: " . $conn->connect_error);
}`

`//To get value of a selected option from select tag:
if(isset($_POST['submit'])){
$selected_val = $_POST['selection'];  // Storing Selected Value In Variable
echo " You have selected :" .$selected_val;  // Displaying Selected Value
}`

`//a string was submitted:`
`$selection = $_POST['selection'];`
`$selection = intval($_POST['selection']);`

`//selecting data from database:`
`$sql = "SELECT * FROM teams WHERE teamname = '$selection'";
$sql = "SELECT * FROM teams WHERE city = '$selection'";
$sql = "SELECT * FROM teams WHERE bestplayer = '$selection'";
$sql = "SELECT * FROM teams WHERE yearformed = $selection";
$sql = "SELECT * FROM teams WHERE website = '$selection'";`


`$conn->close();
?>`

`<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<select name="selection">
<option value="teamname">Team Name</option>
<option value="city">City</option>
<option value="bestplayer">Best Player</option>
<option value="yearformed">Year Formed</option>
<option value="website">Website</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>`

0 个答案:

没有答案