herghost Posted May 4, 2009 Share Posted May 4, 2009 If I want to enter some values into an array like so: $newarray = array($_POST['v1'], $_POST['v2'], $_POST['v3']); $commaseparated = implode(',', $newarray); mysql_query("insert into table set valuess='$commaseparated'") or die('error: '.mysql_error()); Is there a way of having a value as 0? Ie sometimes the values will just be v1 and v2 and v3 will not be needed. I hope this makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/ Share on other sites More sharing options...
the182guy Posted May 4, 2009 Share Posted May 4, 2009 I'm not sure what you mean.... could you explain in more detail? Your SQL statement is invalid because there is no 'SET' inside an INSERT INTO statement. Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825602 Share on other sites More sharing options...
.josh Posted May 4, 2009 Share Posted May 4, 2009 in your database > table where you setup the column you can assign a default value. I'm kind of curious as to why you are hardcoding an array only to turn around and implode it though...why not just format it the way you want to to begin with... Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825604 Share on other sites More sharing options...
herghost Posted May 4, 2009 Author Share Posted May 4, 2009 Basically the amount of fields will be different each time. Please ignore the sql statement for the time being, just quickly showing what I was tring to achieve. as an example, say I am uploading details of a book, the book has its own unique identifier in a database, and an author. I then want to upload the chapter names, obviously each book has a different amount of chapters. Hope that makes more sense Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825617 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Basically the amount of fields will be different each time. Well that sums it up. Someone needs to take up a DB design course. Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825653 Share on other sites More sharing options...
herghost Posted May 4, 2009 Author Share Posted May 4, 2009 Basically the amount of fields will be different each time. Well that sums it up. Someone needs to take up a DB design course. Why? When its inserted into the database it will all go in one field. Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825660 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Why? When its inserted into the database it will all go in one field. Say you want to look up what book has a certain chapter name. You'll have to grab ALL the entries, implode the string, loop the array. Though if you had to say that, then you need to take a DB design course because you didn't know why. Not to be mean, but I'm just telling it like it is. Read Crayon Violent's post. Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825665 Share on other sites More sharing options...
herghost Posted May 4, 2009 Author Share Posted May 4, 2009 Ok, well I dont want to be able to search the field, I just want to echo the result. Perhaps I should just have the user add all the chapters in a text box, one per line, store them all in one field and tehn just echo teh field? Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825687 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Ok, well I dont want to be able to search the field, I just want to echo the result. Perhaps I should just have the user add all the chapters in a text box, one per line, store them all in one field and tehn just echo teh field? Yeah. You would have one entry in the DB for each chapter. The table should be: id PRIMARY_KEY book_id chapter_name Quote Link to comment https://forums.phpfreaks.com/topic/156779-array-question/#findComment-825691 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.