使用$ _GET从URL中获取具有相同ID的多个值并更新数据库 [英] Use $_GET to grab multiple values with same id from URL and update DB

查看:128
本文介绍了使用$ _GET从URL中获取具有相同ID的多个值并更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,试图使用一系列复选框从列表中选择多件设备,以便用户可以检出这些商品。



这是我提交数据的第一页

 < div class =list>< form action = remove.phpenctype =multipart / form-data
name =formid =form>

< input type =submitstyle =color:white; background:none repeat scroll 0%0%
black; font-size:21px; padding-bottom:30px; position:fixed; height:30px;
right:0px; margin-right:1px;
value =Checkout/>
< table cellpadding =0cellspacing =0border =0id =table>
< thead>
< tr>
< th>选择< / th>
设备号码< / th>
< th>设备< / th>
< / tr>< / thead>
< tbody>
<? $ sql =选择*从设备;
$ result = mysql_query($ sql);
while($ rows = mysql_fetch_array($ result)){
if($ rows ['equip_avail'] ==1){
?>

< tr>
< td width =5%>< input type =checkboxname =equip_idvalue =<?php echo
$ rows ['equip_id'];?> ; />< / TD>
< td width =13%><?php echo $ rows ['equip_num'];?>< / td>
< td width =20%><?php echo $ rows ['equipment']; ?>< / TD>
< / tr>
< php + b>< / php>
}
?>

这里是从网址获取数据的页面,并将其发布以确认需要这个页面能够从url中获取多个equip_id(或者我目前不知道的另一个解决方法)并更新正确的user_id的信息。

  $ id = $ _SESSION ['id']我可以得到多个出现在URL中,但只有最后一个被抓住。 ]。 
$ equip_id = $ _ GET ['equip_id'];
$ sql = mysql_query(SELECT * FROM equipment WHERE equip_id ='$ equip_id');
while($ row = mysql_fetch_array($ sql)){
$ equip_id = $ row ['equip_id'];
$ equipment = $ row [equipment];
$ equip_num = $ row [equip_num];
$ equip_avail = $ row [equip_avail];
$ user_id = $ row ['user_id'];


if($ _SERVER ['REQUEST_METHOD'] ==='POST'){
$ user_id = $ _POST ['user_id'];
$ equip_avail = $ _POST ['equip_avail'];
$ sql = mysql_query(UPDATE equipment SET user_id ='$ user_id',
equip_avail ='$ equip_avail'WHERE equip_id ='$ equip_id');

header(Location:equipment_checkout.php);
exit();
} //如果发布
?则关闭>

这是HTML

 < table height =225px> 
< form action =< ;? echo'remove.php?equip_id ='。$ equip_id。'??>
method =postenctype =multipart / form-dataname =formid =form>
< td width =423valign =top>< h2>设备:<?php echo $ equipment?>< / h2>
< h2>设备编号:<?php echo $ equip_num?>< / h2>
< input name =equip_availtype =hiddenid =equip_availvalue =0size =30
maxlength =24/> ($ _SESSION ['id']!=='0')&&($ uaccounttype!=='e')&&
($ uaccounttype!
< =='d')){?>
< input name =user_idtype =hiddenid =user_idvalue =< ;? echo $ _SESSION ['id']
?> size =30maxlength =64/>
< / tr>< ;?} else {
$ sql =SELECT * FROM members WHERE accounttype IN('b','c','e');
$ result = mysql_query($ sql);
?>
< h2>选择员工:< select name =user_idid =user_id>
<? $ count = 1;
while($ rows = mysql_fetch_array($ result)){
if($ count == 1){?>
< option value =<?echo $ rows ['id'];?>>
<? echo $ rows ['firstname']。 。 $ rows ['lastname'];}}?>< / option>
< / select>< / h2>
<?}?>
< input name =Submittype =submitvalue =Check Out/>
< / br>
< / td>
< / form>
< / table>
< / div>

最后一部分需要做的是从第一页中选择每个equip_id在这里显示,然后为每个选定的设备使用正确的user_id更新数据库。就像我说的,我可以一次更新一个,我只需要编辑一个大数就可以编辑。



**注意:我知道这不是发送数据的安全方式,如果有任何关于如何更安全的建议,我会喜欢它。谢谢!!!!

解决方案

您需要将输入名称设为数组( equip_id [] ):

 < input type =checkboxname =equip_id []value = <?php echo $ rows ['equip_id'];?> /> 

