Priya
Priya

Reputation: 143

Multiple form action button in PHP

I have two form action buttons used in one of my php pages. When I click on first button, the second one is automatically loading. I am not PHP developer, so I do not know how to make button work separately. My form action button is like below:

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

Can anyone suggest me to solve this issue?

Thanks.

Upvotes: 1

Views: 109

Answers (1)

Diego Incerti
Diego Incerti

Reputation: 136

I think the problem may be because you are using the same input name for the two buttons.

Try to change the second one to another name like:

<?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']))
{

Upvotes: 1

Related Questions