Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Id Generator for importing data into MySQL

Locked
rnott
Posts: 31
Joined: Fri Mar 14, 2014 6:14 pm

Id Generator for importing data into MySQL

Unread post 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>
massiws
Posts: 503
Joined: Thu May 24, 2012 2:08 am
Location: Milan, Italy
Contact:

Re: Id Generator for importing data into MySQL

Unread post by massiws »

rnott,
how do you import your spreadsheet into nuBuilder?
rnott
Posts: 31
Joined: Fri Mar 14, 2014 6:14 pm

Re: Id Generator for importing data into MySQL

Unread post by rnott »

I save it as .csv then import the csv with PHPMyAdmin
massiws
Posts: 503
Joined: Thu May 24, 2012 2:08 am
Location: Milan, Italy
Contact:

Re: Id Generator for importing data into MySQL

Unread post 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
rnott
Posts: 31
Joined: Fri Mar 14, 2014 6:14 pm

Re: Id Generator for importing data into MySQL

Unread post by rnott »

yes its working...but just wanted to see if any better or other ways to do it.
thanks
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Id Generator for importing data into MySQL

Unread post 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
rnott
Posts: 31
Joined: Fri Mar 14, 2014 6:14 pm

Re: Id Generator for importing data into MySQL

Unread post by rnott »

Thanks, I will try this...this should save some steps
jkdev
Posts: 20
Joined: Fri Aug 01, 2014 2:26 pm

Re: Id Generator for importing data into MySQL

Unread post 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?
jkdev
Posts: 20
Joined: Fri Aug 01, 2014 2:26 pm

Re: Id Generator for importing data into MySQL

Unread post by jkdev »

Well I did device my own method.

Here is how:

http://forums.nubuilder.cloud/viewtopic.php?f=13&t=8510
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Id Generator for importing data into MySQL

Unread post by admin »

.
Locked