Не обновляет базу данных SQL

По какой-то причине мой код не обновляет базу данных MySQL, но не сообщает об ошибках.

register.php (форма)

        <form class="register_form" action="action.php?do=register" method="post">
            Team Name*: <input type="text" name="teamname" required />
            Team Region*: <input type="text" name="teamregion" maxlength="4" required />
            Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
            Team Members: <input type="text" name="teammembers"  />
            <input name="register_submit" type="submit" value="Register" />
        </form>

action.php

    <?php
        $con=mysqli_connect("192.185.#.###","########_reg","#######","#########");
        if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}
            $action = $_GET['do'];
                if($action=="register") {
                    $teamname = $_POST["teamname"];
                    $teamregion = $_POST["teamregion"];
                    $teamleader = $_POST["teamleader"];
                    $teammembers = $_POST["teammembers"];
                        $result = mysqli_query($con, "INSERT INTO teams (teamname, region, teamleader, teammembers, wins, loses)
                        VALUES (" . $teamname . "," . $teamregion . "," . $teamleader . "," . $teammembers . ",0,0);");
                }
    ?>

Есть идеи, почему это не работает правильно?

3 ответа

Решение

Вот рабочий пример с подготовленными утверждениями, которые "лучше" использовать в целом вместо query

action.php

$con = new mysqli('localhost', 'root', '', 'dachi');

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (isset($_GET['do']) && $_GET['do'] === "register") {
    $teamname = $_POST["teamname"];
    $teamregion = $_POST["teamregion"];
    $teamleader = $_POST["teamleader"];
    $teammembers = $_POST["teammembers"];
    $wins = 0;
    $loses = 0;

    $stmt = $con->prepare("INSERT INTO `teams` (`teamname`,`region`,`teamleader`,`teammembers`,`wins`,`loses`) VALUES (?,?,?,?,?,?)");
    $stmt->bind_param('ssssii', $teamname, $teamregion, $teamleader, $teammembers, $wins, $loses);
    $stmt->execute();
    $stmt->close();
}

register.php

<form class="register_form" action="action.php?do=register" method="post">
    Team Name*: <input type="text" name="teamname" required />
    Team Region*: <input type="text" name="teamregion" maxlength="4" required />
    Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
    Team Members: <input type="text" name="teammembers"  />
    <input name="register_submit" type="submit" value="Register" />
</form>

Вы должны изменить:

if($action=="register") {

в

if($action=="register_submit") {

Потому что ваш вход имеет имя тега, установленное в значение register_submit not register.

и изменить $action = $_GET['do']; в $action = $_POST['register_submit'];

register.php (форма)

<form class="register_form" action="action.php?do=register" method="post">

action.php

$action = $_GET['do'];
Другие вопросы по тегам