View Full Version : Help Me Please
ranjitcool
02-07-2005, 04:40 AM
Hello
I Have A Problem
I Have Like 170 College Names That Need To Be Inserted In My Database(mysql)
The Tabel Consists Of Name And Location Feilds
Is There Anyway For Me To Just Insert All The Records Dynamically
I Mean Provide The Php Script With 170 Names And Run A While Or A For Loop Which Wud Insert Each One ?
If Yes Please Provide Me With The Logic Or The Idea
Rightnow I Made A Form And Am Inserting One Record A Time :(
cooleo100d
02-07-2005, 04:51 AM
Hi,
You can do something like this:
<?php
$names[] = 'Name 1';
$names[] = 'Name 2'; // You would keep on going
$num_names = 2; // Set that to the number of names
$x = 0;
while ($x < $num_names)
{
$conn=@mysql_connect ("host", "dbusername","dbpassword")
or die ("Could not connect") ;
$rs = @mysql_select_db ("dbname", $conn)
or die ("Couldn't connect") ;
$sql = "insert into table (name)
values ( \"$names[$x]\" )";
$rs=mysql_query ($sql,$conn) ;
$x++;
}
?>
Hope this helps,
~David
meena
02-07-2005, 05:53 AM
If it just means to insert the records into your db table, Use Mysql Front. It is like entering in Excel Sheet. But if you are looking at creating a Form, where users can fill the details of the college, etc... you have no choice but to fill everything in the form and submit to a PHP script.
Fou-Lu
02-07-2005, 09:40 AM
You could also use XML or CVS to do the same. But in either cases, it would once again assume that you have already created information in some way or another.
Unfortunatly, no matter how you cut it, you cannot get around generating information in the first place. If you take a text file:
college1; college2; college3; etc
You could essentially use this to your advantage, by reading the file, and breaking it at each semi-colon and inserting it from there. But once again, you still need the file.
You can also alter your form if you would like so you can insert several of these into it without needing to send it. A simple example:
<form action="update.php" method="post">
<input type="text" name="college[]" value="" />
<input type="text" name="college[]" value="" />
<input type="text" name="college[]" value="" />
<input type="text" name="college[]" value="" />
<input type="text" name="college[]" value="" />
<input type="hidden" name="action" value="doupdate" />
<input type="submit" name="submit" value="submit" />
</form>
update.php
if ($_POST['action'] == 'doupdate')
{
$college = array();
$college = $_POST['college'];
foreach ($college AS $key=>$value)
{
//... run an insert here, watch your escaping and validation so you protect yourself from sql injection
}
}
Spookster
02-07-2005, 12:42 PM
Hello
I Have A Problem
I Have Like 170 College Names That Need To Be Inserted In My Database(mysql)
The Tabel Consists Of Name And Location Feilds
Is There Anyway For Me To Just Insert All The Records Dynamically
I Mean Provide The Php Script With 170 Names And Run A While Or A For Loop Which Wud Insert Each One ?
If Yes Please Provide Me With The Logic Or The Idea
Rightnow I Made A Form And Am Inserting One Record A Time :(
Read http://www.codingforums.com/postguide.htm
ranjitcool
02-08-2005, 05:05 AM
oh hey thanx for all da help but i really liked the first code method...it was easy to implement with the records though!
i was also thinking of using an array and putting all the data in the array and then using the array and loading it..
eg: array t=('name1','name2');
then
while(x<100)
{
use insert query and insert each item like t[0],t[1] and so on!
}
:p
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.