PHP中的多个表单操作按钮

时间:2016-12-05 17:23:05

标签: javascript php html

我的一个php页面中有两个表单操作按钮。当我点击第一个按钮时,第二个按钮会自动加载。我不是PHP开发人员,所以我不知道如何单独使用按钮。我的表单操作按钮如下所示:

//first button
<?php
echo
    "<form action='' method='post'>
<input type='submit' class='btn btn-default' name='use_button' value='Remove Scores' />
</form>";
if(isset($_POST['use_button']))
{
    $con=mysqli_connect('localhost', 'mydbuser', 'mydbpass', 'mydb');
    if (mysqli_connect_errno($con))
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }    
    $sql = "TRUNCATE TABLE contest_score";
    mysqli_query($con, $sql);
    mysqli_close($con);
    {
        echo "Score Cleared";
    }
}
?> 
<br>
// second button
<?php
echo
    "<form action='' method='post'>
      <input type='submit' class='btn btn-default' name='use_button' value='Load Questions' />
    </form>";
if(isset($_POST['use_button']))
{
    $webcon = mysqli_connect('localhost', 'mydbuser', 'mydbpass', 'mydb');
    if (mysqli_connect_errno())
    {
        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
    }
    /**
     * Queries for reading
     */
    $questions = mysqli_query($webcon, 'SELECT * FROM `questions` ORDER BY RAND() LIMIT 30');
    $mobcon         = mysqli_connectmysqli_connect('localhost', 'mydbuser', 'mydbpass', 'mydb');
    if (mysqli_connect_errno())
    {
        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
    }
    // remove old questions
    $delete = "TRUNCATE TABLE questions";
    mysqli_query($mobcon, $delete);
    /**
     * Insert data from old database
     */
    // questions
    while ($row = mysqli_fetch_array($questions))
    {
        // escape your strings
        foreach($row as $key => $val)
        {
            $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
        }
        mysqli_query($mobcon, "INSERT INTO `questions` (`option1`, `option2`, `option3`, `option4`, `correctans`, `question_text`, `cat_id`, `sub_cat_id`, `level_id`, `quesimage`) VALUES ('" . $row['option1'] . "', '" . $row['option2'] . "', '" . $row['option3'] . "','" . $row['option4'] . "','" . $row['correctans'] . "','" . $row['question_text'] . "','" . $row['cat_id'] . "','" . $row['sub_cat_id'] . "','" . $row['level_id'] . "','" . $row['quesimage'] . "');");
    }
    /*
    Close Connections
    */
    mysqli_close($mobcon);
    mysqli_close($webcon);
    {
        echo "<script>alert('Questions Loaded');</script>";
    }
}
?> 

有人建议我解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

我认为问题可能是因为您对两个按钮使用相同的输入名称。

尝试将第二个更改为另一个名称,如:

<?php

echo
"<form action='' method='post'>
<input type='submit' class='btn btn-default' name='second_use_button' value='Load Questions' />
</form>";

if(isset($_POST['second_use_button']))
{