Wednesday 15 August 2012

html - php update certain fields to database -



html - php update certain fields to database -

i'm trying work on inventory scheme users may view inventory , update quantity value input user , rest remains same database. not working please help me find did wrong. echo success message database isn't updated.

<form name="form" method="post"> <table width="70%" border="5" align="center"><tr> <th scope="row">sku</th> <th scope="row">item description</th> <th scope="row">current qunatity</th> <th scope="row">update quantity</th> <th scope="row">unit price</th> </tr> <tr> <th scope="row"> <?php include('connect.php'); $result = mysqli_query("select * products") or die(mysqli_error()); while($row = mysqli_fetch_array( $result )) { echo "<tr>"; echo '<td><a name="sku[]">'.$row['sku_id'].'</a></td>'; echo '<td>'.$row['description'].'</td>'; echo '<td>'.$row['quantity'].'</td>'; echo '<td><input name="qty[]" /></td>'; echo '<td>'.$row['unit_price'].'</td>'; echo "</tr>"; } ?> </table> <input style="float:right" name="update" type="submit" id="update" value="update"/> </form> <?php if(isset($_post['update'])) { $qty = $_post['qty']; $sku = $_post['sku']; foreach($qty $key => $value) { if(empty($value)) { continue; } else { $sql = "update products set quantity ='".$value."' sku_id = '".$sku[$key]."'"; mysql_query($sql); } } $retval = mysqli_query($sql); if(! $retval) { die('could not update data: '. mysql_error()); } echo 'update info successfully!'; } ?>

this should total reply (with mysqli update):

<form name="form" method="post"> <table width="70%" border="5" align="center"> <tr> <th scope="row">sku</th> <th scope="row">item description</th> <th scope="row">quantity</th> <th scope="row">unit price</th> </tr> <?php include('connect.php'); $result = mysqli_query("select * products") or die(mysqli_error()); while($row = mysqli_fetch_array( $result )) { echo "<tr>"; echo '<td>'.htmlspecialchars($row['sku_id']).'</td>'; echo '<td>'.htmlspecialchars($row['description']).'</td>'; echo '<td><input name="qty['.htmlspecialchars($row['sku_id']).']" value="'.htmlspecialchars($row['quantity']).'"/></td>'; echo '<td><input name="price['.htmlspecialchars($row['sku_id']).']" value="'.htmlspecialchars($row['unit_price']).'"/></td>'; echo "</tr>"; } ?> </table> <input style="float:right" name="update" type="submit" id="update" value="update"/> </form> <?php if(isset($_post['update'])) { $qty = $_post['qty']; $price = $_post['price']; $stmt = $mysqli->stmt_init(); // <- mysqli class way of doing $stmt->prepare("update products set quantity = ?, unit_price = ? sku_id = ?"); foreach($qty $key => $value) { $data = array($qty[$key], $price[$key], $key); $stmt->execute($sql, $data); } echo 'update info successfully!'; } ?>

for testing purposes processing of post can changed to:

if(isset($_post['update'])) { $qty = $_post['qty']; $price = $_post['price']; //$stmt = $mysqli->stmt_init(); // <- mysqli class way of doing //$stmt->prepare("update products set quantity = ?, unit_price = ? sku_id = ?"); foreach($qty $key => $value) { echo "update products set quantity = ".$qty[$key].", unit_price = ".$price[$key]." sku_id = " . $key . "<br/>\n"; //$data = array($qty[$key], $price[$key], $key); //$stmt->execute($sql, $data); } echo 'update info successfully!'; }

php html mysql

No comments:

Post a Comment