jwitk27
jwitk27

Reputation: 3

Trying to make "like" buttons for each of my MySQL rows that are displayed on my HTML page

MySQL, PHP, HTML

So, I'm trying to make "like" buttons for each of my MySQL rows that are displayed on my html page from my table. I have the like button created, and it increases the value, but it increases the value of ALL rows, and I need it to only increase the value of the row that it sits next to on my html page. (I know my code is messy, haven't used php much)

"like.php" page below -

<?php

include "db_connect.php";

$sql = "UPDATE ideas_table SET Idea_Value = Idea_Value + 1";
$result = $mysqli->query($sql) or die('ERROR');

?>

and below is a keyword search page that displays the data, and the like values (all of which increase when the "like" button is pressed)

<?php

include "db_connect.php";
$keywordfromform = $_GET["keyword"];

//search database
echo "<h2>Showing ideas with '$keywordfromform'</h2>";

$sql = "SELECT Idea_ID, Idea, Idea_Value, Idea_Time FROM ideas_table WHERE         
Idea LIKE '%". $keywordfromform ."%'";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo
    // like button below
    "<form action='like.php'>
            <button type='submit'>Like</button>
    </form>".
    $row["Idea_Value"].
    " - Idea: ".
    $row["Idea"].
    "<br>";
}
} else {
    echo "0 results";
}

?>

Once again, I just need the like button (that sits next to the displayed row) to affect ONLY that row. Thank you for your help!!!

Upvotes: 0

Views: 45

Answers (2)

NIKHIL NEDIYODATH
NIKHIL NEDIYODATH

Reputation: 2932

Try this like.php

<?php
include "db_connect.php";

$Idea_Value = $_POST['idea_value']+1;
$Idea_ID = $_POST['Idea_ID'];
$sql = "UPDATE ideas_table SET Idea_Value = $Idea_Value WHERE Idea_ID = '$Idea_ID'";
$result = $mysqli->query($sql) or die('ERROR');
?>

Other Page

<?php

include "db_connect.php";
$keywordfromform = $_GET["keyword"];

//search database
echo "<h2>Showing ideas with '$keywordfromform'</h2>";

$sql = "SELECT Idea_ID, Idea, Idea_Value, Idea_Time FROM ideas_table WHERE         
Idea LIKE '%". $keywordfromform ."%'";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo
    "<form action='like.php'>
            <input type='hidden' name='".$row["Idea_ID"]."' value='".$row["Idea_Value"]."'/>
            <button type='submit'>Like</button>
    </form>".
    $row["Idea_Value"].
    " - Idea: ".
    $row["Idea"].
    "<br>";
}
} else {
    echo "0 results";
}

?>

Upvotes: 0

Mayank Majithia
Mayank Majithia

Reputation: 1966

you need to send Idea_ID in hidden while you submitting Form, put this line before Submit Button.

   <iinput type='hidden' name='Idea_ID' value='".$row["Idea_ID"]."'>

Like update query should like this in Like.php.

include "db_connect.php";

$idea_id=$_REQUEST['Idea_ID'];

$sql = "UPDATE ideas_table SET Idea_Value = Idea_Value + 1 where Idea_ID = ".$Idea_ID;

$result = $mysqli->query($sql) or die('ERROR');

Upvotes: 1

Related Questions