justinh Posted January 23, 2009 Share Posted January 23, 2009 <?php for($i = 1; $i <= $_POST['numberinparty']; ++$i){ $fname = $_POST['' . $i .' fname']; $lname = $_POST['' . $i .' lname']; } ?> This isn't right for some reason, just wondering how you would write the $_POST part to be $_POST['1fname'] for example.. Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/ Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 <?php for($i = 1; $i <= $_POST['numberinparty']; ++$i){ $fname = $_POST[$i .'fname']; $lname = $_POST[$i .'lname']; } ?> edit: there is an easier way to do this though...in your form, you can pass arrays to PHP like this: <input type="text" name="fname[]" /> <input type="text" name="fname[]" /> <input type="text" name="fname[]" /> then in PHP you can loop over it: foreach($_POST['fname'] as $fname){ echo $fname; } Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744415 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 Right now, I have AJAX changing the div containing the firstname / lastname fields based on a drop down menu for the number of guest in party. So i'm not sure if I could do it the way you suggest. Heres the server-side script, well the part that that deals with number of guest <?php if(isset($_GET['numberofguest'])){ $number = $_GET['numberofguest']; for($i = 1; $i <= $number; ++$i){ echo " <b><br>Guest #".$i."</b><br /> <li><label for=\"".$i."fname\">First Name<input type=\"text\" name=\"".$i."fname\"><br /> <label for=\"".$i."lname\">Last Name<input type=\"text\" name=\"".$i."lname\"><br /></li><br /><br />"; } ?> That is ran onChange of the dropdown menu containing possible number of guest in party. So yeah, it probably could be done the way you said, but i think right now, its good b/c it works. Well sortof works.. have one more problem <?php for($i = 1; $i <= $_POST['numberinparty']; ++$i){ $fname = $_POST[$i .'fname']; $lname = $_POST[$i .'lname']; $queryadd = mysql_query("INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$fname', '$lname', '', '', '', '', '', '', '', '', '$_SESSION[huntid]', '', '', '')") or die(mysql_error()); echo "<label><b>Guest ".$i.":</b></label>".$fname." ". $lname."<br /><br>"; } ?> This should be inserting information into a database but it's not. Any ideas? No mysql errors come up. Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744435 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 well, is the FOR loop executing properly...aka is this line outputting info: echo "<label><b>Guest ".$i.":</b></label>".$fname." ". $lname."<br /><br>"; Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744438 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 Yes that line is displaying properly. Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744440 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 weird...i would double check your db, cus the rows should be showing up. do the queries look correct if you echo them? <?php for($i = 1; $i <= $_POST['numberinparty']; ++$i){ $fname = $_POST[$i .'fname']; $lname = $_POST[$i .'lname']; $sql = "INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$fname', '$lname', '', '', '', '', '', '', '', '', '$_SESSION[huntid]', '', '', '')"; echo $sql."<br>"; $queryadd = mysql_query($sql) or die(mysql_error()); echo "<label><b>Guest ".$i.":</b></label>".$fname." ". $lname."<br /><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744454 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 queries look fine. INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'gdggsdg', 'sdfgsdg', '', '', '', '', '', '', '', '', '2353', '', '', '') Guest 1:gdggsdg sdfgsdg INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'dgfsdg', 'sdfgsdfg', '', '', '', '', '', '', '', '', '2353', '', '', '') Guest 2:dgfsdg sdfgsdfg INSERT INTO hrclienta ( cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', 'sdfgsdg', 'sdgsdfg', '', '', '', '', '', '', '', '', '2353', '', '', '') Guest 3:sdfgsdg sdgsdfg There's a similiar query on the same page $query = mysql_query("INSERT INTO hrclienta (cl_id, cl_firstname, cl_lastname, cl_address, cl_city, cl_zip, cl_state, cl_email, cl_cell, cl_home, cl_fax, cl_partyid, cl_under, cl_hunter, cl_partycontact) VALUES('', '$_POST[fname]', '$_POST[lname]', '$_POST[address]', '$_POST[city]', '$_POST[zip]', '$_POST[state]', '$_POST[email]', '$_POST[cellphone]', '$_POST[homephone]', '$_POST[fax]', '$_SESSION[huntid]', '', '', 'y')") or die(mysql_error()); this one works.. I compared the two, and as far as i can tell, they are identical. Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744457 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 do you use phpMyAdmin to manage your database? try pasting the queries into there to see if it reports any errors. but i don't see why they wouldn't work. double check the db you are connecting to is the same db you are looking at. also, how are you confirming "This should be inserting information into a database but it's not" Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744477 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 I run the query in phpmyadmin, it says row added successfully, but the row doesn't show up in the table. This is weird. Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744497 Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 you are seeing things then double and tripple check all database and table names Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744509 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 wow... I didn't notice the pagination in phpmyadmin, there was a second page of row results!!! Duh! Quote Link to comment https://forums.phpfreaks.com/topic/142128-variable-question/#findComment-744520 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.