1

I'm having an issue with a form inside my table. I created a table that shows a list of recent status's that the people the user follows posts. I created a form inside the table so the community can "like" or "unlike".

Every time I try to submit the form, the post is being posted in the URL and stops at my button name "likestatus="

example of the URL returned:

account.php?likesid=5&likerid=117&likestatus=

The input of the form works, as each button shows the corresponding "likesid" and "likerid" in the URL

I just don't understand why it's failing.

Here is the code for my "$_POST['likestatus']"

<?php
if(isset($_POST['likestatus']))
{


//inputs from the form
$likesid =mysqli_real_escape_string($conn, $_POST['likesid']);
$likerid =mysqli_real_escape_string($conn, $_POST['likerid']);

//query to check if already likes status
$sql = "SELECT likesid, likerid FROM user_likes WHERE (likesid='$likesid') AND (likerid='$likerid')";
$result = mysqli_query($conn, $sql);

$alreadylike = mysqli_num_rows($result);
if ($alreadylike > 0) {
    echo "You already liked this status";
}
else {      

    $hostname = "localhost";
    $username = "root";
    $password = "changed for post";
    $databaseName = "changed for post";     
    $connect = mysqli_connect($hostname, $username, $password, $databaseName);      

    //inputs from the form
    $likesid =mysqli_real_escape_string($connect, $_POST['likesid']);
    $likerid =mysqli_real_escape_string($connect, $_POST['likerid']);


    //query to insert into table
    $query = "INSERT INTO `user_likes` SET `likesid`='".$likesid."', `likerid`='".$likerid."'";

    $result = mysqli_query($connect, $query);

    if($result)
    {           
        echo 'updated';
    }
    else
    {
        echo 'not updated';
    }   
    mysqli_close($connect);
    }
}
?>

The form code:

<td style="width: 100px;">  
<p align="center">
<form action="account.php" method="post">   
    <input type="hidden" name="likesid" value="<?php echo $statusid; ?>">
    <input type="hidden" name="likerid" value="<?php echo $_SESSION['id']; ?>">
    <button style="color: #FFFFFF; width: 50%; margin-left: 35px; background-color: transparent; cursor: pointer; text-shadow: 1px 1px 4px #2F3A85" type="submit" name="likestatus">
    <label class="myLabel"><span class="w3-xlarge" style="font-family: 'Righteous'; border: 1px; border-radius: 5px; border-color: #FFFFFF;">like</span></label>
    </button>
</form></p>     
</td>

The page does not return any errors that could point me in any direction, it simply just reloads the page with post info in the URL.

I am assuming the complications start with the fact that the form is inside a table array and maybe the forms submit value is remaining the same throughout all tables?

2
  • What's not working ? Your data are correctly saved in the DB, ain't they ? Also your code is vulnerable to user input manipulation; anyone could change the likerid in the form to force other people to like something. Commented May 6, 2017 at 19:24
  • The data is not being saved in the DB at this point, thanks for the tip on the manipulation, I'll make that easy fix Commented May 6, 2017 at 19:35

2 Answers 2

0

Because your submit button is a button, not an input.

Change this:

<button style="color: #FFFFFF; width: 50%; margin-left: 35px; background-color: transparent; cursor: pointer; text-shadow: 1px 1px 4px #2F3A85" type="submit" name="likestatus">

to this:

<input style="color: #FFFFFF; width: 50%; margin-left: 35px; background-color: transparent; cursor: pointer; text-shadow: 1px 1px 4px #2F3A85" type="submit" name="likestatus">
Sign up to request clarification or add additional context in comments.

3 Comments

The button has already type=submit. Check this w3schools.com/tags/att_button_type.asp
I tried changing the button to input before posting this question, but it gave me the exact same results. Any other ideas?
0

Apparently I was trying to run a form inside of a form, which explains why the post was just hanging out in the URL and not submitting into my database.

I deleted the form tags on the outside of the form that I was trying to submit and it all works as desired.

As embarrassing as it is, I figured I'd post the answer in hopes to save someone else the time I spent...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.