Reputation: 922
I'm looking for the best way to input data into different databases. Right now I've got a page with a drop down menu that asks what database they want to enter into, then that page takes you to a form page that has where you can type information. The problem I'm having is that the if statement is not working correctly.
<form action="test.php" method="GET">
<select name="dropdown">
<option value="a">Database1</option>
<option value="b">Database2</option>
<input type="submit">
</select>
</form>
<?php
if(isset($_GET['dropdown']) && $_GET['dropdown'] === 'a'){
include 'test.php';
} else if($_GET['dropdown'] === 'b'){
include 'Test2.php';
}
?>
Regardless of whether I click Database1
or Database2
I still land on the first test page. Now, I'm assuming this is because the action of the drop down is the first test.php
page, but I'm not sure what I need to change that to in order to incorporate all 3 options.
I feel like even if I get this to work, there has got to be a more efficient way to do this. Maybe a single page where I can have the user choose from the drop down menu, and then whatever data he enters will be sent to the correct data base.
Upvotes: 0
Views: 75
Reputation: 74219
You have edited your question and your original code is not showing.
As per your original post
The reason why your code is failing is that you're not checking to see if anything is set or is equal to the value passed, nor are you assigning any GET variables.
You also need an else
if your 3rd option is chosen or use a 3rd conditional i.e.: if($_GET['dropdown'] === 'c'){...}
if(isset($_GET['dropdown']) && $_GET['dropdown'] === 'a') {
include 'test.php';
// echo "A chosen";
} else if($_GET['dropdown'] === 'b'){
include 'Test2.php';
// echo "B other";
}
else if($_GET['dropdown'] === 'c'){...}
echo "Other, C chosen";
}
Sidenote: I noticed that you are using mixed case for your files.
test.php
and Test2.php
- on Linux systems, test2.php
and Test2.php
are not the same thing should this be a contributing factor to possible errors you may be getting.
Add error reporting to the top of your file(s) right after your opening <?php
tag
error_reporting(E_ALL); ini_set('display_errors', 1);
see if it yields anything.
If your entire code is inside the same page, use action=""
instead of action="test.php"
which is what I did to test my code here, which I suspect may be the issue or a contributing one.
Upvotes: 1
Reputation: 2056
yeah, yes use new version of PHP and MYSQLi function to insert input in different databases
$db=new mysqli('localhost','root','');
if($db->connect_errno > 0 )
{
die("Unable to connect data base");
}
if(isset($_GET["dropdown"]) && $_GET["dropdown"]=="a")
{
$sql="insert into database1.tblTest(name) values('".$_GET["txtName"]."')";
} else if(isset($_GET["dropdown"]) && $_GET["dropdown"]=="b"){
$sql="insert into database2.tblTest(name) values('".$_GET["txtName"]."')";
}
if(!$result=$db->query($sql))
{
die("there was an error: ".$db->error);
}
else
{
echo "Saved...";
}
to use new version my PHP and MYSQLi function there is two pros 1. you can insert input in your select database 2. And MYSQLi function is Improved function of Mysql it protects from SQL Injection
thank you...
Upvotes: 0
Reputation: 6252
I think this is what you want ..
if(isset($_GET['dropdown']) && $_GET['dropdown'] === 'a') {
// the rest of ur code
}
Upvotes: 1