Jump to content

Joning tables


yandoos
Go to solution Solved by Barand,

Recommended Posts

Hello I was hoping some help with joining tables....

 

I have joined tables but have found strange results so i assume I am doing it wrong. Whats happening is that even though I am pulling the query from the product table if there are NULL data in the mem_id field the row is not being pulled. I would add this is using a php while loop, using phpmyadmin and pure mysql it works.

SELECT * FROM product
join distribution ON product.pid = distribution.pid
join plus_signup ON distribution.mem_id = plus_signup.mem_id

Have a look at this phpmyadmin screen shot.

 

 

The records for pid 3, and 4 are not being outputted when I used a php while loop. I need it so that all the records will be pulled even if the mem_id is null. Is there a way I can do this?

 

Thank you :)

post-179293-0-13011100-1438198634_thumb.png

Edited by yandoos
Link to comment
Share on other sites

<?php
$per_page='10';

if (isset($_GET['page'])) {

$page = $_GET['page'];

}else {

$page='1';}

  // Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page; 
	 
	 
$connection = mysqli_connect($dbhost_name, $username, $password, $database);
 $finder1 = mysqli_query($connection,"SELECT * FROM product left join distribution ON product.pid = distribution.pid
 left join plus_signup ON distribution.mem_id = plus_signup.mem_id LIMIT $start_from, $per_page")  or die (mysql_error());
 
  $connection = mysqli_connect($dbhost_name, $username, $password, $database);
 $counter1 = mysqli_query($connection,"SELECT * FROM product left join distribution ON product.pid = distribution.pid
 left join plus_signup ON distribution.mem_id = plus_signup.mem_id")  or die (mysql_error());
 
  // Count the total records
$total_records = mysqli_num_rows($counter1);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
 
 }

echo '<table class="admintable" width="1000" style="padding:0px 10px 0px 10px;" border="0">'; 


while($builder1 = mysqli_fetch_array($finder1))
    {    

$pid = mysqli_real_escape_string($connection, $builder1['pid']);
echo $pid; echo '<br/>';

     
    echo'<tr><td valign="top">'; ?>
    
    
    <table border="0"><tr><td width="100">
    <form action="update_image.php" enctype="multipart/form-data" method="post">	
     <input name="pid" type="hidden" value="<?php echo $builder1['pid'];?>">	   
     Name:  </td><td><input name="productname" type="text" value="<?php echo $builder1['productname'];?>"></td></tr><tr>
      <td>Price: </td><td><input name="price" type="text" value="<?php echo $builder1['price'];?>"></td></tr><tr>
      <td> Stock: </td><td><input name="stock" type="text" value="<?php echo $builder1['stock'];?>"></td></tr><tr>
      
      <?php 
      
       
	  
	   echo '<tr><td colspan="2">';
	   
	   
	   if ($builder1['name'] != NULL){
	   
	    echo 'Product assigned to: <a href="http://dusousbois.co.uk/distributions.php?id=';
	    echo $builder1['pdid'];
	    echo '">';
	    echo $builder1['name'];
	    echo '</a>';
	  
	  
	   } else if ($builder1['name'] == NULL){
	   echo 'Product ready for assigment'; 
	   }
	   
	  
	  
  
	  
	  
	    echo'</td></tr>';
    
      ?>
      
      
      
      
       <td colspan="2">Description:<br/><textarea name="description" cols="40" rows="8"><?php echo $builder1['description'];?></textarea></td></tr><tr>   
       <td><input name="submit" type="Submit" value="Edit Product"/></td>
       
       <td>  <?php if ($builder1['stock'] == '1') {
       echo $pid;
        
	  echo '<a href="delete_products.php?id=';
	  echo $builder1['pid'];
	  
	  echo'">Delete</a>';
	  } else if ($builder1['stock'] == '0') {
	  echo 'This product can not be deleted as it has been assigned to a gallery';
	  
	  }  
	  
	  echo '</td></tr>';}
	  
	  
	  echo'
	  </table>'; 

Here is a snipped of the code in question. The problem is that the $pid is not outputting when the mem_id is NULL

Link to comment
Share on other sites

  • Solution

Don't use "SELECT * ", especially when using joins. Specify the fields you want. Where you have the same field in two table (like pid) specify the table or alias with the field eg SELECT product.pid, .....

 

You only need to connect once per script unless you are connecting to two servers.

 

 

$pid = mysqli_real_escape_string($connection, $builder1['pid']);

real_escape_string() is for sanitizing input BEFORE using it in a query, not afterwards.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.