Mark
Mark

Reputation: 151

Creating an "Upvote" button using simple HTML form and PHP

I'm trying to create a simple upvote button in an HTML form that uses PHP to update a MySQL database. I know there are better implementations with Ajax etc but I'm looking to just use simple HTML and PHP.

I've been able to update the database using input type="number" form element but can't update when I try changing it to input type="submit" with value=1.

HTML

<form action="send_formdata.php" method="POST">
     <label>Digs</label>
     <input type="submit" name="digs" id="digs" value=1>
</form>

PHP (in send_formdata.php)

<?php
    $link =     mysqli_connect("mysql.XXXXXX.com","XXXXX","XXXX","XXXXXX")  or die("failed to connect to server !!");
    mysqli_select_db($link,"XXXXXX");

        if(isset($_REQUEST['submit'])){
            $errorMessage = "";
            $digs=$_POST['digs'];

        if ($errorMessage != "" ) {
            echo "<p class='message'>" .$errorMessage. "</p>" ;
        }else{
            $insqDbtb="UPDATE `XXXXXXXX`.`coffee`
            SET digs = digs + '$digs' 
            WHERE name = 'Africa'";
            mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
        }
    }

?>

MySQL Database Info:

Table name coffee

Column  Type
id  int(10) unsigned Auto Increment  
name    varchar(255)     
roaster varchar(255)     
digs    int(10) unsigned

Upvotes: 1

Views: 793

Answers (1)

Zsw
Zsw

Reputation: 4097

Community Wiki because question was answered in comments.


You said that the code worked when you had input type="number". I suspect you most likely had a submit button with name="submit" that you later removed.

As a result, this statement is always false.

if(isset($_REQUEST['submit'])){

This is because your submit button has name="digs". So when you submit it, there is no $_REQUEST['submit']. Instead there will be $_REQUEST['digs'].

Upvotes: 1

Related Questions