Problem in Mysql, Parent and child table

16 views
Skip to first unread message

sugam prakash

unread,
Sep 5, 2013, 8:00:47 AM9/5/13
to xamp...@googlegroups.com
Hi Friends,

I am facing some problem in mysql query. I have two table. I want to fetch data master table form child table. In master table one column name is mobile.Through this column(mobile) in child table have huge data. And i want to show today data,current week data,current month data, and two different date from child table. But now i facing some problem. My query is

$selectQuery = "SELECT * FROM `child_table` WHERE  `sender_number` = 
(
  SELECT `mobile` FROM `master_table` WHERE `mobile` = `child_table`.`sender_number` AND 
  `id` = '$id'
)";
Blow screen shot . And i am also sending file.


Untitled.jpg
test.php

Ashish Choudhary

unread,
Sep 5, 2013, 1:24:28 PM9/5/13
to xamp...@googlegroups.com
Hello Sugam,

Line 79 - $today = $_POST['today'];
Line 99 - $week = $_POST['week'];

As we can see, the two errors you are getting are from above mentioned lines which says the variables are not defined. This means that the variables are not getting any values from $_POST variable i.e you have either not defined any name such as today and week or you have written incorrect name/spelling, Remember the parameters in post variable($_POST) are case-sensitive and they should match the case defined on the page from where you are submitting the data.

Example:
####################

<input type="text" name="Today" />

$_POST["today"] is wrong.
$_POST["Today"] is correct.

Print/echo  $_POST["today"] on the page where you are getting the post request data and notice what's the output.

####################

Let us know if you get any issues with that.
Also share the solution if you resolve it.

Note: Whenever you get "Undefined index" error that means that the index mentioned in the error is not being assigned any value or the value it is getting is undefined.

Thanks. 



--
Ask New Question from : https://groups.google.com/forum/?fromgroups#!newtopic/xamppdev
 
Create filter of mails from this group http://www.wikihow.com/Create-a-Filter-in-Gmail
---
You received this message because you are subscribed to the Google Groups "PHP Developers' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xamppdev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xamppdev/548cb98d-591b-44e3-8512-a7121e2514dd%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

sugam prakash

unread,
Sep 5, 2013, 1:40:26 PM9/5/13
to xamp...@googlegroups.com
Only i want to know query of current week and current month.


On Thu, Sep 5, 2013 at 11:08 PM, sugam prakash <mrs...@gmail.com> wrote:
Ashish i think u are not getting my question. Today is value of date radio button.

<form name="search" method="post" action="my_profile.php">
    <label><input type="radio" name="date" value="today">Today</label>
    <label><input type="radio" name="date" value="week">This Week</label>
    <label><input type="radio" name="date" value="month">This Month</label>
    To <input type="date" name="date1" value="" placeholder="YYYY/MM/DD">
    From <input type="date" name="date2" value="" placeholder="YYYY/MM/DD">
    <input type="submit" value="Search">
  </form>
and code is ..

$selectQuery="";
if ($_POST['date'] == 'today' ){
    $selectQuery = "SELECT * FROM `sms_in` WHERE `sent_dt`= now() AND `sender_number` = 
  (
    SELECT `mobile1` FROM `test` WHERE `mobile1` = `sms_in`.`sender_number` AND 
    `id` = '$id'
  )";
}else if ($_POST['date'] =='week'){
  
   $selectQuery = "SELECT * FROM `sms_in` WHERE `sent_dt` = `sent_dt`>(`sent_dt`-(60*60*24*7)) AND `sender_number` = 
  (
    SELECT `mobile1` FROM `test` WHERE `mobile1` = `sms_in`.`sender_number` AND 
    `id` = '$id'
  )";
}else if ($_POST['date'] =='month'){
  $selectQuery = "SELECT * FROM `sms_in` WHERE `sent_dt` = `sent_dt`>(`sent_dt`-(60*60*24*30)) AND `sender_number` = 
  (
    SELECT `mobile1` FROM `test` WHERE `mobile1` = `sms_in`.`sender_number` AND 
    `id` = '$id'
  )";
}else if (isset($_POST['date1']) AND isset($_POST['date1'])){
  $date1 = $_POST['date1'];
  $date2 = $_POST['date2'];

  $selectQuery = "SELECT * FROM `sms_in` WHERE `sent_dt` between $date1 AND $date2 AND `sender_number` = 
  (
    SELECT `mobile1` FROM `test` WHERE `mobile1` = `sms_in`.`sender_number` AND 
    `id` = '$id'
  )";
}else{
  $selectQuery = "SELECT * FROM `sms_in` WHERE `sender_number` = 
  (
    SELECT `mobile1` FROM `test` WHERE `mobile1` = `sms_in`.`sender_number` AND 
    `id` = '$id'
  )";
}
echo "$selectQuery";
$selectResult = mysql_query($selectQuery)or die(mysql_error());
while($ans = mysql_fetch_assoc($selectResult)) {
?>
    <tr>
      <td><?=$ans['id']?></td>
      <td><?=$ans['sent_dt']?></td>
      <td><?=$ans['sms_text']?></td>
    </tr>


vikas dwivedi

unread,
Sep 5, 2013, 2:12:48 PM9/5/13
to xamp...@googlegroups.com
Hello Sugam,

Please provide MYSQL tables with data as queries so we can create same DB tables to test script

If your code is more than 25 lines then please paste your code on http://pastebin.com


regards !
vikas dwivedi 


Ashish Choudhary

unread,
Sep 5, 2013, 2:20:35 PM9/5/13
to xamp...@googlegroups.com

There are several methods but I think this will be the best as it subtracts the time.
This will also be good for exceptional cases also like today is 2nd jan 2013 and one week before will be 27 Dec 2012.

$date = date('Y-m-d H:i:s',time()-(7*86400)); // 7 days ago
$sql = "SELECT * FROM table WHERE date <='$date' ";




Ashish Choudhary

unread,
Sep 5, 2013, 2:24:24 PM9/5/13
to xamp...@googlegroups.com
Correction: one week before will be was 27 Dec 2012.

sugam prakash

unread,
Sep 5, 2013, 2:49:23 PM9/5/13
to xamp...@googlegroups.com
test1.sql

sanjay adhek

unread,
Sep 5, 2013, 3:47:27 PM9/5/13
to xamp...@googlegroups.com
Hi Sugam,

I am sending the  query for your requirement. please try this query .I think your problem is solve.

$today= "SELECT * FROM `sms_in` WHERE `sent_dt` = CURDATE()";

$week =SELECT * FROM `sms_in` WHERE `sent_dt` BETWEEN NOW() - INTERVAL 7 DAY AND NOW() ORDER BY `id` DESC;

$month ="SELECT * FROM sms_in WHERE YEAR(sent_dt) = YEAR(CURDATE()) AND MONTH(sent_dt) = MONTH(CURDATE())";

Thanks &Regards
Sanjay Kumar Adhek.


sugam prakash

unread,
Sep 6, 2013, 1:55:50 AM9/6/13
to xamp...@googlegroups.com
Thanks Sanjay  it is working....


Reply all
Reply to author
Forward
0 new messages