Php 使用$\u GET更新mysql数据库中的多列

Php 使用$\u GET更新mysql数据库中的多列,php,mysql,Php,Mysql,我想在3列中更新一行中的3个字段,但我不知道怎么做。我已经在这里搜索了google和searcedh,但找不到任何解决方案。我想用$\u GET通过以下方式更改博客文章的标题、段落和类别: <?php $id = $_GET['id']; ?> <div class="middle"> <div class="content" style="width:100%;"> <div class="context" style="width:100

我想在3列中更新一行中的3个字段,但我不知道怎么做。我已经在这里搜索了google和searcedh,但找不到任何解决方案。我想用
$\u GET
通过以下方式更改博客文章的
标题
段落
类别

<?php
$id = $_GET['id'];
?>
<div class="middle">
  <div class="content" style="width:100%;">
    <div class="context" style="width:100%">
      <?php
    if(isset($_POST['submit'])){
        $title = $_POST['title'];
        $txt = $_POST['txt'];
$query = ("UPDATE tbl_post SET title='$title' WHERE id=$id");
$query = ("UPDATE tbl_post SET txt='$txt' WHERE id=$id");
和insert.php:

<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<div class="middle">
  <div class="content" style="width:100%;">
    <div class="context" style="width:100%">


    <?php
    if(isset($_POST['submit'])){
        $title = $_POST['title'];
        $cat = $_POST['cat'];
        $txt = $_POST['txt'];
        echo 'title = '.$title.'<br>'.'category ='.$cat.'<br>'.'txt = '.$txt;
$query = "INSERT INTO tbl_post(`title`,`txt`,`cat_id`) VALUES  ('$title','$txt','$cat')";   
mysql_query($query,$con);
header("location:insert.php");
exit(); 
        }
?>

 <form action="" method="post">
        <p>عنوان مطلب</p>
        <input type="text" name="title" style="width:200px; border:1px solid #8C8C8C;">
        <p>دسته بندی</p>
        <select name="cat" style="width:200px">
          <?php

$query = "SELECT * FROM `tbl_cat` ORDER BY `id` ASC";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){


?>
          <option value="<?php echo $rows ['id'] ?>"><?php echo $rows ['name'] ?></option>
          </li>
          <?php } ?>
        </select>
        <p>محتوای پست</p>
        <textarea name="txt" style="width:300px"></textarea>
        <div class="clear"></div>
        <input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">



      </form>
    </div>
  </div>
</div>
<?php require_once("footer.php"); ?>


将所有字段合并到一个查询中:

$title = $_POST['title'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];

$query = "UPDATE tbl_post SET title='$title', txt = '$txt', cat = '$cat' WHERE id = $id";
此外,您应该切换到参数化查询,而不是替换到SQL中;这意味着使用
PDO
mysqli
。否则,您需要转义输入数据。看


U不需要运行2个查询来更新1个表,您可以这样做
updatetable SET this='$that',abc='$xyz',其中sun='$moon'
“我想更新3个字段…”我只看到2:
title
txt
。第三个是什么?执行查询的代码在哪里?如果在单独的查询中执行每个字段,则必须在
$query
分配之间执行查询。我猜您是在最后执行查询,因此它只执行您分配的最后一个
$query
。添加了完整的代码。标题和txt已更新。但仍然无法更新类别。谢谢。第一个问题已解决。问题仅在“$title”之后(,)!!但是“$cat”仍然不起作用,我无法更新数据库中的类别选择(下拉菜单)。您好,您的问题是$query是可变的,而不是您没有调用db的查询函数,那么查询值是:“update tbl_post SET cat=”$cat'其中id=$id”。您可以看到mysql更新语法和工作说明:@Barmar的答案很好。
$title = $_POST['title'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];

$query = "UPDATE tbl_post SET title='$title', txt = '$txt', cat = '$cat' WHERE id = $id";