mancroft Posted July 13, 2003 Share Posted July 13, 2003 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)\"; ?> Quote Link to comment Share on other sites More sharing options...
xr6vn9z Posted July 13, 2003 Share Posted July 13, 2003 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. Quote Link to comment Share on other sites More sharing options...
sploit Posted July 17, 2003 Share Posted July 17, 2003 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.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.