I need to pass the value of a drop down list to an already existing table, I've tried a simple MySql query which works properly in the MySQL SQL console.
Here is the query which I need to run: update Trial set Status = 'Test Status' where id = '123'
When I try to run this query it runs into variable issues as it's unavailable to get the value of ID from the row.
Below are two Queries, the first one fetches all the data, and the second which is not proper is the one which needs to update the value of the drop down list to the already existing table where the ID mathches. Any help is appreciated.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cotactform";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, email, phone, Status,Url, datetime FROM Trial";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr style='text-align:center;'><td>" . $row["id"]. "</td><td>" . $row["name"] . "</td><td>" . $row["email"]. "</td><td>". $row["phone"]. "</td><td>". $row["Url"]. "</td><td>". $row["datetime"]. "</td><form method='get' action='displaydata.php'><td><select name='Status'><option>--Select Status--</option><option value='valid'>Valid</option><option value='invalid'>Invalid</option><option value='followup'>FollowUp</option></select><button type='submit' name='submit'>Submit</button></form></td> <td>". $row["Status"]."</td></tr>";
}
} else {
echo "0 results";
}
if(isset($_GET['submit']))
{
$Status=$_GET['Status'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// The query to update the Status
$ins="update Trial set Status = '$Status' where id = 'id'";
if($ins)
{
echo "<br>".$Status."inserted";
}
else
{
echo mysql_error();
}
}
$conn->close();
?>
Edit Updated for adding the hidden field:
while($row = $result->fetch_assoc()) {
echo "<tr style='text-align:center;'><td>" . $row["id"]. "</td><td>" . $row["name"] . "</td><td>" . $row["email"]. "</td><td>". $row["phone"]. "</td><td>". $row["Url"]. "</td><td>". $row["datetime"]. "</td><form method='get' action='displaydata.php'><td><select type='hidden' name=".$row["id"]."><option>--Select Status--</option><option value='valid'>Valid</option><option value='invalid'>Invalid</option><option value='followup'>FollowUp</option></select><button type='submit' name='submit'>Submit</button></form></td> <td>". $row["Status"]."</td></tr>";
}
Second query check:
if(isset($_GET['submit']))
{
$Status=$_GET['Status'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$id=$_GET['id'];
//$Status=$_GET['Status'];
$ins = "update Trial set Status = '" . $Status ."' where id = '".$id."'";
$run = $conn->query($ins);
if($ins)
{
echo "<br>".$Status."inserted";
}
else
{
echo mysql_error();
}
}