Jump to content

insert values into a database using a file


mancroft

Recommended Posts

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)\";

 

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.)  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.