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());

Edited by derekshull
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.