mizz key_me Posted September 29, 2008 Share Posted September 29, 2008 Hye everyone.. How to make a form with two queries? In my problem, i need to make a form for new student registration. In the form, the data that i need is about the student and his/her parents. This is what i have made: <?php if(isset($_POST['add'])) { $student_name = addslashes($_POST['student_name']); $student_dob = addslashes($_POST['student_dob']); $student_regno = addslashes($_POST['student_regno']); $student_gender = addslashes($_POST['student_gender']); $parent_name = addslashes($_POST['parent_name']); $parent_icno = addslashes($_POST['parent_icno']); $parent_job = addslashes($_POST['parent_job']); $parent_dependant = addslashes($_POST['parent_dependant']); $parent_totsalary = addslashes($_POST['parent_totsalary']); $student_add = addslashes($_POST['student_add']); $parent_contactno = addslashes($_POST['parent_contactno']); include 'db_connect.php'; $query1 = "INSERT INTO student (student_name, student_dob, student_gender, student_add) VALUES ('$student_name', '$student_dob', '$student_gender', '$student_add')"; $query2 = " INSERT INTO parent (parent_name, parent_icno, parent_job, parent_dependant, parent_totsalary, parent_contactno) VALUES ('$parent_name', '$parent_icno', '$parent_job', '$parent_dependant', '$parent_totsalary', '$parent_contactno')"; $result = mysql_query($query1 && $query2); if($result) echo 'Add success!! Your request is been processed. Thank you.'; else echo 'Add failed!! Try again.'; } ?> Someone can show me the right way?? Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/ Share on other sites More sharing options...
Daniel0 Posted September 29, 2008 Share Posted September 29, 2008 Just call mysql_query() twice. I.e. $result = mysql_query($query1) && mysql_query($query2); $result will then be a boolean having the value true if both queries were successful and false if either of those failed. You have a problem with data integrity if one of them fails though as you'll have to remove information. You should look into transactions. Those are, however, only in InnoDB and they are not supported by the mysql extension which you use. This means you'll have to use MySQLi or PDO (see the manual). Edit: Actually, query two will not be executed if query one failed using the snippet I showed above because of operator associativity (logical and is left associative). If the first (left) operand is false then the "and" cannot possibly return true thus evaluating the second (right) operand will be redundant. You'll still have a problem if query one were successful and query two wasn't though, so you should still look into transactions. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653003 Share on other sites More sharing options...
timmah1 Posted September 29, 2008 Share Posted September 29, 2008 I'm pretty sure this will work <?php if(isset($_POST['add'])) { $student_name = addslashes($_POST['student_name']); $student_dob = addslashes($_POST['student_dob']); $student_regno = addslashes($_POST['student_regno']); $student_gender = addslashes($_POST['student_gender']); $parent_name = addslashes($_POST['parent_name']); $parent_icno = addslashes($_POST['parent_icno']); $parent_job = addslashes($_POST['parent_job']); $parent_dependant = addslashes($_POST['parent_dependant']); $parent_totsalary = addslashes($_POST['parent_totsalary']); $student_add = addslashes($_POST['student_add']); $parent_contactno = addslashes($_POST['parent_contactno']); include 'db_connect.php'; $query1 = "INSERT INTO student (student_name, student_dob, student_gender, student_add) VALUES ('$student_name', '$student_dob', '$student_gender', '$student_add')"; $query2 = " INSERT INTO parent (parent_name, parent_icno, parent_job, parent_dependant, parent_totsalary, parent_contactno) VALUES ('$parent_name', '$parent_icno', '$parent_job', '$parent_dependant', '$parent_totsalary', '$parent_contactno')"; $result = mysql_query($query1) && mysql_query($query2); if($result) echo 'Add success!! Your request is been processed. Thank you.'; else echo 'Add failed!! Try again.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653007 Share on other sites More sharing options...
KevinM1 Posted September 29, 2008 Share Posted September 29, 2008 You're pretty close. A couple of things: Addslashes() is not secure. You should use mysql_real_escape_string() instead. Secondly, and more importantly, you can't use logical AND (&&) in the manner in which you did. Instead, you're going to have to run each query individually: $result1 = mysql_query($query1); $result2 = mysql_query($query2); if($result1 && $result2) { //everything okay } else { //something went wrong with the insert } Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653009 Share on other sites More sharing options...
Daniel0 Posted September 29, 2008 Share Posted September 29, 2008 I'm pretty sure this will work Good job repeating me. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653011 Share on other sites More sharing options...
timmah1 Posted September 29, 2008 Share Posted September 29, 2008 Not repeating, just beat me to the punch Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653018 Share on other sites More sharing options...
mizz key_me Posted September 29, 2008 Author Share Posted September 29, 2008 thanks to Daniel0, timmah1 and Nightslyr. I've tried all the codes, they work but other things happen.. The data just only got into student table but not parents table.. Plus, it keeps saying 'add failed'. :-\ Here's all the codes: <?php if(isset($_POST['add'])) { $student_name = addslashes($_POST['student_name']); $student_dob = addslashes($_POST['student_dob']); $student_regno = addslashes($_POST['student_regno']); $student_gender = addslashes($_POST['student_gender']); $parent_name = addslashes($_POST['parent_name']); $parent_icno = addslashes($_POST['parent_icno']); $parent_job = addslashes($_POST['parent_job']); $parent_dependant = addslashes($_POST['parent_dependant']); $parent_totsalary = addslashes($_POST['parent_totsalary']); $student_add = addslashes($_POST['student_add']); $parent_contactno = addslashes($_POST['parent_contactno']); include 'db_connect.php'; $query1 = "INSERT INTO student (student_name, student_dob, student_regno, student_gender, student_add) VALUES ('$student_name', '$student_dob', '$student_regno', '$student_gender', '$student_add')"; $query2 = " INSERT INTO parent (parent_name, parent_icno, parent_job, parent_dependant, parent_totsalary, parent_contactno) VALUES ('$parent_name', '$parent_icno', '$parent_job', '$parent_dependant', '$parent_totsalary', '$parent_contactno')"; $result1 = mysql_query($query1 && $query2); if($result); echo'Add success!! Your request is been processed. Thank you.'; else echo 'Add failed!! Try again.'; } ?> <br /> <h2 class="style7"><u>Year 1 Registration Form</u></h2> <form action="registration.php" method="post"> <table width="563" border="0"> <tr> <td width="242" scope="col" align="left"><span class="style10">Name :</span></td> <td width="303" scope="col" align="left"><input name="student_name" type="text" size="50" maxlength="50" /></td> </tr> <tr> <td align="left"><span class="style10">Birth Certificate No. :</span></td> <td align="left"><input name="student_regno" type="text" size="7" maxlength="7" /></td> </tr> <tr> <td align="left"><span class="style10">Date of Birth :</span></td> <td align="left"><select name="student_dob"> <option>Day</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> <option>11</option> <option>12</option> <option>13</option> <option>14</option> <option>15</option> <option>16</option> <option>17</option> <option>18</option> <option>19</option> <option>20</option> <option>21</option> <option>22</option> <option>23</option> <option>24</option> <option>25</option> <option>26</option> <option>27</option> <option>28</option> <option>29</option> <option>30</option> <option>31</option> </select> <select name="select"> <option>Month</option> <option>January</option> <option>February</option> <option>March</option> <option>April</option> <option>May</option> <option>June</option> <option>July</option> <option>August</option> <option>September</option> <option>October</option> <option>November</option> <option>December</option> </select> <select name="select2"> <option>Year</option> <option>2000</option> <option>2001</option> <option>2002</option> <option>2003</option> </select> </td> </tr> <tr> <td align="left" class="style10">Gender :</td> <td align="left"><input name="student_gender" type="radio" value="MALE" /> <span class="style8">Male</span> <input name="student_gender" type="radio" value="FEMALE" /> <span class="style8">Female </span></td> </tr> <tr> <td align="left" class="style10">Father's / Guardian's Name :</td> <td align="left"><input name="parent_name" type="text" size="40" maxlength="40" /></td> </tr> <tr> <td align="left" class="style10">Father's / Guardian's I/C No. :</td> <td align="left"><input name="parent_icno"type="text" size="12" maxlength="12" /> <span class="style8">Ex : 630321015432</span></td> </tr> <tr> <td align="left" class="style10">Father's / Guardian's Occupation :</td> <td align="left"><input name="parent_job" type="text" size="25" maxlength="35" /></td> </tr> <tr> <td align="left" class="style10">Jumlah Tanggungan :</td> <td align="left"><input name="parent_dependant" type="text" size="3" maxlength="3" /> <span class="style8">person</span></td> </tr> <tr> <td align="left" class="style10">Total Monthly Salary :</td> <td align="left"><span class="style8">RM</span> <input name="parent_totsalary" type="text" size="7" maxlength="7" /></td> </tr> <tr> <td align="left" class="style10">Home Address :</td> <td align="left"><input name="student_add" type="text" size="40" maxlength="40" /></td> </tr> <tr> <td align="left" class="style10">Contact No. :</td> <td align="left"><input name="parent_contactno" type="text" size="10" maxlength="10" /> <span class="style8"> Ex : 0123456789</span></td> </tr> </table> <br /><input type="submit" name="add" value=" Submit " /> </form> Help me.. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653042 Share on other sites More sharing options...
timmah1 Posted September 29, 2008 Share Posted September 29, 2008 You have this $result1 = mysql_query($query1 && $query2); if($result); and it should actually be $result1 = mysql_query($query1) && mysql_query($query2); if($result1); Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653050 Share on other sites More sharing options...
mizz key_me Posted September 29, 2008 Author Share Posted September 29, 2008 You have this $result1 = mysql_query($query1 && $query2); if($result); and it should actually be $result1 = mysql_query($query1) && mysql_query($query2); if($result1); Oops, my mistake. Hehe.. I've correct it but still did not work. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653065 Share on other sites More sharing options...
Daniel0 Posted September 29, 2008 Share Posted September 29, 2008 That has got to do with what I told you about operator associativity. If the first one fails then the other one will not be run. You will have to use transactions in order to rollback the changes. Using PDO it could look like this: // ... try { $db->beginTransaction(); $db->query($query1); $db->query($query2); $db->commit(); echo 'Insertion was successful.'; } catch (PDOException $e) { $db->rollBack(); echo 'Insertion failed.'; } In that snippet, $db is an instance of the class PDO. Quote Link to comment https://forums.phpfreaks.com/topic/126290-how-to-make-form-with-two-queries/#findComment-653090 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.