You are here:  » Cant Insert into database


Cant Insert into database

Submitted by OZZ on Sat, 2007-07-07 13:34 in

Hi.
Great tool By the way.
im just a newbe to this so please be patient.

i cant seem to insert in to my database..keep getting unexpected T_STRING..
i cant see what im doing wrong.
here is a look at the code.

<?php
  
require("includes/magicparser.php");
  
mysql_connect("localhost""xxx""xxx") or die(mysql_error());
  
mysql_select_db("test") or die(mysql_error());
  function 
myRecordHandler($product)
  {
  
$sql =
  
"INSERT INTO target_test (stockcode,product_description,extendeddescription) VALUES
'"
.mysql_real_escape_string($product["STOCKCODE"])."',
'"
.mysql_real_escape_string($product["PRODUCT_DESCRIPTION"])."',
'"
.mysql_real_escape_string($product["EXTENDEDDESCRIPTION"])."';
  mysql_query(
$sql);
  }
   
$url = "https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx";
  
MagicParser_parse($url,"myRecordHandler","xml|RESPONSE/PRODUCT/");
?>

can anyone see where im going wrong??
its been doing my head in for ages
Thanks i hope
mark

Submitted by support on Sat, 2007-07-07 15:28

Hello Mark,

I think you're just missing a closing " off the end of the line where you build your SQL. Try this:

<?php
  
require("includes/magicparser.php");
  
mysql_connect("localhost""xxx""xxx") or die(mysql_error());
  
mysql_select_db("test") or die(mysql_error());
  function 
myRecordHandler($product)
  {
  
$sql =
  
"INSERT INTO target_test (stockcode,product_description,extendeddescription) VALUES
'"
.mysql_real_escape_string($product["STOCKCODE"])."',
'"
.mysql_real_escape_string($product["PRODUCT_DESCRIPTION"])."',
'"
.mysql_real_escape_string($product["EXTENDEDDESCRIPTION"])."'";
  
mysql_query($sql);
  }
   
$url "https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx";
  
MagicParser_parse($url,"myRecordHandler","xml|RESPONSE/PRODUCT/");
?>

Hope that does the trick!
Cheers,
David.

Submitted by OZZ on Sat, 2007-07-07 17:29

Thanks for that dave.
iv just tried that. i get no error this time but table is not getting any items.
any clue?

Submitted by support on Sat, 2007-07-07 17:35

Hi,

Your basic structure looks OK - I think you may need brackets around the values. In addition, when creating database loading script such as this, it is always handy to EMPTY the table before you run the import. It is also handy to print out any MySQL error message if the query doesn't work as often this will identify the problem. I've added this code, and the brackets required to the version below - see how you get on with this:

<?php
  
require("includes/magicparser.php");
  
mysql_connect("localhost""xxx""xxx") or die(mysql_error());
  
mysql_select_db("test") or die(mysql_error());
  
$sql "DELETE FROM target_test";
  
mysql_query($sql);
  function 
myRecordHandler($product)
  {
     
$sql =
       
"INSERT INTO target_test
          (
          stockcode,
          product_description,
          extendeddescription
          )
          VALUES
          (
          '"
.mysql_real_escape_string($product["STOCKCODE"])."',
          '"
.mysql_real_escape_string($product["PRODUCT_DESCRIPTION"])."',
          '"
.mysql_real_escape_string($product["EXTENDEDDESCRIPTION"])."'
          )
       "
;
     if (!
mysql_query($sql))
     {
       
// SQL failed, print error message and abort
       
print mysql_error();exit();
     }
  }
  
$url "https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx";
  
MagicParser_parse($url,"myRecordHandler","xml|RESPONSE/PRODUCT/");
?>

Cheers,
David.

Submitted by OZZ on Sun, 2007-07-08 15:05

Hi dave
Thanks for the quick reply.
i tried the last post. it is still not filling the database but, not displaying any error either.
yet it will delete any data in the tables, i filled with some info maually to try.
any clues.?
Cheers
Mark

Submitted by support on Sun, 2007-07-08 18:33

Hi Mark,

Firstly, have you confirmed that your record handler function is being called - in other words that you are receiving the XML correctly, and then parsing it with an appropriate format string? You need to double check this before moving on to debug your SQL. Both of these can be done with print statements. Here's a modified version of the script above, that counts the records, and then displays the last SQL statement constructed:

<?php
  
require("includes/magicparser.php");
  
mysql_connect("localhost""xxx""xxx") or die(mysql_error());
  
mysql_select_db("test") or die(mysql_error());
  
$sql "DELETE FROM target_test";
  
mysql_query($sql);
  
$sql "";
  
$counter 0;
  function 
