使用提交按钮更新mysql中的字段 [英] Updating a field in mysql with submit button

查看:101
本文介绍了使用提交按钮更新mysql中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者如标题所述,我可能会以错误的方式进行操作.我已经在phpmyadmin的表中创建了一些字段.我有一个"post_id"作为唯一标识符.然后是标题为"first_name","last_initial","email","feedback"和"approved"的字段或(行).

Or as the title states, I may be going about it the wrong way. I've created some fields in a table within phpmyadmin. I have a "post_id" as the unique identifier. Then fields or (rows) titled "first_name", "last_initial", "email", "feedback" and "approved".

它用于网站上的推荐页面.我一切正常,然后意识到我需要为网站所有者创建一种在显示评论之前批准评论的方法.

It's for a testimonials page on a website. I've go things working properly then realized I needed to create a way for the site owner to approve comments before displaying them.

我是php和mysql的新手,所以对于这里的许多人来说,这可能非常简单.当我在表格上方的个人鉴定"页面上连接到数据库以输入个人鉴定时,我将调用所有值为"1"的行,因为这是已批准"注释.默认情况下,当有人在表单中输入信息以提交证明时,该字段将被赋予"0"值.现在在后端,我将连接到数据库并在字段中显示所有值为"0"的行.

I'm new to php and mysql so this may be very simple for a lot of you on here. When I connect to the database on the testimonials page just above the form to input a testimonial, I'm calling all of the rows that have a value of "1" as this is the "approved" comments. By default when someone enters info into the form to submit a testimonial, that field is given a value of "0". Now on the back end, I'm connecting to the database and displaying all rows with the value of "0" in the fields.

我正在使用html表显示字段,并且该表的最后一列具有一个复选框.理想情况下,网站所有者可以单击要批准的评论旁边的复选框,然后单击提交"按钮,只有选中的复选框将被批准.

I'm using an html table to display the fields and the last column of the table has a checkbox. Ideally, the site owner would click the checkboxes next to the comments they want to approve and click a submit button and only the checked boxes will be approved.

我遇到的问题是单击提交并连接执行此操作的php代码时,我正在使用UPDATE函数将已批准"字段中的值从0更改为1.

The problem I'm running into is when clicking submit and connecting the the php code that does this, I'm using the UPDATE function to change the value in the "approved" field from 0 to 1.

我遇到的问题是正在批准每条评论,因为我无法通过WHERE(函数,语句或所谓的东西)来限定它.而且由于此页面是动态的,并且将显示不同的评论要批准,我确实没有唯一的标识符.

The problem I'm having is it is approving every comment because I'm not qualifying it somehow with the WHERE (function, statement, or whatever this is called.) And since this page is dynamic and will be displaying different comments for approval, there is really no unique identifier as I can tell.

我希望我对这个问题的无知不会让您感到困惑,并且您能够帮助我.

I hope my ignorance on this subject hasn't confused you too badly and you'll be able to help me out.

我在这里连接到数据库...

I'm connecting to the database here......

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM feedback WHERE approved = 0");    

echo '<h1>Submitted Comments</h1>';    
echo '<form action="approve_comment.php" method="post">';    
echo '<table>';       
echo '<tr>';
echo '<th>First Name</th>';
echo '<th>Last</th>';
echo '<th>Email</th>';
echo '<th>Comment</th>';
echo '<th>Check</th>';
echo '</tr>';

while($row = mysqli_fetch_array($result))
{    
    echo '<tr style="border:1px solid #000000; padding:10px;">';
    echo '<td style="border:1px solid #000000; padding:10px; width:100px;">' . $row["first_name"].'</td>';
    echo '<td style="border:1px solid #000000; padding:10px; width:50px;">' . $row["last_initial"] . '</td>';
    echo '<td style="width:200px; padding:10px;">' . $row["email"] . '</td>';
    echo '<td style="padding:10px; width:400px;">' . $row["feedback"] . '</td>';
    echo '<td>' . '<input type="hidden" name="check" value="0"/>'.'<input type="checkbox" name="check" value="1"/>' . '</td>';
    echo '</tr>';         
}

echo '</table>';
echo '<input style="float:right;" type="submit" value="Approve Selected" />';
echo '</form>';

mysqli_close($con);
?>    

该代码显示了他们将在其中验证注释的后端元素.动作如下: 在这里连接到数据库...

That code displays the back end element where they would aprove the comments. The action is as follows: Connecting to database here...

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}  

mysqli_query($con,"UPDATE feedback SET approved=1 WHERE approved='0'");  

mysqli_close($con);
header("Location: http://redirecting to the back end page here");

?>

除我的WHERE部分外,其他所有东西都工作正常.我不确定要放在这里,还是不确定是否正确地从数据库中调用信息以使其正常工作.

Everything is working fine except my WHERE part. I'm not sure what to put here or if I'm even calling the info from the database properly to make this work.

任何输入,将不胜感激!谢谢!

Any input you have will be greatly appreciated! Thanks!

推荐答案

使用数据库记录的ID:

Use your database record's ID:

首先在您的表单中

echo "<input type=\"hidden\" name=\"post_id\" value=\"$row[post_id]\"/>";

然后在处理程序中

$post_id = mysqli_real_escape_string($con, $_POST["post_id"]);
mysqli_query($con,"UPDATE feedback SET approved=1 WHERE post_id='$post_id'");  

经过编辑以添加强制性的SQL注入保护!

Edited to add obligatory SQL injection protection!

这篇关于使用提交按钮更新mysql中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