php - Update echoed data using WHILE loop. Only updates one record -
i can't seem able update records except first one. not sure how modify of displayed records.
<?php if(isset($_post["action"]) == "update") { $id = $_post['m_id'][0]; $type = $_post['type'][0]; // if echo $id & $type, gives me first record.** mysql_query(" update membership_type set mt_type ='$type' mt_id = '$id'" ); } ?>
all of within same php page.
<form name=form action='' method='post'> <?php $result=mysql_query("select * membership_type;"); while($rows=mysql_fetch_array($result)) { ?> <input size=35 class=textfield type=text name='type[]' value='<?php echo $rows['mt_type']; ?>'> <input type=hidden name='m_id[]' value="<?php echo $rows['mt_id']; ?>"> <input type=submit value="update"> <?php } ?>
how edit of displayed records clicking update button???
first: should never use mysql_* functions deprecated.
second: try code:
<?php // connection database $mysqli = new mysqli('host', 'user', 'password', 'database'); // check if there's post request in file if($_post){ foreach($_post['m_id'] $id => $type){ $query = "update membership_type set mt_type = '".$type."' mt_id = '".$id."'"; // try exec query $mysqli->query($query) or die($mysqli->error); } }else{ // membership_type records , iterate $result = $mysqli->query("select * membership_type") or die($mysqli->error); ?> <form name='form' action='<?php echo $_server['php_self'] ?>' method='post'> <?php while($row = $result->fetch_object()){ ?> <input size='35' class='textfield' type='text' name='m_id[<?php echo $row->mt_id ?>]' value='<?php echo $row->mt_type; ?>'> <input type='submit' value="update"> <?php } ?> </form> <?php } ?>
third: in order add more security (this code vulnerable), try mysqli_prepare
Comments
Post a Comment