Forum Moderators: coopster

Message Too Old, No Replies

Automatically update database

         

Joppiesaus

2:10 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Hello,

I currently have a webshop with products that are shipped from a big wholesaler under my name. This wholesaler has a database with all the products in it which also contains the variable "status" which contains information if the product is on stock or not.

I use the same ID numbers (variable productid) so that it should be possible to look up the id number and update my database with the correct delivery time.

This is the php code of the wholesaler to get the product information:

<?php
// Example

$login = "####";
$password = "####";

$filename = "http://example.com/retail/prijslijst_excel.php?choosecell=TRUE=&tilde=TRUE&productid=on&retailprice=on&name=on&content=on&info=on&price=on&category=on&brand=on&status=on&btw=on&gewicht=on&admin_username=".$login."&admin_password=".$password;

$lines = file($filename);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br /><BR>\n";
$column = explode ( '"~"', $line ); // gescheidingsteken "~"
echo $column[0] . " productid <BR>";
echo $column[1] . " naam <BR>";
echo $column[2] . " info <BR>";
// ETC
//Voorbeeld sqlcode $sql = "INSERT INTO `product` ( `productid` , `name` , `info`) VALUES ('".$column[0]."', '".$column[1]."', '".$column[2]."')";
echo '<HR>';
}

// www.example.com
?>

With this code it is not a problem to echo all product information from their database on a .php page. However, the ONLY thing I need is the "status" variable updated, all the other fields like content are filled with my own stuff (SEO - unique content n all ;) ).

So, if this get gives an id number 10000, it should look up if
10000 also exists in my variable "product.productid" and then update the variable "product.status" with their status variable.

However, this is completely new to me and I couldreally usesome help.

thanks.

Joppiesaus

11:34 am on Dec 12, 2008 (gmt 0)

10+ Year Member



I got a little bit further. The complete code now looks like this:

<?php require_once('Connections/sosql.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_sosql, $sosql);
$query_Recordset1 = "SELECT * FROM product";
$Recordset1 = mysql_query($query_Recordset1, $sosql) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cron - update voorraadstatus</title>
</head>
<body>
<?php

$login = "#*$!X";
$password = "#*$!X";

// index number for status field
$voorraadIndex = 10;

// index number for product ID
$productidIndex = 0;

$filename =
"http://example.com/retail/prijslijst_excel.php"
."?veldenkiezer=TRUE=&tilde=TRUE&productid=on&verkoopprijs=on"
."&naam=on&omschrijving=on&info=on&inkoopprijs=on&categorie=on"
."&merk=on&voorraad=on&btw=on&gewicht=on"
."&admin_username=".$login
."&admin_password=".$password;

$lines = file($filename);
foreach ($lines as $line) {
$columns = explode('"~"', $line);
$voorraad = mysql_real_escape_string($columns[$voorraadIndex]);
$productid = mysql_real_escape_string($columns[$productidIndex]);
$query = "
SELECT COUNT(*) FROM `testuh`
WHERE id ='{$productid}'
";
$result = mysql_query($query);
$hits = (integer) mysql_result($result, 0, 0);
if ($hits == 1) {
$query = "
UPDATE `testuh` SET `levertijd`='{$voorraad}'
WHERE id ='{$productid}'
";
$update = mysql_query($query);
}
}
?>

The field "levertijd" (with the current delivery status information of the product) is changed. However, they are now all empty. So they are erased, but new information is not updated...

I cant seem to find out whats going wrong..

Joppiesaus

2:28 pm on Dec 13, 2008 (gmt 0)

10+ Year Member



Hello,

I currently have a webshop with products that are shipped from a big wholesaler under my name. This wholesaler has a database with all the products in it which also contains the variable "status" which contains information if the product is on stock or not.

I use this code:

<?php require_once('Connections/sosql.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_sosql, $sosql);
$query_Recordset1 = "SELECT * FROM product";
$Recordset1 = mysql_query($query_Recordset1, $sosql) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cron - update voorraadstatus</title>
</head>
<body>
<?php

$login = "#*$!X";
$password = "#*$!X";

// index number for status field
$voorraadIndex = 8;

// index number for product ID
$productidIndex = 0;

$filename =
"http://example.com/retail/prijslijst_excel.php"
."?veldenkiezer=TRUE=&tilde=TRUE&productid=on&verkoopprijs=on"
."&naam=on&omschrijving=on&info=on&inkoopprijs=on&categorie=on"
."&merk=on&voorraad=on&btw=on&gewicht=on"
."&admin_username=".$login
."&admin_password=".$password;

$lines = file($filename);
foreach ($lines as $line) {
$columns = explode('"~"', $line);
$voorraad = mysql_real_escape_string($columns[$voorraadIndex]);
$productid = mysql_real_escape_string($columns[$productidIndex]);
$query = "
SELECT COUNT(*) FROM `testuh`
WHERE id ='{$productid}'
";
$result = mysql_query($query);
$hits = (integer) mysql_result($result, 0, 0);
if ($hits == 1) {
$query = "
UPDATE `testuh` SET `levertijd`='{$voorraad}'
WHERE id ='{$productid}'
";
$update = mysql_query($query);
}
}
?>

This code works fine now. However, I still have a few questions:
1. is it possible to give products that havent matched any of the id numbers of the wholesaler a status code EOL (end of life) so that I do not need to delete deleted items at the wholesaler, but I can just update their status so they cant be ordered anymore?
2. Is there any danger when updating that empty fields are imputted (because of error or something)? This would be very bad because empty "voorraad"/staus fields means the product cant be ordered.

Hope to hear what you guyz think about this.

[edited by: Joppiesaus at 2:29 pm (utc) on Dec. 13, 2008]

coopster

10:17 pm on Dec 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi Joppiesaus --

I suppose the first one would be fairly easy ... run an UPDATE on your table for the items that have not matched an ID number to set the status code as you stated.

The second ... I'm not certain you have offered enough information for us to help you with out thoughts.