Page 1 of 1

Id Generator for importing data into MySQL

Posted: Thu Apr 10, 2014 4:30 pm
by rnott
I wrote the following php script to create Ids that can be used for importing data.
I enter the number of ids I want to create and submit returns a html table that contains the php uniqid() function ids. (Default is 50,stops at 500)

Then I copy & paste into the spreadsheet that contains the data that will be imported into mysql.

Is this the correct way to import data or is there alternative ways?
Thanks!

Code: Select all

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>Generate nuBuilder ids that can be used when importing data</h2>
<p><span class="error">* required field.</span></p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
How many ids? <input type="text" name="genCnt"><br>
<span class="error">* <?php echo $genCntErr;?></span>
<br><br>
<input type="submit">
</form>
<table border="1"><tbody>
<?php

$genCntErr="";
$genCnt="";

if($_SERVER["REQUEST_METHOD"] == "POST")
{
  if(empty($_POST["genCnt"]))
  {
    $genCntErr="Defaulted 50 ids";
    generateIds(50);
  }
  else
    {
      $genCnt = test_input($_POST["genCnt"]);
      if(is_numeric($genCnt))
      {
        generateIds($genCnt);
      }
      else
        {$genCntErr="Enter a valid Number";}
    }
}

function test_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

function generateIds($howMany)
{
   for($i=1; $i <=$howMany; $i++) {
      if ($i > 500) {
         break;
      }
      echo '<tr><td>',$i,'</td><td>',uniqid(),'</td></tr>';
   }
}

?>
</tbody></table>
<span class="error">* <?php echo $genCntErr;?></span>
<br><br>
</body>
</html>

Re: Id Generator for importing data into MySQL

Posted: Fri Apr 11, 2014 4:43 pm
by massiws
rnott,
how do you import your spreadsheet into nuBuilder?

Re: Id Generator for importing data into MySQL

Posted: Sat Apr 12, 2014 4:14 am
by rnott
I save it as .csv then import the csv with PHPMyAdmin

Re: Id Generator for importing data into MySQL

Posted: Sat Apr 12, 2014 12:25 pm
by massiws
rnott,
there isn't a preferred way to import data in nuBuilder: you can use what you want.
If all your data are stored in a csv file, adding a column width uniqid() values and then import via phpmyadmin is one possible way.

Max

Re: Id Generator for importing data into MySQL

Posted: Sun Apr 13, 2014 5:23 am
by rnott
yes its working...but just wanted to see if any better or other ways to do it.
thanks

Re: Id Generator for importing data into MySQL

Posted: Tue Apr 15, 2014 12:51 am
by admin
rnott,

This is what I would do..

(lets say you have just imported a table called account and want to add a Primary Key.)

Code: Select all

nuRunQuery('ALTER TABLE account  ADD account_id VARCHAR(25) NOT NULL FIRST,  ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER account_id');  //-- this auto populates id with numbers then

$t = nuRunQuery('SELECT * FROM account');

while($r = db_fetch_object($t)){

	$id = nuID();
	nuRunQuery("UPDATE account SET account_id = '$id' WHERE id = '$r->id'");              //--  update account_id to a unique string (better in so many ways than using numbers)
	
}

nuRunQuery("ALTER TABLE account DROP id");
nuRunQuery("ALTER TABLE account ADD PRIMARY KEY(account_id)");

print 'Done!';

Steven

Re: Id Generator for importing data into MySQL

Posted: Wed Apr 16, 2014 3:18 am
by rnott
Thanks, I will try this...this should save some steps

Re: Id Generator for importing data into MySQL

Posted: Fri Aug 08, 2014 8:34 am
by jkdev
Has anybody been able to develop a interface to upload a csv file within NuBuilderPro and then gerenate the Primary Key (ID) on the fly and update the table?

Re: Id Generator for importing data into MySQL

Posted: Tue Sep 02, 2014 8:54 pm
by jkdev
Well I did device my own method.

Here is how:

http://forums.nubuilder.cloud/viewtopic.php?f=13&t=8510

Re: Id Generator for importing data into MySQL

Posted: Fri Sep 05, 2014 4:48 am
by admin
.