473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

tables connection

3 New Member
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
4 1235
Luuk
1,047 Recognized Expert Top Contributor
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
mara90
3 New Member
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
Luuk
1,047 Recognized Expert Top Contributor
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
mara90
3 New Member
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

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

Similar topics

3
by: G-Fit | last post by:
Hello group, I have several servers hosting SQL databases. On each of them, I have several databases. All those databases have the same structure (even those on different servers), only the data...
8
by: ZRexRider | last post by:
Hi, I have an application that uses MS-SQL Linked tables. I have a utility routine that drops all links and re-establishes them. It works great when my connection string is a DSN connection...
1
by: Ahmet Karaca | last post by:
Hi. myds.Reset(); mycommand.SelectCommand.CommandText= "Select att1 from Ing as Ingredient, Pro as Product "+ "where Pro.ad='apple' and Pro.id=Ing.id"; mycommand.Fill(myds, "Product"); // Here...
1
by: peter.konda | last post by:
Hello! I have been busting my head with a problem, that goes like this: 1. with a first ajax call to the server(apache 2.0 + php), I create a temporary table like this: create temporary table...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
0
by: Jim Stools | last post by:
Hopefully this will make some sense.. I have a database that has around 50 tables - I thought about putting each table in a class and the data connection in a class then I could manage the (tables)...
4
by: Jim Stools | last post by:
Forget the re-post I had my clock set 12 hours earlier Hopefully this will make some sense.. I have a database that has around 50 tables - I thought about putting each table in a class and the...
2
by: Venkata Narayana | last post by:
Hi, You all may be knowing that Connection.isClosed() does not tells us if the underying DB connection is active or not; it only checks if Connection.close() had been previously called or not....
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.