dongyinzheng6572 2014-02-18 21:04
浏览 16
已采纳

使用提交按钮更新mysql中的字段

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.

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.

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.

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.

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");

?>

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!

  • 写回答

1条回答 默认 最新

  • douhuai2015 2014-02-18 21:16
    关注

    Use your database record's ID:

    First in your form:

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

    Then in the handler:

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

    Edited to add obligatory SQL injection protection!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作