dongyuntao2000 2014-06-06 09:28
浏览 28
已采纳

使用先前值和当前值更新每一行

sorry for the complicated heading.i am doing learning php and got stuck.i have a database table table_name

id(primary key)     name       ip 
1                     a         192.168.0.1,192.168.0.5,171.87.65 //separated by comma's
2                     b         192.168.0.1,175.172.2.6,164.77.42

now i want to add an array of values ip[0] and ip[1] coming from a two different text-area to the end of the ip's of each name and just updating the ip column of each row.so it will just append new values with previous one.

name a<textarea rows="4" cols="40"  name="ip[]"></textarea> 
name b<textarea rows="4" cols="40"  name="ip[]"></textarea>
<input type="submit" />

this is how its inserted

if(isset($_POST['submit'])) {
$ip_details = $_POST['ip'];
$values = array(

array('id' => '"1"', 'name' => '"a"',  ip => '"'.$ip_details[0].'"'),

  array('id' => '"2"','name' => '"b"', ip => '"'.$ip_details[1].'"'),
);


 $columns = implode(', ', array_keys($values[0]));
foreach($values as $value) {
    $value = implode(', ', $value);

$statement = "INSERT INTO `center_listt` (id,name,ip)  VALUES     ($value)";

    $res=mysql_query($statement);
echo "success";
}
}

i need to update each rows of namea and b with new values coming from text-area with previous values. i am thinking of array_push after fetching ip from table in while loop but could not really do it.warning: array_push expects parameter 1 to be array integer given its because the $row['ip'] fetched in while loop is not valid array which array_push expects. and it will only add new values in different new rows each time which i don't want.can someone please help what to do.

  • 写回答

2条回答 默认 最新

  • dongqi9125 2014-06-09 13:58
    关注
    <?php
    if(isset($_POST['submit'])) {
    
    
    //print_r($ips); die;
    $i = 0;
    foreach($_POST['ip_details'] as $ipaddr) {
        $ips[$i] = $ips[$i].$ipaddr;
        $i++;
    }
    
    $r = 1;
    foreach($ips as $ip){
    //echo "UPDATE center_listt SET ipdetails = '$ip' WHERE `id_center` = '$r'"; die;
        if(mysql_query("UPDATE center_listt SET ipdetails = '$ip' WHERE `id_center` = '$r'")) echo "IP Address Updated <br />";
        else echo 'error occurred';
        $r++;
    }
    }
    
    $sql="select * from center_listt";
    $res=mysql_query($sql);
    if(!$res) {
    die('could not connect'.mysql_error());
    } 
    while($row=mysql_fetch_assoc($res)) 
    {
    echo $row['ipdetails']; }
    ?>
    

    its a bad practise to insert form values from array.you can fetch it from db bcoz if in future you want to add new form values you need to rewrite again with array values while fetching from db will only need you to insert new values in db.

    my query will add ip's in your specific column in a single row only updating the ip with new values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器