Jump to content

mysql database table row not updating


hance2105
Go to solution Solved by hance2105,

Recommended Posts

hello guys i worked out the codes to update a row in my sql database table. in fact i select the product id and some fields are populated automatically. the category, subcategory and photo upload do not populate as those have to be done by the user when updating the product. i think there is some issue with my update code below. can anyone help?

 

<?php
 
if(isset($_POST['update'])){
 
include('db_connect.php');
 
$prod_id=$_POST['prod_id'];
$prod_name=$_POST['prod_name'];
$prod_brand=$_POST['prod_brand'];
$prod_desc=$_POST['prod_desc'];
$prod_price=$_POST['prod_price'];
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$prod_w_c=$_POST['prod_w_c'];
$prod_photo=$_POST['prod_photo'];
$url='upd_prod.php';
 
$sql=$sql_query("UPDATE tbl_product SET prod_name='$prod_name', prod_brand='$prod_brand', prod_desc='$prod_desc', prod_price='$prod_price', cat='$cat', subcat='$subcat', prod_w_c='$prod_w_c', prod_photo='$prod_photo' WHERE prod_id='$prod_id'");
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
else 
{
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
echo "<script>alert('Product successfully updated')</script>";
 
}
 
}
 
?> 
 
Link to comment
Share on other sites

First of wrap you code:

 // Check to see if edited data has been submitted by the user:
 if (isset($_POST['action']) && $_POST['action'] == 'enter') {
	 
	 // Determine if post should be sticky or not:
	 $sticky = $_POST['sticky'];
	 if(!$user->isAdmin()) {
		 $sticky = 'no';
	 }
	 
	 // Update the edited text:	 
	 $query = 'UPDATE pages
			 SET creatorId   =  :creatorId,
			    sticky       =  :sticky,
				title        =  :title,
				content      =  :content,
				dateUpdated  =  NOW()
			 WHERE id=:id';
			 
      // Prepare the Statement:
	 $stmt = $pdo->prepare($query);
	 
	 // Clean-up user content:
	 // Setup an array - 'title' and 'content' are the keys:
	 $data = array('title' => $_POST['title'], 'content' => $_POST['content']);

	 // Create an new instance:
	 $dirtyWord = new DirtyWord($data);
	 
	 // Check you content for bad language:
	 $title = $dirtyWord->checkTitle;
	 $content = $dirtyWord->checkContent;			 
	 
	 // execute the statement:
	 $show_details = $stmt->execute(array(':creatorId' => $user->getId(), ':sticky' => $sticky, ':title' => $title, ':content' => $content, ':id' => $page->getId()));
	 	 	 	
 }

Secondly what you are you using is depreciated http://www.php.net/manual/en/function.mysql-connect.php

and I would advised you reading up on this then using either: http://us1.php.net/manual/en/mysqli.construct.php (mysqli) or http://us1.php.net/manual/en/pdo.construct.php (pdo)

 

Lastly the above code is how you update something in PDO, it was written in OOP but php.net shows examples on how to do things the procedural way and once you get code written the correct way I'm sure people here will gladly help you out if you are stock.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.