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

Link to comment
Share on other sites

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."%'
");
Edited by Barand
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.