PHP MySQL inserting 3 different form values to 4 different tables -
i need php forms. trying insert @ least 2 & max of 3 team players data mysql registration purposes.
by using loop 3 form values , iterate until data stored in tables
first table players table: columns names follows.
playerid, playerfirstname, playerlastname, playerdob, playernationalranking, playerphonenumber, playeremailaddress, playerpassportnumber,playerplayinghistory, playeraddress, playercountry
second table participants table: columns names follows.
playerid, participtionid, championshipid, countryid, created_at, status (is deleted or not) , room_booking_status
third table photo links
time, filelocation, ip, idusers, playerid
on top of that, need insert 4th table , teamplayers table: column names follows.
teamid, participtionid, countryid, idplayer1, idplayer2, idplayer3, timestamp
php code starts follows:
if (!isset($_session)) { session_start(); } $editformaction = $_server['php_self']; if (isset($_server['query_string'])) { $editformaction .= "?" . htmlentities($_server['query_string']); } $colname_numrows = "-1"; if (isset($_session['mm_usergroup'])) { $colname_numrows = $_session['mm_usergroup']; } if ((isset($_post["mm_insert"])) && ($_post["mm_insert"] == "form1")) { for($i=1;$i<4;$i++) { // combining date of birth $playerdob = $_post['year'.$i]."-".$_post['month'.$i]."-".$_post['day'.$i]; $playerdob = mysql_real_escape_string($playerdob); // first query $insertplayerinfo = sprintf("insert players (playerid, playerfirstname, playerlastname, playerdob, playernationalranking, playerphonenumber, playeremailaddress, playerpassportnumber, playerplayinghistory, playeraddress, playercountry) values ('', %s, %s, '$playerdob', %s, %s, %s, %s, %s, %s, %s)", getsqlvaluestring(str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($_post['playerfirstname'.$i])))), "text"), getsqlvaluestring(str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($_post['playerlastname'.$i])))), "text"), getsqlvaluestring($_post['playernationalranking'.$i], "int"), getsqlvaluestring($_post['playerphonenumber'.$i], "text"), getsqlvaluestring($_post['playeremailaddress'.$i], "text"), getsqlvaluestring($_post['playerpassportnumber'.$i], "text"), getsqlvaluestring($_post['playerplayinghistory'.$i], "text"), getsqlvaluestring($_post['playeraddress'.$i], "text"), getsqlvaluestring($_post['playercountryid'.$i], "int")); mysql_select_db($db, $entrysystem); if($myplayerinforesult = mysql_query($insertplayerinfo, $entrysystem)) { $_session['playerid'.$i] = mysql_insert_id($entrysystem) or die(mysql_error()); // second query $insertsql = sprintf("insert participants (playerid, participationid, championshipid, countryid, created_at, status, room_booking_status) values ('".$_session['playerid'.$i]."','', %s, %s, %s, '1', '0')", getsqlvaluestring($_post['championshipid'], "int"), getsqlvaluestring($_post['countryid'], "int"), getsqlvaluestring($_post['created_at'], "date")); mysql_select_db($db, $entrysystem); if($myinsertsql = mysql_query($insertsql, $entrysystem)) { // if submitted, proceed next page $participationid = mysql_insert_id($entrysystem)or die(mysql_error()); $_session['participationid'.$i] = $participationid; } if(!is_dir("upload/".$_session['mm_username'])) { mkdir("upload/".$_session['mm_username']); } function savedata(){ global $_files, $_post, $putitat; $sql = "insert `db`.`photos` ( `time`, `filelocation`, `ip`, `countryid`, `participationid`) values (now(),'".mysql_real_escape_string($putitat)."', '". $_server['remote_addr']."', '". $_session['mm_usergroup']."' ,'" .$_session['participationid'.$i]."');"; mysql_query($sql) or die(mysql_error()); } $putitat = "upload/".$_session['mm_username']."/" .sha1(rand())."-" .basename($_files['uploadedfile'.$i]['name']); $putitat = str_replace("php", "txt" , $putitat); if(move_uploaded_file($_files['uploadedfile'.$i]['tmp_name'],$putitat)) { savedata(); } else { if(copy($_files['uploadedfile'.$i]['tmp_name'],$putitat)) { savedata(); } else { echo 'image upload has not been successful. go , try again.'; } } // ids mysql_select_db($db, $entrysystem); $insertteaminfo = sprintf("insert teamplayers (teamid, participationid, countryid, idplayer1, idplayer2, idplayer3, timestamp) values('','".$_session['participationid']."', %s,'".$_session['playerid1']."', '".$_session['playerid2']."', '".$_session['playerid3']."','')", getsqlvaluestring($_post['countryid'], "int")); mysql_query($insertteaminfo) or die(mysql_error()); $insertgoto = "myentries.php"; if (isset($_server['query_string'])) { $insertgoto .= (strpos($insertgoto, '?')) ? "&" : "?"; $insertgoto .= $_server['query_string']; } header(sprintf("location: %s", $insertgoto)); } } } html form goes here...
<form enctype="multipart/form-data" method="post" name="form1" action="<?php echo $editformaction; ?>"> <table width="100%"> <tr> <td align="center"> <label> <p>select championship</p> </label> <select name="championshipid"> <option>select championship..</option> <?php { ?> <option value="<?php echo $row_availablechampionships['championshipid']?>" ><?php echo $row_availablechampionships['championshipname']?></option> <?php } while ($row_availablechampionships = mysql_fetch_assoc($availablechampionships)); ?> </select> </td> </td> </tr> </table> // first table selecting championship <!-- form 1 starts here --> player 1 <label><p>first name </p></label> <input type="text" name="playerfirstname1" value="" /> <input type="hidden" name="max_file_size" value="5000000" /> choose photo upload: <input name="uploadedfile1" type="file" /> <input name="countryid" type="hidden" value="<?php echo $_session['mm_usergroup']; ?>" /> <input name="participationid" type="hidden" value="<?php echo $_session['idparticipants']; ?>" /> <label>last name</label> <input type="text" name="playerlastname1" value="" /> <label>date of birth</label> <select name="day1"> <option>day</option> <?php ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } ?> </select> <span class="selectrequiredmsg">please select item.</span></span><span id="sprymonthselect"> <select name="month1"> <option>month</option> <option value="01" >01-january</option> <option value="02" >02-february</option> <option value="03" >03-march</option> <option value="04" >04-april</option> <option value="05" >05-may</option> <option value="06" >06-june</option> <option value="07" >07-july</option> <option value="08" >08-august</option> <option value="09" >09-september</option> <option value="10" >10-october</option> <option value="11" >11-november</option> <option value="12" >12-december</option> </select> <select name="year1"> <option>year</option> <?php ($year = 1950; $year <= 2005; $year++) { echo "<option value=\"$year\">$year</option>\n"; } ?> </select> <label>national ranking</label> <input type="text" name="playernationalranking1" value="" > <label>phone number</label> <input type="text" name="playerphonenumber1" value="" > <label> email address</label> <input type="text" name="playeremailaddress1" value=""> <label> passport number </label><input type="text" name="playerpassportnumber1" value=""> <label>playing history</label> <textarea name="playerplayinghistory1" value=""></textarea> <label> address </label> <textarea name="playeraddress1" value=""></textarea> </table> <input type="hidden" name="playerid1" value=""> <input type="hidden" name="playerdob1" value=""> <input type="hidden" name="playercountry1" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="idusers1" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="created_at1" value="<?php $now = new datetime(); echo $now->format('y-m-d h:i:s');?>"> <!-- form 1 ends here --> <!-- form 2 starts here --> player 2 <label><p>first name </p></label> <input type="text" name="playerfirstname2" value=""> <input type="hidden" name="max_file_size" value="5000000" /> choose photo upload: <input name="uploadedfile2" type="file" /> <input name="countryid" type="hidden" value="<?php echo $_session['mm_usergroup']; ?>"> <input name="participationid" type="hidden" value="<?php echo $_session['idparticipants']; ?>"> <label>last name</label> <input type="text" name="playerlastname2" value=""> <label>date of birth</label> <select name="day2"> <option>day</option> <?php ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } ?> </select> <span class="selectrequiredmsg">please select item.</span></span><span id="sprymonthselect"> <select name="month2"> <option>month</option> <option value="01" >01-january</option> <option value="02" >02-february</option> <option value="03" >03-march</option> <option value="04" >04-april</option> <option value="05" >05-may</option> <option value="06" >06-june</option> <option value="07" >07-july</option> <option value="08" >08-august</option> <option value="09" >09-september</option> <option value="10" >10-october</option> <option value="11" >11-november</option> <option value="12" >12-december</option> </select> <select name="year2"> <option>year</option> <?php ($year = 1950; $year <= 2005; $year++) { echo "<option value=\"$year\">$year</option>\n"; } ?> </select> <label>national ranking</label> <input type="text" name="playernationalranking2" value="" > <label>phone number</label> <input type="text" name="playerphonenumber2" value="" > <label> email address</label> <input type="text" name="playeremailaddress2" value=""> <label> passport number </label><input type="text" name="playerpassportnumber2" value=""> <label>playing history</label> <textarea name="playerplayinghistory2" value=""></textarea> <label> address </label> <textarea name="playeraddress2" value=""></textarea> </table> <input type="hidden" name="playerid2" value=""> <input type="hidden" name="playerdob2" value=""> <input type="hidden" name="playercountry2" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="idusers2" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="created_at2" value="<?php $now = new datetime(); echo $now->format('y-m-d h:i:s');?>"> <!-- form 2 ends here --> <!-- form 3 starts here --> player 3 <label><p>first name </p></label> <input type="text" name="playerfirstname3" value=""> <input type="hidden" name="max_file_size" value="5000000" > choose photo upload: <input name="uploadedfile3" type="file" > <input name="countryid" type="hidden" value="<?php echo $_session['mm_usergroup']; ?>"> <input name="participationid" type="hidden" value="<?php echo $_session['idparticipants']; ?>"> <label>last name</label> <input type="text" name="playerlastname3" value=""> <label>date of birth</label> <select name="day3"> <option>day</option> <?php ($day = 1; $day <= 31; $day++) { echo "<option value=\"$day\">$day</option>\n"; } ?> </select> <span class="selectrequiredmsg">please select item.</span></span><span id="sprymonthselect"> <select name="month3"> <option>month</option> <option value="01" >01-january</option> <option value="02" >02-february</option> <option value="03" >03-march</option> <option value="04" >04-april</option> <option value="05" >05-may</option> <option value="06" >06-june</option> <option value="07" >07-july</option> <option value="08" >08-august</option> <option value="09" >09-september</option> <option value="10" >10-october</option> <option value="11" >11-november</option> <option value="12" >12-december</option> </select> <select name="year3"> <option>year</option> <?php ($year = 1950; $year <= 2005; $year++) { echo "<option value=\"$year\">$year</option>\n"; } ?> </select> <label>national ranking</label> <input type="text" name="playernationalranking3" value="" > <label>phone number</label> <input type="text" name="playerphonenumber3" value="" > <label> email address</label> <input type="text" name="playeremailaddress3" value=""> <label> passport number </label><input type="text" name="playerpassportnumber3" value=""> <label>playing history</label> <textarea name="playerplayinghistory3" value=""></textarea> <label> address </label> <textarea name="playeraddress3" value=""></textarea> </table> <input type="hidden" name="playerid3" value=""> <input type="hidden" name="playerdob3" value=""> <input type="hidden" name="playercountry3" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="idusers3" value="<?php echo $_session['mm_usergroup']; ?>"> <input type="hidden" name="created_at3" value="<?php $now = new datetime(); echo $now->format('y-m-d h:i:s');?>"> <!-- form 3 ends here --> <input type="hidden" name="mm_insert" value="form1"> </form> <?php // }//end if statement number of participants ?> i don't know went wrong, how not storing data, stores 2 players , 2 participation , stores 1 id in teamsplayers table , 2 images in files table.
how script work way should. suggestions welcome.
Comments
Post a Comment