Php MySQLi插入到If中

Php MySQLi插入到If中,php,mysql,mysqli,Php,Mysql,Mysqli,我遇到的问题是,下面的PHP代码只将数据插入一个表中。我想要的是,如果文章中的目录类别等于,例如,blueinsert-into-Table-blue,但是如果它等于yellow-INSERT-into-yellow,但是如果它等于red-INSERT-into-Table-red 我找到的唯一答案是insert if exist,而不是多个insert if语句。任何帮助都将不胜感激。我只是在学习PHP代码 <?php //Open a new connection to the

我遇到的问题是,下面的PHP代码只将数据插入一个表中。我想要的是,如果文章中的目录类别等于,例如,blueinsert-into-Table-blue,但是如果它等于yellow-INSERT-into-yellow,但是如果它等于red-INSERT-into-Table-red

我找到的唯一答案是insert if exist,而不是多个insert if语句。任何帮助都将不胜感激。我只是在学习PHP代码

    <?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','some directory','password','some username');

//Output any connection error
if ($mysqli->connect_error) {
    die('Connection failed : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//values to be inserted in database table
$firstname  = '$_POST[firstname]'; 
$lastname   = '$_POST[lastname]';
$city       = '$_POST[city]';   
$state      = '$_POST[state]';
$zipcode    = '$_POST[zipcode]';
$directorycategory  = '$_POST[directorycategory]';
$active     = '$_POST[active]';


    $query = ("INSERT INTO blue(
            firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)");

    $statement = $mysqli->prepare($query);

    //bind parameters
    $statement->bind_param('sssssss', $_POST['firstname'], $_POST['lastname'], $_POST['city'], $_POST['state'], $_POST['zipcode'], $_POST['directorycategory'], $_POST['active']);

    if($statement->execute()){
       header("some location");
    }else{
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    $statement->close();
?>


可以使用句点将字符串与变量连接起来,形成一个大字符串

我看不出您在哪里有查询的控制结构。如果我正确理解了这个问题,我认为您需要这样的东西:

    if(directorycategory  == 'blue'){$query = ("INSERT INTO blue(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }           
    else if(directorycategory  == 'yellow'){$query = ("INSERT INTO yellow(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }       

等等

你的答案最有效。最后的代码如下

     <?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','some directory','password','some username');

//Output any connection error
if ($mysqli->connect_error) {
    die('Connection failed : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//values to be inserted in database table
$firstname  = '$_POST[firstname]'; 
$lastname   = '$_POST[lastname]';
$city       = '$_POST[city]';   
$state      = '$_POST[state]';
$zipcode    = '$_POST[zipcode]';
$directorycategory  = '$_POST[directorycategory]';
$active     = '$_POST[active]';


        if($directorycategory  == 'Employer'){
    $query = ("INSERT INTO employer(
            firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)");
        }
     else if($directorycategory  == 'Blue'){$query = ("INSERT INTO blue(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Green'){$query = ("INSERT INTO green(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Red'){$query = ("INSERT INTO red(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Orange'){$query = ("INSERT INTO orange(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }       

    $statement = $mysqli->prepare($query);

    //bind parameters
    $statement->bind_param('sssssss', $firstname, $lastname, $city, $state, $zipcode, $directorycategory, $active);

    if($statement->execute()){
       header("some location");
    }else{
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    $statement->close();
?>


首先,从删除“$\u POST[firstname]”和所有其他的引号开始,并在数组名称本身中使用引号。i、 e.
$\u POST['firstname']
-这些引号放错了位置。但是,您不需要这些,您已经在绑定中有了它们并且格式正确,那么为什么它们在您的代码中呢?您可以将
$\u POST['directorycography']
的值连接到查询字符串
$query=(“插入到“$\u POST['directorycography']”(firstname,lastname,city,st…)
@PLAudet最好先对输入进行清理。我刚刚给了他解决方案。不能为他做所有的工作;-)。而且,他没有消毒任何东西所以。。。(但我同意你的看法)案例转换也会起作用。有很多方法可以做到这一点,甚至是通过SQL。@champlow007。由于某种原因,我在使用该查询时会得到一个空白页。这是我需要在代码中更改的唯一命令吗?目录类别的值来自html表单中的选择框。当您尝试执行$query=(“插入蓝色(firstname)值(?))之类的简单操作时,它是否起作用$语句->绑定参数('s','Marquise')@champlow007是的,这很有效。我在MySQL中有四个表。我有一个html表单,它有一个下拉菜单,如果用户选择蓝色,我希望插入将文章中的所有数据插入表单上选定的MySQL表中。谢谢,您可以将其标记为该页面未来访问者的答案。
     <?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','some directory','password','some username');

//Output any connection error
if ($mysqli->connect_error) {
    die('Connection failed : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//values to be inserted in database table
$firstname  = '$_POST[firstname]'; 
$lastname   = '$_POST[lastname]';
$city       = '$_POST[city]';   
$state      = '$_POST[state]';
$zipcode    = '$_POST[zipcode]';
$directorycategory  = '$_POST[directorycategory]';
$active     = '$_POST[active]';


        if($directorycategory  == 'Employer'){
    $query = ("INSERT INTO employer(
            firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)");
        }
     else if($directorycategory  == 'Blue'){$query = ("INSERT INTO blue(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Green'){$query = ("INSERT INTO green(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Red'){$query = ("INSERT INTO red(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }
     else if($directorycategory  == 'Orange'){$query = ("INSERT INTO orange(
        firstname, lastname, city, state, zipcode, directorycategory, active) VALUES(?, ?, ?, ?, ?, ?, ?)"); }       

    $statement = $mysqli->prepare($query);

    //bind parameters
    $statement->bind_param('sssssss', $firstname, $lastname, $city, $state, $zipcode, $directorycategory, $active);

    if($statement->execute()){
       header("some location");
    }else{
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    $statement->close();
?>