Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

php - Not updating SQL database

For some reason my code isn't updating mySQL database, but it isn't reporting any errors.

register.php (form)

        <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);");
                }
    ?>

Any ideas why this isn't working correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a working sample with prepared statements, that are "better" to use generally instead of query

action.php

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

if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...