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

时间:2015-07-17 23:14:28

标签: php mysql

我想在3列中连续更新3个字段,但我不知道该怎么做。我已经在这里搜索了google和searcedh,但无法找到任何解决方案。我想使用这种方式使用title更改博客文章的paragraphcategory$_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");

当我只使用$_title$_txt中的一个时,它可以正常工作。但是,我无法找到一种方法来同时更新这两个字段,并且无法更新类别选择。

update.php页面的完整代码:

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

和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 个答案:

答案 0 :(得分:2)

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

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

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

此外,您应该切换到参数化查询,而不是替换为SQL;这意味着使用PDOmysqli。否则,您需要转义输入数据。参见

How can I prevent SQL injection in PHP?