Jump to content

Search 2 tables at once


newhip

Recommended Posts

Ok if have some code that i am trying to add to. 

<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="search"/>
<input type="submit" name="search1"/>
</form>
<?php

if(isset($_POST['search1'])){
$search=mysql_real_escape_string($_POST['search']);
}
include 'connect.php';
if(isset($_POST['search1'])){

$query=mysql_query("SELECT * FROM media WHERE 
extra_cred  LIKE '%".$search."%' OR 
artist_id  LIKE '%".$search."%' OR 
title  LIKE '%".$search."%' OR 
content  LIKE '%".$search."%' OR 
detail  LIKE '%".$search."%' OR 
tags  LIKE '%".$search."%' OR 
user  LIKE '%".$search."%' OR 
type LIKE '%".$search."%'

")
?>

What I would like to do is also search another table within that database by the name of artists with the same POST data. Anyone know what to do? I've been at this for a while.

Link to comment
https://forums.phpfreaks.com/topic/285921-search-2-tables-at-once/
Share on other sites

Is there a relationship between both tables? Do DESCRIBE to provide more information about tables structure. If there is no relationship between these two tables you can consider using an UNION query.  

Also, never use the dreaded, evil "select star" in your queries in the future. It's a very, very bad practice.

Use a join on artist_id

$query=mysql_query("SELECT m.*, a.artist_name 
FROM media m
    JOIN artist a USING (artist_id)
WHERE
extra_cred LIKE '%".$search."%' OR
artist_name LIKE '%$search%' OR
m.artist_id LIKE '%".$search."%' OR
title LIKE '%".$search."%' OR
content LIKE '%".$search."%' OR
detail LIKE '%".$search."%' OR
tags LIKE '%".$search."%' OR
user LIKE '%".$search."%' OR
type LIKE '%".$search."%'
");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.