derekshull Posted November 16, 2012 Share Posted November 16, 2012 (edited) 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 November 16, 2012 by derekshull Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 16, 2012 Share Posted November 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
derekshull Posted November 16, 2012 Author Share Posted November 16, 2012 What would be the best way to go about this then? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.