Jump to content

On Duplicate Username Go To Next Null Column


derekshull

Recommended Posts

In the code below I'm trying to make it so that when they submit a newfoldername if the username is already in the database it will just add that newfoldername to the NEXT NULL COLUMN. Stumped. Thanks for the help!

 

global $user;
$username = $user->name;
$newfoldername = $_POST['newfoldername'];


$query = "INSERT INTO folders (username, folder1)
VALUES ('$username', '$newfoldername')
ON DUPLICATE KEY UPDATE
(NEXT NULL COLUMN)='$newfoldername'";


mysql_query($query) or die("Query: $query
Error: ".mysql_error());

There's no easy way to do this in a query because your table design is laid out like a spread-sheet, not as a database table.

 

You should have ONE ROW per data item. Then the problem is simple, just insert a new row if the existing data is not already in the table.

To expand on PFMaBiSAd's response. You have a many-to-one relationship which necessitates two tables. Since you are inserting into a table called "folders" I assume you already have a users table (if not you need one). Also, your users table should have an auto-increment ID field for the purposes of "relating" data between tables (thus the term relational database).

 

So, I would expect to see two tables with at least these fields

 

users

user_id, username, etc.

 

folders

folder_id, user_id, foldername

 

Now, for every folder that a user has there will be a unique record in the folders table. A user can have no folders, 1 folder, or many folders. If you want to limit the number of folders a user has you can take care of that in the code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.