php - update multiple columns in mysql database using $_GET -
i want update 3 fields in row in 3 columns don't know how it. searched google , searcedh here couldn't find solution it. want change title
, paragraph
, category
of blog post using $_get
using 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' id=$id"); $query = ("update tbl_post set txt='$txt' id=$id");
when use 1 of $_title
or $_txt
, works. couldn't find way update both fields , 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' id=$id"); $query = ("update tbl_post set txt='$txt' id=$id"); $query = ("update tbl_post set cat='$cat' id=$id"); mysql_query($query,$con); header("location:insert.php"); exit(); } ?> <form action="" method="post"> <?php $id = $_get['id']; $query = "select * `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 * `tbl_cat` order `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 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 * `tbl_cat` order `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"); ?>
combine fields single query:
$title = $_post['title']; $txt = $_post['txt']; $cat = $_post['cat']; $query = "update tbl_post set title='$title', txt = '$txt', cat = '$cat' id = $id";
also, should switch parametrized queries instead of substituting sql; means using pdo
or mysqli
. otherwise need escape input data. see
Comments
Post a Comment