dongteng2534 2015-07-17 23:14
浏览 31

使用$ _GET更新mysql数据库中的多个列

I want to update 3 fields in a row in 3 columns but I don't know how to do it. I already searched google and searcedh here but couldn't find any solution for it. I want to change title, paragraph and category of a blog post using $_GET using this way:

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

when I use only one of $_title or $_txt, it works. But I couldn't find a way to update both fields together and couldnt update category selection.

full code of update.php page :

<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<?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");
    $query = ("UPDATE tbl_post SET cat='$cat' WHERE id=$id");

mysql_query($query,$con);
header("location:insert.php");
exit(); 
        }
?>
      <form action="" method="post">
        <?php

      $id = $_GET['id'];

$query = "SELECT * FROM `tbl_post` WHERE(id=$id)";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
        <p>عنوان مطلب</p>
        <input type="text" name="title" style="width:200px; border:1px solid #8C8C8C" value="<?php echo $rows['title'] ?>">
        <p>محتوای پست</p>
        <textarea name="txt" style="width:300px"><?php echo $rows['txt'] ?></textarea>
        <div class="clear"></div>
        <?php } ?>
        <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>
        <input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">
      </form>
    </div>
  </div>
</div>
<?php require_once("footer.php"); ?>

and 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"); ?>
  • 写回答

1条回答 默认 最新

  • dtlhy0771 2015-07-17 23:25
    关注

    Combine all the fields into a single query:

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

    Also, you should switch to parametrized queries instead of substituting into the SQL; this means using PDO or mysqli. Otherwise you need to escape the input data. See

    How can I prevent SQL injection in PHP?

    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染