Jump to content

Updating MYSQL Data via PHP Webservice Call


PeterFrank

Recommended Posts

Hello, I'm hoping someone may be able to help me.

 

I have a php shopping cart (zen cart to be exact) and I have a web service that someone helped me write to allow retrieval of data from the MYSQL database to be used by an external website built in .net (I am a .net developer).

 

Anyway, the current web service only allows "reading" of data which is working fine, however now I would like to add an extra function that would allow the external site to update the stock levels in the Zen Cart's mysql database via a web service call.

 

This is what I have curently : 

<?php
require 'includes/configure.php';
error_reporting(0);
class stephin 
{

	function __construct($hostname,$dbuser,$dbpass,$dbname) {
		global $config; 
		if(!$this->dblink) {
			if($this->dblink=mysql_connect($hostname,$dbuser,$dbpass)){
				if(mysql_select_db($dbname,$this->dblink)){
					return $this->dblink;
				}else{
					die("Database cant be selected.");
					return false;				
				}
			}
			else{
				die("No database connection");
				return $this->dblink;
			}
		}
		else{
			return $this->dblink;
		}		
	}	

	function select($query){
		$result2=mysql_query($query,$this->dblink);
		$result3=mysql_num_rows($result2);
		if($result3 > 0){
			while($row=mysql_fetch_object($result2)){
				if($row){
					$result[] = $row;
				}
			}
			return $result;
		}
		return false;
	}
}
$db = new stephin(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD,DB_DATABASE);
$id=$_GET[id];
$action=$_GET[action];
if($action=="GetProductDetails")
{
$data=$db->select("select zen_products_description.products_id,zen_products.products_model,zen_products_description.products_name,zen_products.products_status,zen_products.products_price,zen_products.products_quantity,zen_products.products_weight,zen_products.product_is_always_free_shipping as free_shipping,zen_products.products_sort_order as sort_order,zen_products.master_categories_id as categories_id,zen_products_description.products_description,zen_products.products_image  from zen_products_description inner join zen_products on zen_products.products_id= zen_products_description.products_id  where zen_products.products_id=$id");
}
if($action=="GetProductList")
{
$data=$db->select("select zen_products_description.products_id,zen_products.products_model,zen_products_description.products_name,zen_products.products_status,zen_products.products_quantity,zen_products.products_price,zen_products.products_weight,zen_products.products_sort_order as sort_order,zen_products.master_categories_id as categories_id  from zen_products_description inner join zen_products on zen_products.products_id= zen_products_description.products_id order by zen_products_description.products_id");
}
echo json_encode($data);
?>

I have figured out that I will need something like this:

$prod_id = $_GET['prod_id'];
$prod_qty = $_GET['prod_qty'];

$sql = "UPDATE zen_products SET products_quantity = $prod_qty WHERE products_id = $prod_id" ;

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}

But I am just not sure how to glue it together with the existing code I have.

 

In addition I would also like to make sure that the quantity cannot be updated to a value less than zero.

 

Obviously I also have security issues to consider but I will worry about that once I can get this working.

 

Thanks in advance.

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.