vendredi 24 juin 2016

mySQL query for updating multiple data that have 1 same column data from PHP form


Here is my code <?php $mysql_db_hostname = "localhost"; $mysql_db_user = "root"; $mysql_db_password = ""; $mysql_db_database = "data"; $connection = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database"); mysql_select_db($mysql_db_database, $connection) or die("Could not select database"); ?> <?php $result = mysql_query("SELECT * FROM prefix WHERE cid = '123'") or die(mysql_error()); echo "<form method='post'><table border='1' cellpadding='7'>"; echo "<tr> <th><font color='Red'>CID</font></th> <th><font color='Red'>IP Prefix</font></th> </tr>"; if (isset($_POST['upd'])) { if ($_POST['dynfields']) { foreach ( $_POST['dynfields'] as $key=>$value ) { $values = mysql_real_escape_string($value); $query = mysql_query("UPDATE prefix SET ipfx = '$values' WHERE cid = '123'", $connection ); if ($query) { echo "berhasl".mysql_info(); } } } } $row = mysql_fetch_array( $result ); echo '<tr> <td><font color="#333">' . strtoupper($row['cid']) . '</font></td><td><table>'; $no=1; while($row = mysql_fetch_array( $result )) { echo ' <tr> <td><input id="field_'.$no++.'" name="dynfields[]" type="text" value="' . $row['ipfx'] . '"><hr></td> </tr>'; //////////////////////////////////// } echo '</table></td></tr> <tr> <td colspan=2><button type="submit" name="upd" style="float:right;">Update</button></td> </tr> </table> </form>'; $no++; ?> When I click submit, it will update all the ipfx data from the first input form where cid=123. Like this: Before : +----+-----+-------------+ | id | cid | ipfx | +----+-----+-------------+ | 1 | 123 | 12.34.56.78 | | 2 | 123 | 87.65.34.21 | +----+-----+-------------+ After : +----+-----+-------------+ | id | cid | ipfx | +----+-----+-------------+ | 1 | 123 | 12.12.12.12 | | 2 | 123 | 12.12.12.12 | +----+-----+-------------+ But, I want to update it with each value from each input form. Like this: Before : +----+-----+-------------+ | id | cid | ipfx | +----+-----+-------------+ | 1 | 123 | 12.34.56.78 | | 2 | 123 | 87.65.34.21 | +----+-----+-------------+ After : +----+-----+-------------+ | id | cid | ipfx | +----+-----+-------------+ | 1 | 123 | 12.12.12.12 | | 2 | 123 | 87.87.87.87 | +----+-----+-------------+ Anybody can help me to solve this? Thanks

Aucun commentaire:

Enregistrer un commentaire