然后 $ _ GET ['equip_id'] 将是一个 $ rows ['equip_id'] s的数组。

虽然肯定会更好使用 $ _ POST


I have ran into an issue trying to use a series of checkboxes to select multiple pieces of equipment from a list so that a user can then "checkout" the items.

Here is my first page that submits the data

        <div class="list"><form action="remove.php" enctype="multipart/form-data"
         name="form" id="form">

<input type="submit" style="color: white; background: none repeat scroll 0% 0%
 black; font-size: 21px; padding-bottom: 30px; position: fixed; height: 30px;
 right: 0px; margin-right: 1px;"
     value="Checkout" />
    <table cellpadding="0" cellspacing="0" border="0" id="table">
<thead>
<tr>
<th>Select</th>
<th>Equipment Number</th>
<th>Equipment</th>
</tr></thead>
    <tbody>
  <? $sql    = "SELECT * FROM equipment";
    $result = mysql_query($sql);
    while ($rows = mysql_fetch_array($result)) {
        if ($rows['equip_avail'] == "1") {
?>

<tr>
<td width="5%"><input type="checkbox" name="equip_id" value="<?php echo 
$rows['equip_id']; ?>" /></td>
<td width="13%"><?php echo $rows['equip_num'];?></td>
<td width="20%"><?php echo $rows['equipment']; ?></td>
</tr>
<?php
        }
    }
    echo '</tbody></table></form></div>';
}
?>

And here is the page that then takes the data from the url, and posts it to confirm, I need this page to be able to take multiple "equip_id" from the url (or another workaround I am currently unaware of) and update the information for the correct "user_id". I can get multiple to appear in the URL but only the last one is grabbed.

    $id = $_SESSION['id'];
$equip_id =$_GET['equip_id'];
$sql = mysql_query("SELECT * FROM equipment WHERE equip_id='$equip_id'"); 
while($row = mysql_fetch_array($sql)){
    $equip_id = $row['equip_id'];
    $equipment = $row["equipment"];
    $equip_num = $row["equip_num"];
    $equip_avail = $row["equip_avail"];
    $user_id= $row['user_id'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user_id = $_POST['user_id'];
    $equip_avail = $_POST['equip_avail'];
    $sql = mysql_query("UPDATE equipment SET user_id='$user_id', 
    equip_avail='$equip_avail' WHERE equip_id='$equip_id'"); 

    header("Location: equipment_checkout.php");
exit();
} // close if post
?>

This is the HTML

     <table height="225px">
<form action="<? echo 'remove.php?equip_id='.$equip_id.' '?>"
   method="post" enctype="multipart/form-data" name="form" id="form">
<td width="423" valign="top"><h2>Equipment: <?php echo $equipment?></h2>
    <h2>Equipment Number: <?php echo $equip_num?></h2>
<input name="equip_avail" type="hidden" id="equip_avail" value="0" size="30"
maxlength="24" />
<?if(($_SESSION['id'] !=='0') && ($uaccounttype !== 'e') &&
($uaccounttype !== 'd')){?>
<input name="user_id" type="hidden" id="user_id" value="<? echo $_SESSION['id']
?>" size="30" maxlength="64" />
    </tr><?} else {
        $sql="SELECT * FROM members WHERE accounttype IN ('b', 'c', 'e')";
    $result = mysql_query($sql);            
        ?> 
<h2>Select Employee: <select name="user_id" id="user_id">
<?  $count = 1;
    while($rows = mysql_fetch_array($result)){
        if($count == 1){?>
<option value="<?echo $rows['id'];?>">
<?  echo $rows['firstname']. " ". $rows['lastname'] ;}} ?></option>
</select></h2>
<?}?>       
    <input name="Submit" type="submit" value="Check Out"/>
    </br>
   </td>
  </form>
</table>
</div>

What I need this last part to do is for every "equip_id" selected from the first page, to be displayed here and then the database to be updated with the correct "user_id" for each of the equipment selected. Like I said, I can get one to update at a time, I just need to make it so that a large amount can be edited.

**NOTE: I know that this is not a secure way to send data, if any advice on how to so this more securely, I would love it. THANK YOU!!!!

解决方案

You need to make the input name an array (equip_id[]):

<input type="checkbox" name="equip_id[]" value="<?php echo $rows['equip_id']; ?>" />

And then $_GET['equip_id'] will be an array of $rows['equip_id']s.

Although surely it would be better to use $_POST?

这篇关于使用$ _GET从URL中获取具有相同ID的多个值并更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