Array shows perfect but never inserts

[php]<?php
$dbhost = ‘localhost’;
$dbuser = ‘xxxx’;
$dbpass = ‘xxxxx’;
$dbname = ‘survey’;
$dblink = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
mysql_select_db($dbname, $dblink);
$csvfile = “survey.csv”;
$seperator = “|”;
$lines = file($csvfile);
foreach($lines as $line) {
$rows = explode($seperator,$line);
echo “

”;
print_r($rows);
echo “
”;
//exit;

$check = mysql_query(“SELECT token FROM lime_tokens_67937 WHERE token = ‘$uniquecsvfield1’ AND attribute_2 = $uniquecsvfield2 LIMIT 1”);
if(mysql_query($check) == 0) //If no rows are returned

$sql=(“INSERT INTO lime_tokens_67937 set
firstname=’”.$rows[2]."’,
lastname=’".$rows[3]."’,
token=’".$rows[4]."’,
validfrom=’".date(‘Y-m-d’)."’,
validuntil=’".date(‘Y-m-d’, strtotime(’+30 days’))."’
attribute_1=’".$rows[4]."’,
attribute_2=’".$rows[5]."’,
attribute_3=’".$rows[6]."’,
attribute_4=’".$rows[7]."’,
attribute_5=’".$rows[8]."’,
attribute_6=’".$rows[1]."’")

or die("Insert Error: ".mysql_error());
echo $sql ;
$result = mysql_query($sql, $dblink);
}
//@unlink($csvfile);
?>[/php]

This is the output from the echo

Array
(
    [0] => 39550     
    [1] => TONY SANTANGELI               
    [2] =>                               
    [3] =>                               
    [4] => 01011011043087    
    [5] => 12/06/11
    [6] => 303 
    [7] => DRASKO S                 
    [8] =>          .00
    [9] => 

)

INSERT INTO tokens set firstname=' ', lastname=' ', token='01011011043087 ', validfrom='2011-12-06', validuntil='2012-01-05' attribute_1='01011011043087 ', attribute_2='12/06/11', attribute_3='303 ', attribute_4='DRASKO S ', attribute_5=' .00', attribute_6='TONY SANTANGELI '

But it never gets inserted in to the database. I’m pulling what little hair I have left been looking at this for a week now and can’t see where I screwed up. This is my first php code attempt any help you can offer would be great or just tell me I’m stupid for missing some thing that is under my nose works too! ???

TANKS A BILLION!

Probably there is some field type mismatch or wrong field name reference. But to find out what definitely is wrong, try to echo mysql_error() after the mysql_query() function call (currently you have mysql_error() before executing the query, which make no sense).
[php]$result = mysql_query($sql, $dblink);
echo mysql_error();[/php]

Also, remove this line from your code:
[php]or die("Insert Error: ".mysql_error());[/php]

and add semicolon at the end of this line:
[php]attribute_6=’".$rows[1]."’")[/php]

Hello Keith, you using wrong $sql query for inserting data into database table. Replace your $sql qurey

[php]
$sql=(“INSERT INTO lime_tokens_67937 set
firstname=’”.$rows[2]."’,
lastname=’".$rows[3]."’,
token=’".$rows[4]."’,
validfrom=’".date(‘Y-m-d’)."’,
validuntil=’".date(‘Y-m-d’, strtotime(’+30 days’))."’
attribute_1=’".$rows[4]."’,
attribute_2=’".$rows[5]."’,
attribute_3=’".$rows[6]."’,
attribute_4=’".$rows[7]."’,
attribute_5=’".$rows[8]."’,
attribute_6=’".$rows[1]."’")
[/php]

With below code

[php]
#NOTE: if any field is define as integer in table than remove single quotes.
$sql=“INSERT INTO lime_tokens_67937 (firstname, lastname, token, validfrom, validuntil, attribute_1, attribute_2, attribute_3, attribute_4, attribute_5, attribute_6,) VALUES (’”.$rows[2]."’ ,’".$rows[3]."’,’".$rows[4]."’,’".date(‘Y-m-d’)."’,’".date(‘Y-m-d’, strtotime(’+30 days’))."’,’".$rows[4]."’,’".$rows[5]."’,’".$rows[6]."’,’".$rows[7]."’,’".$rows[8]."’,’".$rows[1]."’)";

[/php]
I hope this will helpful for you.
Reply Your Feedback
SR

Sponsor our Newsletter | Privacy Policy | Terms of Service