Reputation: 83
Statement
I would like to get the value from the dropdown in main.php file into MySQL server using PHP file.
Condition
If the user didn't select any choice or a select default choice <option value="0">(Select Animal)</option>
where option value = 0.
Then if the option value = 0, the value from dropdown should not be inserted into the MySQL server.
So, what should I need to add into add.php?
main.php
<html>
<?php
include 'connection.php';
require_once 'add.php';
?>
<body>
<form method="post" action="add.php">
<select id="animal" name="animal">
<option value="0">(Select Animal)</option> <!--Non select option-->
<option value="Cat">Cat</option>
<option value="Dog">Dog</option>
<option value="Cow">Cow</option>
</select>
<button type="submit" name="Save">Save</button > <!--Send the selected value to add.php-->
</form>
</body>
</html>
Another PHP file (add.php)
<?php
include 'connection.php';
if(isset($_POST['Save']))
{
$animal = mysqli_real_escape_string($conn,$_POST['animal']; );
$conn->query("INSERT INTO animal (name) VALUES(?)");
header("location: main.php");
}
?>
Upvotes: 0
Views: 351
Reputation: 83
From @sony's answer, I change if($_POST['animal']!= 0)
to if($_POST['animal']!= '0')
make it works.
Change
<?php
include 'connection.php';
if(isset($_POST['Save']))
{
if($_POST['animal']!= '0') //2 Single quotes are added.
{
$animal = mysqli_real_escape_string($conn,$_POST['animal']);
$sql="INSERT INTO animal (name) VALUES(?) ";
header("location: main.php");
}else{
header("location: main.php");
}
}
?>
Upvotes: 0
Reputation: 1304
Make sure you know the array key exists before checking if it is set or you may get an error/warning.
Then ensure that the animal value is greater than 0... and include it in your query.
<?php
include 'connection.php';
if(array_key_exists('Save', $_POST)) {
if(array_key_exists('animal', $_POST) && $_POST['animal'] > 0) {
$animal = mysqli_real_escape_string($conn, $_POST['animal']);
//Prepare statement
$conn->prepare("INSERT INTO animal (name) VALUES (?)");
//Include animal value in string query
$conn->bind_param("s", $animal);
//Run query
$conn->query($sql);
header("location: main.php");
} else {
//Fail gracefully
header("location: error.php"); //Or whereever you want to redirect to.
}
}
?>
Bare in mind, you are not actually running the query here.
You're just creating it as a string, nor are you adding the value of animal to it.
Also, remove that semicolon after $_POST['animal']
when doing mysqli_real_escape_string()
.
EDIT: I've updated mine with your update.
EDIT 2: I have also added what you need to correctly prepare the statement.
Upvotes: 1
Reputation:
you can use Javascript validation for the dropdown or Keep a condition for $_POST of animal like below:
<?php
include 'connection.php';
if(isset($_POST['Save']))
{
if($_POST['animal']!=0){
$animal = mysqli_real_escape_string($conn,$_POST['animal']);
$sql="INSERT INTO animal (name) VALUES(?) ";
header("location: main.php");
}else{
header("location: main.php");
}
}
?>
Upvotes: 0