Jump to content


Photo

insert values into a database using a file


  • Please log in to reply
2 replies to this topic

#1 mancroft

mancroft
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 July 2003 - 09:24 AM

What I wish to do is insert values into a database using a file (insert.php).

1. Is the layout of the file (see below) the proper way to do it?

2. Once the file is created, what do I do?, just put it on the server and then run the url http://pathtofile/insert.php

<?
//dbuser
$usr = \"yyyyyyyy\";
//dbpass
$pwd = \"xxxxxxxx\";
//dbname
$db = \"zzzzzzzzzz\";
//dbhost
$host = \"localhost\";

// connect to the sql database
$link = mysql_connect($server, $user, $pass);
$db = mysql_select_db($database, $link);

\"insert into items values(0, \'Tony\', \'Tony blah\', 23.95)\";
\"insert into items values(0, \'FI\', \'FI blah\',36.50)\";

?>

#2 xr6vn9z

xr6vn9z
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 13 July 2003 - 02:56 PM

change...

\"insert into items values(0, \'Tony\', \'Tony blah\', 23.95)\";
\"insert into items values(0, \'FI\', \'FI blah\',36.50)\";

to...

insert into items values(0, \'Tony\', \'Tony blah\', 23.95);
insert into items values(0, \'FI\', \'FI blah\',36.50);

I\'m kind of new with PHP but I think you just do what you suggested and upload the file to the server and open it in the browser.

Also, I\'ve not dealt with remote servers yet, so I am not sure if $host=\"localhost\" would be correct.

#3 sploit

sploit
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 July 2003 - 07:08 AM

No, just writing the queries would not do the job for you.

The following code will help:

[php:1:1c115473be]<?php

//db variables
$db_host = \"xyz\";
$db_name = \"xyz\";
$db_user = \"xyz\";
$db_pwd = \"xyz\";

$query1 = \"insert into items values(0, \'Tony\', \'Tony blah\', 23.95)\";
$query2 = \"insert into items values(0, \'FI\', \'FI blah\',36.50)\";

//db connection
$link = mysql_connect($db_host, $db_user, $db_pwd)
or die(\"Could not connect : \" . mysql_error());
mssql_select_db($db_name)
or die(\"Could not select database\");

//execute query
mysql_query($query1)
or die(\"Query failed : \" . mysql_error());

mysql_query($query2)
or die(\"Query failed : \" . mysql_error());

?>[/php:1:1c115473be]

However, if you have lots of lines to add, you can try the following algo:
1, save all lines in a file
2, using a PHP script, read the file line-by-line
3, in a manner similar to above, execute each line (query)

There are other ways also, you may wanna have a look at the LOAD DATA INFILE syntax.

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. If the LOCAL keyword is specified, the file is read from the client host. If LOCAL is not specified, the file must be located on the server. (LOCAL is available in MySQL Version 3.22.6 or later.)  






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users