rvinikof Posted July 20, 2007 Share Posted July 20, 2007 Using AJAX, I have a drop down that lists a number of books. Once a number is picked, where q is the number picked, it does this: for($i=0; $i < $q; $i++) { echo '<b>Book author:</b> <input type = "text" name = "bookauthor"/><br/><br/>'; echo '<b>Book title:</b> <input type = "text" name = "booktitle"/><br/><br/>'; echo '<b>Book edition:</b> <input type = "text" name = "edition"/><br/><br/>'; echo '<b>ISBN:</b> <input type = "text" name = "isbn"/><br/><br/>'; echo '<b>Required or Optional?</b> <select name = "reqoropt">'; echo '<option value = "required">Required</option><option value = "optional">Optional</option></select><br/><br/>'; echo "<hr/>"; } Once this is submitted, I have requestIDs and a courseID that is inserted into a bookrequest table q number of times. That part works. I need to now insert the author, title, ect. into this table as a new line so that each author, title ect. gets a different requestID. Right now, if q is 2, my table will look like this: requestID courseID author title ect. 1 12 2 12 Instead of: requestID course ID author title ect. 1 12 author1 title1 2 12 author2 title2 I think I need to make the author, title, ect. into an array and then insert it, but everything I've tried isn't really working. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 You can make the names of inputs into an array. For example, you might have: <input type = "text" value="names[]" /> <br /> <input type = "text" value="names[]" /> <br /> <input type = "text" value="names[]" /> <br /> <input type = "text" value="names[]" /> <br /> Your php might then contain: <?php foreach($_POST['names'] as $value){ echo $value.'<br />';//would echo each of the 4 names that were entered. } ?> Obviously just an example, but you should see how you can apply it to your code. Quote Link to comment Share on other sites More sharing options...
rvinikof Posted July 20, 2007 Author Share Posted July 20, 2007 Thanks. When I echo it I get all the different values, but then what do I use to insert it? Because when I try it, it only enters the last value whatever number of times. It doesn't insert every value. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 You'll need to have your insert query inside the while/for loop. <?php for($x=0;$x<=count($yourarray);$x++){ mysql_query("INSERT INTO `yourtable` VALUES ('$somearray[$x]','$anotherarray[$x]')"); } ?> Again, just an example. Not sure where you're at so cant give anything more specific. Quote Link to comment Share on other sites More sharing options...
rvinikof Posted July 20, 2007 Author Share Posted July 20, 2007 My insert statement looks like this: for ($i = 0; $i < $Numofbooks; $i++) { $sqlinsertlogin = "INSERT INTO bookrequest (requestID, loginID, courseID, author) VALUES ('', '".$loginid[0]."', '".$crnforclass[0]."', '".$Author"')"; $insertlogin = mysql_query($sqlinsertlogin); if (!$insertlogin) { $insertloginerr = mysql_error(); echo $insertloginerr; exit(); } } If I put $Author[$i], then I get the first letter of the the very last value in one row and the second letter of the very last value in the next row and so on. If I just leave $Author, then it puts the last value in every row. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 Sounds to me like $Author is still a string. Where did you define it? Did you change the form so that it was an array? Quote Link to comment Share on other sites More sharing options...
rvinikof Posted July 20, 2007 Author Share Posted July 20, 2007 foreach($_POST["bookauthor"] as $Author) { echo $Author.'<br/>'; } I have it pulled in from the form and used in a foreach like you suggested. I mean, it is pulled from the form as a string I guess. Where am I supposed to change it? Or do I have to define it differently in my database? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 Well you dont need that foreach, since you're not actually wanting to echo the values. Try something like: <?php $Author = $_POST['bookauthor']; for ($i = 0; $i < $Numofbooks; $i++) { $sqlinsertlogin = "INSERT INTO bookrequest (requestID, loginID, courseID, author) VALUES ('', '".$loginid[0]."', '".$crnforclass[0]."', '".$Author"')"; $insertlogin = mysql_query($sqlinsertlogin); if (!$insertlogin) { $insertloginerr = mysql_error(); echo $insertloginerr; exit(); } } ?> Quote Link to comment Share on other sites More sharing options...
rvinikof Posted July 20, 2007 Author Share Posted July 20, 2007 That works if I use $Author[$i] in the Insert query. Thank you so much. 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.