tables connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mara90
    New Member
    • Apr 2014
    • 3

    #1

    tables connection

    hello i have two tables ,pelates(firstn ame,lastname,id _country) and country(country _name,id_countr y) 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:
    Code:
    Country:<?php 
    require_once('../mysqli_connect.php');
    $q= "SELECT * FROM country";
    $r=mysql_query($q);
    //var_dump($r);
    ?>
    <select  name="country_name" id="country_name">
    
    <?php while ($row=mysql_fetch_assoc($r)) { ?>
    <?php echo'<option value="' . $row['id_country'] . '" />' .$row['country_name'] . ' </option>';?>
    <?php } mysql_close($link);  
     ?>
     
    </sele
    what can i do?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Pleas change you code in Line#3 to contain the actual field names, like this (untested):
    Code:
    $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(firstn ame,lastname,id _country) and country(country _name,id_countr y) 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(firstn ame,lastname,id_country)"
    I seem to be missing the point.... ;)

    Comment

    • mara90
      New Member
      • Apr 2014
      • 3

      #3
      thanks for the fast answer.
      i have to insert into pelates a lot of things.actually a piece of my code is here:
      Code:
      if(empty($_POST['first_name'])) {
      	$errors[]='You forgot to enter your first name.';
      	}else{
      	$fn=mysql_real_escape_string(trim($_POST['first_name']));
      	}
      	if(empty($_POST['last_name'])) {
      	$errors[]='You forgot to enter your last name.';
      	}else{
      	$ln=mysql_real_escape_string(trim($_POST['last_name']));
      	}
          if(empty($_POST['email'])) {
      	$errors[]='You forgot to enter your email.';
      	}else{
      	$e=mysql_real_escape_string(trim($_POST['email']));
      	}
      	if(!empty($_POST['pass1'])) { 
      	    if($_POST['pass1'] !=
      		$_POST['pass2']) {
      	$errors[]='Your password did not match the confirmed password.';
      	}else{
      	$p=mysql_real_escape_string(trim($_POST['pass1']));
      	}
          }else {
          $errors[]='You forgot to enter your password.';
          }
          if(empty($_POST['birth'])) {
      	$errors[]='You forgot to enter your birth.';
      	}else{
      	$b=mysql_real_escape_string(trim($_POST['birth']));
      	}
      	if(empty($_POST['address'])) {
      	$errors[]='You forgot to enter your address.';
      	}else{
      	$a=mysql_real_escape_string(trim($_POST['address']));
      	}
      
      
      	if(empty($_POST['tilefwno'])) {
      	$errors[]='You forgot to enter your tilefwno.';
      	}else{
      	$t=mysql_real_escape_string(trim($_POST['tilefwno']));
      	}
      	
      	
      if(empty($errors)) {
      
      $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"])."')";
      $r=mysql_query($q,$link);
      all it works but i can't put the id_country .any help??

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        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.)

        Comment

        • mara90
          New Member
          • Apr 2014
          • 3

          #5
          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:
          Code:
          <?php
          $page_title='Register';
          include ('includes/header.html');
          
          
          if (isset($_POST['submitted'])){
          if(isset($_FILES['upload'])) {
          $allowed =array ('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png');
          
          if (in_array($_FILES['upload'] ['type'], $allowed)){
          
          if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name'] }")) {
          echo '<p><em>The file has been uploaded..</em></p>';
          }
          }else{
          echo '<p class="error">Please uploaad a JPEG or PNG image.</p>';
          }
          }
          if ($_FILES['upload']['error']>0) {
          echo '<p class="error">The file could not be uploaded because: <strong>';
          
          switch($_FILES['upload']['error']){
          case 1:
          print 'The file exceeds the upload_max_filesize setting in php.ini.';
          break;
          case 2:
          print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
          break;
          case 3:
          print 'The file was only partially uploaded.';
          break;
          case 4:
          print 'No file was uploaded.';
          break;
          case 6:
          print 'No temporary folder was available.';
          break;
          case 7:
          print 'Unable to write to the disk.';
          break;
          case 8:
          print 'File uploaded stopped.';
          break;
          default:
          print 'A system error occured.';
          break;
          }
          print '</strong></p>';
          }
          if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']))
              {
             echo $_FILES["upload"]["tmp_name"] . " already exists. ";
                }
              else
                {
                move_uploaded_file($_FILES["upload"]["tmp_name"],
                "upload/" . $_FILES["upload"]["tmp_name"]);
               
                }
              
          
          //var_dump($_POST);
          require_once('../mysqli_connect.php');
          
              $errors=array();
              
          	if(empty($_POST['first_name'])) {
          	$errors[]='You forgot to enter your first name.';
          	}else{
          	$fn=mysql_real_escape_string(trim($_POST['first_name']));
          	}
          	if(empty($_POST['last_name'])) {
          	$errors[]='You forgot to enter your last name.';
          	}else{
          	$ln=mysql_real_escape_string(trim($_POST['last_name']));
          	}
              if(empty($_POST['email'])) {
          	$errors[]='You forgot to enter your email.';
          	}else{
          	$e=mysql_real_escape_string(trim($_POST['email']));
          	}
          	if(!empty($_POST['pass1'])) { 
          	    if($_POST['pass1'] !=
          		$_POST['pass2']) {
          	$errors[]='Your password did not match the confirmed password.';
          	}else{
          	$p=mysql_real_escape_string(trim($_POST['pass1']));
          	}
              }else {
              $errors[]='You forgot to enter your password.';
              }
              if(empty($_POST['birth'])) {
          	$errors[]='You forgot to enter your birth.';
          	}else{
          	$b=mysql_real_escape_string(trim($_POST['birth']));
          	}
          	if(empty($_POST['address'])) {
          	$errors[]='You forgot to enter your address.';
          	}else{
          	$a=mysql_real_escape_string(trim($_POST['address']));
          	}
          
          
          	if(empty($_POST['tilefwno'])) {
          	$errors[]='You forgot to enter your tilefwno.';
          	}else{
          	$t=mysql_real_escape_string(trim($_POST['tilefwno']));
          	}
          	
          	
          if(empty($errors)) {
          
          $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"])."')";
          var_dump($q);
          $r=mysql_query($q,$link);
          
          if($r){
             echo '<h1>Thank you.. <h1>
             <p>You are now registered </p>';
             }else {
             echo '<h1>System Error..</h1>
             <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>';
            
             }
            
            }else{
            echo 'the following error occured:';
          foreach($errors as $msg){
          	echo" =$msg<br />\n";
          }
          echo 'please try again';
          }
          
          mysql_close($link);
          }
          
          ?>
          
          
          <h1>Register</h1>
          <form enctype="multipart/form-data" method="post" action="register.php">
          
          <legend><b>Enter your information in the form below:</b></legend>
          
          First Name: <input type="text" name="first_name" size="10" maxlength="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /><br/>
          Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /><br/>
          Email: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /><br/>
          Password: <input type="password" name="pass1" size="10" maxlength="20" /><br/>
          Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /><br/>
          Birth:<input type="date" name="birth" placeholder="(MM/DD/YYYY)" required><br>
          Address: <input type="text" name="address" size="50" maxlength="80"  /><br/>
          Country:<?php 
          require_once('../mysqli_connect.php');
          $q= "SELECT id_country,country_name FROM country";
          $r=mysql_query($q);
          //var_dump($r);
          ?>
          <select  name="country_name" id="country_name">
          
          <?php while ($row=mysql_fetch_assoc($r)) { ?>
          <?php echo'<option value="' . $row['id_country'] . '" />' .$row['country_name'] . ' </option>';?>
          <?php } mysql_close($link);  
           ?>
           
           </select>
          Tilefwno: <input type="text" name="tilefwno" size="30" maxlength="40" /><br/>
          Upload :<legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend>
          <p><b>Filename:</b> <input type="file" name="upload" /></p>
          <input type="submit" name="submit" value="Register" />
          <input type="hidden" name="submitted" value="TRUE" />
          </form>
          </body>

          Comment

          Working...