Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2004, 05:08 PM
Authorized User
 
Join Date: Dec 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Script to upload text file to SQL with errors

I am trying to write a script to upload data from a text file to my mssql server. The example scripts I have found only take me so far, and I have so many errors that I can’t get a handle on whether this approach is even the right way of doing it. Here’s what I have:

<?php
$filehandle = "./uma_stars.txt";
// *.txt is a tab deliminated file
$fp = fopen("uma_stars.txt", "r");

//Open database
$opendb = './db.php';
include ($opendb);

while(!feof($fp))
{
$line = fgets($filehandle, 4096);
$ra = substr($line,0,11);
$dec = substr($line,12,8);
$catalog_num = substr($line,21,11);
$constellation = substr($line,37,3);

mssql_query("insert into TEST values(ra = '$ra', dec = '$dec', catalog_num = '$catalog_num',constellation = '$constellation')")
or die("sql error");
}

mssql_close($db);
fclose($fp);

?>

and my errors are:

Warning: fgets(): supplied argument is not a valid stream resource in ….\test\upload_star_data.php on line 12

Warning: mssql_query(): message: The name 'ra' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. (severity 15) in ….\test\upload_star_data.php on line 18

Warning: mssql_query(): Query failed in …\test\upload_star_data.php on line 18
sql error

Any one have some tips for me?


 
Old March 9th, 2004, 07:02 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Warning: fgets(): supplied argument is not a valid stream resource in ….\test\upload_star_data.php on line 12

You are creating a string in the $filehandle variable, but opening the file with the $fp variable (which is the resource indicator -- or the handle). You must supply the resource indicator to fgets.

Have a look at this example:
http://www.php.net/fgets

Warning: mssql_query(): message: The name 'ra' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. (severity 15) in ….\test\upload_star_data.php on line 18

Warning: mssql_query(): Query failed in …\test\upload_star_data.php on line 18
sql error

For an INSERT query you don't have to specify the column names in the VALUES portion of the SQL. You'd do that for an UPDATE query, but an UPDATE query is set-up slightly differently. Each inserted value must correspond directly to the number of columns in the database. So if you have 4 fields in your database, you should be inserting 4 values.

Here is your code with corrections..
Code:
<?php

// *.txt is a tab deliminated file
$fp = fopen("uma_stars.txt", "r");

//Open database
include ('./db.php');

while(!feof($fp))
{
    $line = fgets($fp, 4096);
    $ra = substr($line,0,11);
    $dec = substr($line,12,8);
    $catalog_num = substr($line,21,11);
    $constellation = substr($line,37,3);

mssql_query("INSERT INTO TEST values('$ra', '$dec', '$catalog_num', '$constellation')")
or die(mysql_error());
}

mssql_close($db);
fclose($fp);

?>
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old March 10th, 2004, 12:00 PM
Authorized User
 
Join Date: Dec 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It worked allthough I did have to change the mssql_query because I have other colums in my table that where being left null. So I used:

mssql_query( "INSERT INTO Test ( ra, dec, catalog_num, constellation ) VALUES ( '$ra', '$dec', '$catalog_num', '$constellation' )" ) or die( 'SQL Error.' );

Also after that I was still getting an "message: Error converting data type varchar to numeric." so I changed my colum types in the table for the upload. I will change it back afterwards.

Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
file upload script problem kale_tushar ASP.NET 1.0 and 1.1 Professional 7 November 30th, 2006 05:08 PM
Upload file to server from ASP script crmpicco Classic ASP Components 2 June 14th, 2006 01:25 AM
asp file upload script having problems with MYSQL paulmcn Classic ASP Databases 0 September 16th, 2005 01:26 PM
Upload a Text file from a Web Client to SQL Server petercyriljones Classic ASP Professional 0 May 20th, 2005 10:26 AM
multiple file upload errors Laurie PHP How-To 0 August 15th, 2004 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.