@alxeewldmr
Спам-модератор в ASKFM

Почему форма отправляется в 2 разные таблицы?

Всем привет, не могу решить такую проблему. Есть два Class'a support.php и subscribe.php. Цель у них проста, Class subscribe.php выполняется в sidebar.php, и там находится только одно поля видите email и кнопка  submit, и меэл добавляется в таблицу. Вторая форма находится в footer.php которая связана support.php и там 3 поля форма и одна из них email. Вопрос в том почему когда что-то отправляется через index.php куда подключены и sidebar и footer, они цепляют другу и заполняются обе в таблицы в базе данных?

Коды классов:
subscribe.php

<?php
class subscribe
{
    // database connection and table name
    private $conn;
    private $table_name = "Subscribe";

    // object properties
    public $id;
    public $email;

    public function __construct($db)
    {
        $this->conn = $db;
    }

    function create(){
        //write query
        $query = "INSERT INTO " . $this->table_name . " (email)" .
            "VALUES ('{$this->email}');";

        $stmt = $this->conn->prepare($query);

        // posted values
        $this->email=htmlspecialchars(strip_tags($this->email));
        // bind values
        $stmt->bindParam(":email", $this->email);

        if($stmt->execute()){
            return true;
        }else{
            return false;
        }

    }
}


support.php
<?php
class support {
    // database connection and table name
    private $conn;
    private $table_name = "ContactUs";

    // object properties
    public $id;
    public $name;
    public $theme;
    public $email;

    public function __construct($db)
    {
        $this->conn = $db;
    }

    function support(){
        //write query
        $query = "INSERT INTO " . $this->table_name . "(name, theme, email)" .
            "VALUES ('{$this->name}','{$this->theme}','{$this->email}');";

        $stmt1 = $this->conn->prepare($query);

        // posted values
        $this->name=htmlspecialchars(strip_tags($this->name));
        $this->theme=htmlspecialchars(strip_tags($this->theme));
        $this->email=htmlspecialchars(strip_tags($this->email));
        // bind values
        $stmt1->bindParam(":name", $this->name);
        $stmt1->bindParam(":theme", $this->theme);
        $stmt1->bindParam(":email", $this->email);

        if($stmt1->execute()){
            return true;
        }else{
            return false;
        }

    }
}


sidebar.php

<?php
// include database and object files
include_once 'DB/database.php';
include_once 'Objects/category.php';
include_once 'Objects/subscribe.php';


// get database connection
$database = new Database();
$db = $database->getConnection();

// pass connection to objects

$category = new Category($db);
$subscribe = new subscribe($db);
if($_POST){
    $subscribe->email = $_POST['email'];
    if($subscribe->create()){
        echo "<div class='alert alert-success'>You have successfully subscribed to the newsletter. <br>
        The newsletter about new out products will be sent to your e-mail address.</div>";
    }

    // if unable to create the product, tell the user
    else{
        echo "<div class='alert alert-danger'>Unable to create product.</div>";
    }
}
?>
<code lang="html">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
                                            <br>
                                            <input type="text" name="email" value="" class="form-control" placeholder="SCU number" required>
                                            <br>
                                            <button type="submit" class="btn btn-success">Add email</button>
                                        </form>
</code>


footer.php

<?php
// include database and object files
include_once 'DB/database.php';
include_once 'Objects/category.php';
include_once 'Objects/support.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// pass connection to objects
$category = new Category($db);
$support = new support($db);
if($_POST){
    $support->name = $_POST['name'];
    $support->theme = $_POST['theme'];
    $support->email = $_POST['email'];
    if($support->support()){
        echo "<div class='alert alert-success'>Thank you, for question. <br> We will be in touch soon..</div>";
    }

    // if unable to create the product, tell the user
    else{
        echo "<div class='alert alert-danger'>Unable to create product.</div>";
    }
}
?>
<code lang="html">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
                                <div class="input-append newsletter-box text-center">
                                    <input type="text" class="full text-center" placeholder="Name" name="name">
                                    <input type="text" class="full text-center" placeholder="Theme" name="theme">
                                    <input type="text" class="full text-center" placeholder="Email" name="email">
                                    <button type="submit" class="btn btn-success">Submit</button>
                                </div>
                            </form>

</code>
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ответы на вопрос 1
Stalker_RED
@Stalker_RED
Потому что в обоих проверяется if($_POST), вот и срабатывают.

А вообще - ужасная лапша, которую проще с нуля переписать чем исправить.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы