By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,037 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,037 IT Pros & Developers. It's quick & easy.

tables connection

P: 3
hello i have two tables ,pelates(firstname,lastname,id_country) and country(country_name,id_country) and a form.i want to insert into tha pelates table the id_country from the other table but i can't.also in my form the countries appear with this code:
Expand|Select|Wrap|Line Numbers
  1. Country:<?php 
  2. require_once('../mysqli_connect.php');
  3. $q= "SELECT * FROM country";
  4. $r=mysql_query($q);
  5. //var_dump($r);
  6. ?>
  7. <select  name="country_name" id="country_name">
  8.  
  9. <?php while ($row=mysql_fetch_assoc($r)) { ?>
  10. <?php echo'<option value="' . $row['id_country'] . '" />' .$row['country_name'] . ' </option>';?>
  11. <?php } mysql_close($link);  
  12.  ?>
  13.  
  14. </sele
what can i do?
Apr 3 '14 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,035
Pleas change you code in Line#3 to contain the actual field names, like this (untested):
Expand|Select|Wrap|Line Numbers
  1. $q= "SELECT id_country, country_name FROM country";
This gives better readable code, and it's more errorproof.
(You should NEVER use '*' in any sql-query!)

Back to your queston: "hello i have two tables ,pelates(firstname,lastname,id_country) and country(country_name,id_country) and a form.i want to insert into tha pelates table the id_country from the other table"

Hmmmz. so you want to insert id_country in the table "pelates(firstname,lastname,id_country)"
I seem to be missing the point.... ;)
Apr 3 '14 #2

P: 3
thanks for the fast answer.
i have to insert into pelates a lot of things.actually a piece of my code is here:
Expand|Select|Wrap|Line Numbers
  1. if(empty($_POST['first_name'])) {
  2.     $errors[]='You forgot to enter your first name.';
  3.     }else{
  4.     $fn=mysql_real_escape_string(trim($_POST['first_name']));
  5.     }
  6.     if(empty($_POST['last_name'])) {
  7.     $errors[]='You forgot to enter your last name.';
  8.     }else{
  9.     $ln=mysql_real_escape_string(trim($_POST['last_name']));
  10.     }
  11.     if(empty($_POST['email'])) {
  12.     $errors[]='You forgot to enter your email.';
  13.     }else{
  14.     $e=mysql_real_escape_string(trim($_POST['email']));
  15.     }
  16.     if(!empty($_POST['pass1'])) { 
  17.         if($_POST['pass1'] !=
  18.         $_POST['pass2']) {
  19.     $errors[]='Your password did not match the confirmed password.';
  20.     }else{
  21.     $p=mysql_real_escape_string(trim($_POST['pass1']));
  22.     }
  23.     }else {
  24.     $errors[]='You forgot to enter your password.';
  25.     }
  26.     if(empty($_POST['birth'])) {
  27.     $errors[]='You forgot to enter your birth.';
  28.     }else{
  29.     $b=mysql_real_escape_string(trim($_POST['birth']));
  30.     }
  31.     if(empty($_POST['address'])) {
  32.     $errors[]='You forgot to enter your address.';
  33.     }else{
  34.     $a=mysql_real_escape_string(trim($_POST['address']));
  35.     }
  36.  
  37.  
  38.     if(empty($_POST['tilefwno'])) {
  39.     $errors[]='You forgot to enter your tilefwno.';
  40.     }else{
  41.     $t=mysql_real_escape_string(trim($_POST['tilefwno']));
  42.     }
  43.  
  44.  
  45. if(empty($errors)) {
  46.  
  47. $q= "INSERT INTO `pelates` (`first_name`, `last_name`, `email`, `pass`, `registration_date`, `birth`, `address`,`id_country`, `tilefwno`,`filename`) VALUES ('$fn','$ln','$e', MD5('$p'), NOW(),'$b','$a','$_POST[id_country]','$t','".mysql_real_escape_string($_FILES["upload"]["name"])."')";
  48. $r=mysql_query($q,$link);
all it works but i can't put the id_country .any help??
Apr 3 '14 #3

Expert 100+
P: 1,035
When you are handling other $_POST data you do it like this, i.e. on the address field:
$_POST['address']

Why do you think the insert can do it like this:
.....a','$_POST[id_country]','$.....
(hint: the single-quotes arround id_country are missing.

And you should do input-validation on that field too!
(you to check if it's a valid country_id.)
Apr 4 '14 #4

P: 3
thanks for your repsonse but how i can display the id_country from the table country when the form is submitted??it doesn't take anything in thie field.here is my entire code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $page_title='Register';
  3. include ('includes/header.html');
  4.  
  5.  
  6. if (isset($_POST['submitted'])){
  7. if(isset($_FILES['upload'])) {
  8. $allowed =array ('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png');
  9.  
  10. if (in_array($_FILES['upload'] ['type'], $allowed)){
  11.  
  12. if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name'] }")) {
  13. echo '<p><em>The file has been uploaded..</em></p>';
  14. }
  15. }else{
  16. echo '<p class="error">Please uploaad a JPEG or PNG image.</p>';
  17. }
  18. }
  19. if ($_FILES['upload']['error']>0) {
  20. echo '<p class="error">The file could not be uploaded because: <strong>';
  21.  
  22. switch($_FILES['upload']['error']){
  23. case 1:
  24. print 'The file exceeds the upload_max_filesize setting in php.ini.';
  25. break;
  26. case 2:
  27. print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
  28. break;
  29. case 3:
  30. print 'The file was only partially uploaded.';
  31. break;
  32. case 4:
  33. print 'No file was uploaded.';
  34. break;
  35. case 6:
  36. print 'No temporary folder was available.';
  37. break;
  38. case 7:
  39. print 'Unable to write to the disk.';
  40. break;
  41. case 8:
  42. print 'File uploaded stopped.';
  43. break;
  44. default:
  45. print 'A system error occured.';
  46. break;
  47. }
  48. print '</strong></p>';
  49. }
  50. if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']))
  51.     {
  52.    echo $_FILES["upload"]["tmp_name"] . " already exists. ";
  53.       }
  54.     else
  55.       {
  56.       move_uploaded_file($_FILES["upload"]["tmp_name"],
  57.       "upload/" . $_FILES["upload"]["tmp_name"]);
  58.  
  59.       }
  60.  
  61.  
  62. //var_dump($_POST);
  63. require_once('../mysqli_connect.php');
  64.  
  65.     $errors=array();
  66.  
  67.     if(empty($_POST['first_name'])) {
  68.     $errors[]='You forgot to enter your first name.';
  69.     }else{
  70.     $fn=mysql_real_escape_string(trim($_POST['first_name']));
  71.     }
  72.     if(empty($_POST['last_name'])) {
  73.     $errors[]='You forgot to enter your last name.';
  74.     }else{
  75.     $ln=mysql_real_escape_string(trim($_POST['last_name']));
  76.     }
  77.     if(empty($_POST['email'])) {
  78.     $errors[]='You forgot to enter your email.';
  79.     }else{
  80.     $e=mysql_real_escape_string(trim($_POST['email']));
  81.     }
  82.     if(!empty($_POST['pass1'])) { 
  83.         if($_POST['pass1'] !=
  84.         $_POST['pass2']) {
  85.     $errors[]='Your password did not match the confirmed password.';
  86.     }else{
  87.     $p=mysql_real_escape_string(trim($_POST['pass1']));
  88.     }
  89.     }else {
  90.     $errors[]='You forgot to enter your password.';
  91.     }
  92.     if(empty($_POST['birth'])) {
  93.     $errors[]='You forgot to enter your birth.';
  94.     }else{
  95.     $b=mysql_real_escape_string(trim($_POST['birth']));
  96.     }
  97.     if(empty($_POST['address'])) {
  98.     $errors[]='You forgot to enter your address.';
  99.     }else{
  100.     $a=mysql_real_escape_string(trim($_POST['address']));
  101.     }
  102.  
  103.  
  104.     if(empty($_POST['tilefwno'])) {
  105.     $errors[]='You forgot to enter your tilefwno.';
  106.     }else{
  107.     $t=mysql_real_escape_string(trim($_POST['tilefwno']));
  108.     }
  109.  
  110.  
  111. if(empty($errors)) {
  112.  
  113. $q= "INSERT INTO `pelates` (`first_name`, `last_name`, `email`, `pass`, `registration_date`, `birth`, `address`,`id_country`, `tilefwno`,`filename`) VALUES ('$fn','$ln','$e', MD5('$p'), NOW(),'$b','$a','$_POST[id_country]','$t','".mysql_real_escape_string($_FILES["upload"]["name"])."')";
  114. var_dump($q);
  115. $r=mysql_query($q,$link);
  116.  
  117. if($r){
  118.    echo '<h1>Thank you.. <h1>
  119.    <p>You are now registered </p>';
  120.    }else {
  121.    echo '<h1>System Error..</h1>
  122.    <p class="error">You could not to be registered due to a system problem. Go back and fill the form again in: <a href="register.php">http://localohost/week3/htdocs/register.php</a> .</p>';
  123.  
  124.    }
  125.  
  126.   }else{
  127.   echo 'the following error occured:';
  128. foreach($errors as $msg){
  129.     echo" =$msg<br />\n";
  130. }
  131. echo 'please try again';
  132. }
  133.  
  134. mysql_close($link);
  135. }
  136.  
  137. ?>
  138.  
  139.  
  140. <h1>Register</h1>
  141. <form enctype="multipart/form-data" method="post" action="register.php">
  142.  
  143. <legend><b>Enter your information in the form below:</b></legend>
  144.  
  145. First Name: <input type="text" name="first_name" size="10" maxlength="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /><br/>
  146. Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /><br/>
  147. Email: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /><br/>
  148. Password: <input type="password" name="pass1" size="10" maxlength="20" /><br/>
  149. Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /><br/>
  150. Birth:<input type="date" name="birth" placeholder="(MM/DD/YYYY)" required><br>
  151. Address: <input type="text" name="address" size="50" maxlength="80"  /><br/>
  152. Country:<?php 
  153. require_once('../mysqli_connect.php');
  154. $q= "SELECT id_country,country_name FROM country";
  155. $r=mysql_query($q);
  156. //var_dump($r);
  157. ?>
  158. <select  name="country_name" id="country_name">
  159.  
  160. <?php while ($row=mysql_fetch_assoc($r)) { ?>
  161. <?php echo'<option value="' . $row['id_country'] . '" />' .$row['country_name'] . ' </option>';?>
  162. <?php } mysql_close($link);  
  163.  ?>
  164.  
  165.  </select>
  166. Tilefwno: <input type="text" name="tilefwno" size="30" maxlength="40" /><br/>
  167. Upload :<legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
  168. <p><b>Filename:</b> <input type="file" name="upload" /></p>
  169. <input type="submit" name="submit" value="Register" />
  170. <input type="hidden" name="submitted" value="TRUE" />
  171. </form>
  172. </body>
  173.  
Apr 5 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.