Reputation: 51
I'm trying to get my button to work. It works with $_SERVER["REQUEST_METHOD"] == "POST"
but not with isset($_POST['submit'])
. The problem with $_SERVER["REQUEST_METHOD"] == "POST"
is that it gives me multiple entries.
My code:
if(isset($_POST['submit']))
{
$server_address = $_POST['submitServerIp'];
$server_port = $_POST['submitServerPort'];
$Query = new SourceQuery( );
try
{
$Query->Connect( $server_address, $server_port, SQ_TIMEOUT, SQ_ENGINE );
$data = $Query->GetInfo( );
}
catch( Exception $e )
{
echo $e->getMessage( );
$error = true;
}
$Query->Disconnect( );
if($error == false)
{
$errorMessage = "false";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$stmt = $conn->prepare("INSERT INTO Servers (ipaddress, port) VALUES (?, ?)");
$stmt->bind_param("ss", $ipaddress, $port);
$ipaddress = $_POST['submitServerIp'];
$port = $_POST['submitServerPort'];
$stmt->execute();
$stmt->execute();
$stmt->close();
mysqli_close($conn);
}
}
?>
HTML:
<form method="post" action="">
<div class="container-fluid search-top">
<div class="addContainer">
<div class="addContainer col-sm-12">
<p class="searchText"><?php echo htmlspecialchars($errorMessage);?></p>
<div class="container col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12 ">
<div class="input-group stylish-input-group">
<input type="text" name="submitServerIp" class="form-control input-lg" placeholder="Hostname/IP" style="border:0;">
<input type="text" name="submitServerPort" class="form-control input-lg" placeholder="Port" style="border:0;">
<span class="input-group-addon">
<button class="btn btn-primary" type="submit" value="submit">
<span class="fas fa-plus" aria-hidden="true"></span>
</button>
</span>
</div>
</div>
</div>
</div>
Can I not use the button the way I use it or am I doing something else wrong?
Upvotes: 0
Views: 97
Reputation: 8621
When you use a $_POST[KEY]
variable, the key is actually the name
of the form element that you submitted, not the value
.
To solve your issue, simply add name="submit"
to your submit button.
<button class="btn btn-primary" type="submit" name="submit" value="submit">
<span class="fas fa-plus" aria-hidden="true"></span>
</button>
As for the 2 entries in the database, you have an extra execute()
function being called.
$stmt = $conn->prepare("INSERT INTO Servers (ipaddress, port) VALUES (?, ?)");
$stmt->bind_param("ss", $ipaddress, $port);
$ipaddress = $_POST['submitServerIp'];
$port = $_POST['submitServerPort'];
$stmt->execute();
$stmt->execute();
$stmt->close();
mysqli_close($conn);
You should remove one of the above lines that say $stmt->execute();
.
Upvotes: 3