PHP mySQL Update page -
i'm trying make simple product page (no login or that) in php , mysql. far, shows on products page fine, can delete/add fine admin page. there i'm trying figure out how create edit function. made form populates mysql table based on productid convenience (edit2.php?id=x) posts edit.php , updates database. far, reusing old code isn't working, somehow hoping simple.
this add product (which works) :
<?php session_start(); if(isset($_post) && isset($_post['hp']) && empty($_post['hp'])) { if( isset($_session['token']) && $_session['token'] == $_post['token'] ) { mysql_connect('localhost', 'test_admin', 'test'); mysql_select_db('test_product'); $data = array_map('mysql_real_escape_string', $_post); $query = " insert products ( product_name, price, description, image ) values ( '{$data['product_name']}', '{$data['price']}', '{$data['description']}', '{$data['image']}' ) "; if(mysql_query($query)) { echo '<p>your information saved.</p>'; unset($_session['token']); } else { echo '<p>there error storing data, please try again later.</p>'; } } else { echo '<p>your data has been saved.</p>'; } } else { echo '<p>error.<br />please try again later.</p>'; } ?>
this update (which throws out syntax error or 'error storing data' :
<?php mysql_connect('localhost', 'test_admin', 'test'); mysql_select_db('lbriedis_product'); $data = array_map('mysql_real_escape_string', $_post); $query = "update products id = ".$pageid." ( product_name, price, description, image ) values ( '{$data['product_name']}', '{$data['price']}', '{$data['description']}', '{$data['image']}' ) "; if(mysql_query($query)) { echo '<p>your information saved.</p>'; unset($_session['token']); } else { echo '<p>there error storing data, please try again later.</p>'; echo mysql_error(); //used development , testing } ?>
i session errors when attempt use session validation because start session on admin page (add product form on admin.php, edit , delete links) ? nevertheless, removed session checks moment i'm struggling more update part.
also i'm not sure of best way pass $pageid update query 'update id = $pageid. have hidden field value (see edit form), how reference correctly?
edit form (values display correctly id = $pageid working here:
<?php $dol = "$"; $pageid = (int)$_get['id']; mysql_connect('localhost', 'lbriedis_admin', 'xxxxxx123'); mysql_select_db('lbriedis_product'); $result = mysql_query("select * products id = ".$pageid.""); if($result){ $data = mysql_fetch_assoc($result); } ?> <form id="inputform" method="post" action="edit.php"> <fieldset> <label>pool name:</label> <?php echo '<input type="text" name="product_name" value="'.$data['pool_name'].'" />'; ?> <label>price:</label> <br> <?php echo '$ <input type="text" name="price" value="'.$data['price'].'" />'; ?> <br /> <label>description:</label> <?php echo '<textarea name="description">'.$data['description'].'</textarea>'; ?> <label>image:</label></label> <?php echo '<input type="text" name="image" value="'.$data['image'].'" />'; ?> <input type="hidden" name="token" value="<?php echo $_session['token']; ?>" /> <input type="text" id="hp" name="hp" /> <?php echo '$ <input type="hidden" name="$pageid" value="'.$pageid.'" />'; ?> <br> <input type="submit" name="submit" value="update" /> </fieldset> </form>
your update query have incorrect sintax
$query = "update products id = ".$pageid." ( product_name, price, description, image ) values ( '{$data['product_name']}', '{$data['price']}', '{$data['description']}', '{$data['image']}' )
correct sintax should be
$query="update products set product_name = '".$data['product_name']."', price ='".$data['price']."', description = '".$data['description']."', image = '".$data['image']."' id = '".$pageid."'
Comments
Post a Comment