myRecordHandler($product)
  {
     global 
$counter;
     global 
$sql;
     
$counter++;
     
$sql =
       
"INSERT INTO target_test
          (
          stockcode,
          product_description,
          extendeddescription
          )
          VALUES
          (
          '"
.mysql_real_escape_string($product["STOCKCODE"])."',
          '"
.mysql_real_escape_string($product["PRODUCT_DESCRIPTION"])."',
          '"
.mysql_real_escape_string($product["EXTENDEDDESCRIPTION"])."'
          )
       "
;
     if (!
mysql_query($sql))
     {
       
// SQL failed, print error message and abort
       
print mysql_error();exit();
     }
  }
  
$url "https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx";
  
MagicParser_parse($url,"myRecordHandler","xml|RESPONSE/PRODUCT/");
  print 
"<p>Processed ".$counter." records.</p>";
  print 
"<p>Last SQL statement was: ".$sql."</p>";
?>

Now, if you see something like this:

Processed 0 records.
Last SQL statement was:

Then that means that either your URL is wrong, or the server you connect to is not currently returning any XML, or the format string (XML|RESPONSE/PRODUCT/) is wrong, so check these first.

Moving on to debug the SQL, what you want to see is this (for example):

Processed 18 records.
Last SQL statement was: INSERT INTO ... etc. etc.

In this instance, it means you are parsing records correctly, but your SQL is not working. What I would do then is copy and paste the SQL into a MySQL admin tool (e.g. phpMyAmin - or whatever you use to manage your database) and see how it behaves when entered directly.

Hope this helps!
Cheers,
David.

Submitted by OZZ on Thu, 2007-08-02 19:39

Thanks David.
works fine, turned out it wouldnt work localy for some reason, works fine on my host.

having a small problem trying to select specific items ie. pics from a specific category, is it possible?.
iv had a look in the fourm and tried this.

<?php
  
require("includes/MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($record)
  {
       global 
$counter;
 if ((
$link["CATEGORY"] == "ca3b"))
 {
  
$counter++;
    print 
"<img src='".$record["IMAGEURL"]."' />";
  }
       }
  
MagicParser_parse("https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx","myRecordHandler","xml|RESPONSE/PRODUCT/");
  if (!
$counter)
    {
    print 
"none <br />";
  }
?>

it returns (none)

any ideas.?

Cheers
Mark

Submitted by support on Thu, 2007-08-02 19:42

Hi mark,

One tiny error in the code you posted - myRecordHandler() is called with $record as the variable, but the code you have added uses $link..

Try this:

<?php
  
require("includes/MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($record)
  {
       global 
$counter;
 if ((
$record["CATEGORY"] == "ca3b"))
 {
  
$counter++;
    print 
"<img src='".$record["IMAGEURL"]."' />";
  }
       }
  
MagicParser_parse("https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx","myRecordHandler","xml|RESPONSE/PRODUCT/");
  if (!
$counter)
    {
    print 
"none <br />";
  }
?>

All i've done is change $link["CATEGORY"] to $record["CATEGORY"]...

Hope this helps!
Cheers,
David.

Submitted by OZZ on Thu, 2007-08-02 20:01

cheers for that.
think i need a break.LOL
i tried that still get response (none).should it work like an sql statement?

cheers
mark

Submitted by support on Thu, 2007-08-02 20:10

Hi Mark,

The code to this extent looks fine, but it is of course possible to get no results if there is a problem accessing the feed, or if it is actually true that none of the products have a CATEGORY field that equals "ca3b".

Firstly, always confirm that your parsing code is working correctly by adding simple debug code, for example:

<?php
  
require("includes/MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($record)
  {
       print 
"Yes, we've got records!";exit();
       global 
$counter;
 if ((
$record["CATEGORY"] == "ca3b"))
 {
  
$counter++;
    print 
"<img src='".$record["IMAGEURL"]."' />";
  }
       }
  
MagicParser_parse("https://www.targetsecure.co.uk/xml/tcxml.asp?action=ALLPRODUCTS&account=xxx","myRecordHandler","xml|RESPONSE/PRODUCT/");
  if (!
$counter)
    {
    print 
"none <br />";
  }
?>

This script should just print "Yes, we've got records!" and then exit. If this does not happen, it would imply that either the feed is not being returned in response to the URL, or that the format string is wrong (you could use the demo tool on this website to check it out).

The next step would be to study the feed manually and have a look at the category values in order to visually confirm that there is at least one category field of that value. If it looks like the case, if you could perhaps email me or post a link to where I can download the feed i'll take a look for you - reply to your reg code or forum registration email is the easiest way to get me...

Cheers,
David.