简体   繁体   English

使用 php 表单和 mysql 增加按钮点击次数

[英]Increment a count on button click with php form and mysql

I'm new to php and mysql so this I'm sure is very simple!我是 php 和 mysql 的新手,所以我相信这很简单!

I would like to increment a specific entry depending on which button was clicked.我想根据单击哪个按钮来增加特定条目。 The problem I am having is that I'm not sure how to tell mysql which entry to increment via php.我遇到的问题是我不确定如何通过 php 告诉 mysql 增加哪个条目。

I'm using a while loop to display my table and then on the end of each row adding a button that has a name = $row[id] value = $row[likes].我使用 while 循环来显示我的表格,然后在每一行的末尾添加一个名称 = $row[id] value = $row[likes] 的按钮。 If name was simply a word then it wouldn't be a problem but I need it to be different depending on the row it's in. (I'm using the row id the auto increments, I don't display it but it exists).如果 name 只是一个词那么它就不是问题但我需要它根据它所在的行而有所不同。(我使用自动递增的行 id,我不显示它但它存在) .

My.html:我的.html:

$host="xxx";
$username="xxx";
$password="xxx";
$db_name="xxx";
$tbl_name="blog";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$result = mysql_query("SELECT * FROM $tbl_name ORDER BY id DESC");

echo "<table id='blog'>
<tr>
<th>Update</th>
<th>Likes</th>
</tr>";

while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td id='entry'>" . $row['entry'] . "</td>";
    echo "<td id='like'>" . "<form action ='likes.php' method ='post'>" . "<input type='submit' name='$row[id]' value='$row[likes]' />" . "</form>" . "</td>";              
    echo "</tr>"; }
echo "</table>";
mysql_close($con);
?>

My.php:我的.php:

<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con) {
    die('Could not connect: ' . mysql_error()); }

mysql_select_db("xxx", $con);

if(mysql_query("UPDATE blog SET likes = likes +1 WHERE id = '$_POST[$id]'")) {
    header('location:blog.php'); }
else {
    echo "Error: " . mysql_error(); }
mysql_close($con);
?> 

All I want to do is link 'input name = $row[id]' in the html document with the WHERE id = $_POST[id] so that it will increment the like count on button click.我想要做的就是将 html 文档中的 'input name = $row[id]' 链接到 WHERE id = $_POST[id] ,这样它将增加按钮点击时的点赞数。

Thanks in Advance!!提前致谢!!

Use a hidden input within the form to tell the PHP side which entry to increment.在表单中使用隐藏的输入来告诉 PHP 端要增加哪个条目。

echo "<td id='like'><form action ='likes.php' method ='post'><input type='hidden' name='id' value='" . (int)$row['id'] . "' /><input type='submit' name='submit' value='" . (int)$row['likes'] . "' /></form></td>";

The query line should be:查询行应该是:

if(mysql_query("UPDATE blog SET likes = likes +1 WHERE id = '" . (int)$_POST['id'] . "'")) {

Notice I casted the IDs as (int) , this prevents SQL Injection in the query, and prevents XSS when outputting.请注意,我将 ID 转换为(int) ,这可以防止查询中的 SQL 注入,并防止输出时的 XSS。

The submit button is unreliable for the transportation of data, this is because in some situations not all browsers actually send the submit button as a POST/GET variable.提交按钮对于数据传输来说是不可靠的,这是因为在某些情况下并非所有浏览器实际上都将提交按钮作为 POST/GET 变量发送。

The other thing I noticed was the use of this syntax $row[likes] which should be:我注意到的另一件事是使用这种语法$row[likes]应该是:

$row['likes']

If you don't include quotes then PHP first treats likes as a constant and if not defined, falls back as a string.如果您不包含引号,那么 PHP 首先将likes视为常量,如果未定义,则返回为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM