I need help with my php code

Discussion in 'PHP' started by mby247, Jun 5, 2010.

  1. #1
    I got 2 usertable on my database but I wish to have the two usertable and run them independently can any help me with my php script? It is working but it would echo the "else" in the first usertable before running the query. What might be the problem?

    ====================================================

    <?php
    //Connect To Database
    $hostname='fooexample.com';
    $username='fooexample';
    $password='xxxxx';
    $dbname='fooexample';
    $usertable='ECMY4543';


    mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);

    //This array contains all correct tracking numbers
    $track_number = $_POST['track_number'];
    if(empty($track_number)){
    die('<b>Please go back & enter tracking number</b>');
    }
    else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
    die('<b>Invalid tracking number, please go back</b>');
    }
    else{
    $correct_tracking_numbers=array(ECMY4572957, BHSZ006933);
    if(in_array($track_number,$correct_tracking_numbers)){
    $query = 'SELECT * FROM ' . $usertable;
    $result = mysql_query($query) or die(mysql_error());
    echo "<table border='5'>
    <tr>
    <th>Date/Time</th>
    <th>Event</th>
    <th>Location</th>
    <th>Details</th>
    <th>Type of Move</th>
    </tr>";
    while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row['Date/Time'] . "</td>";
    echo "<td>" . $row['Event'] . "</td>";
    echo "<td>" . $row['Location'] . "</td>";
    echo "<td>" . $row['Details'] . "</td>";
    echo "<td>" . $row['Type of Move'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";

    }else{
    echo '<b>The tracking number is invalid, Please go back & enter a Valid Tracking Number!</b>';
    }
    }


    //Connect To Database 2
    $hostname='fooexample.com';
    $username='fooexample';
    $password='xxxxx';
    $dbname='fooexample';
    $usertable1='FTYX345';


    mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);

    //This array contains all correct tracking numbers for database 2
    $track_number = $_POST['track_number'];
    if(empty($track_number)){
    die('<b>Please go back & enter tracking number</b>');
    }
    else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
    die('<b>Invalid tracking number, please go back</b>');
    }
    else{
    $correct_tracking_numbers2=array(FTYX34555);
    if(in_array($track_number,$correct_tracking_numbers2)){
    $query = 'SELECT * FROM ' . $usertable1;
    $result = mysql_query($query) or die(mysql_error());
    echo "<b>SHIPMENT HISTORY</b>";
    echo "<br>";
    echo "<table border='5'>
    <tr>
    <th>Date/Time</th>
    <th>Event</th>
    <th>Location</th>
    <th>Details</th>
    <th>Type of Move</th>
    </tr>";
    while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row['Date/Time'] . "</td>";
    echo "<td>" . $row['Event'] . "</td>";
    echo "<td>" . $row['Location'] . "</td>";
    echo "<td>" . $row['Details'] . "</td>";
    echo "<td>" . $row['Type of Move'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";

    }else{
    echo '<b>The tracking number is invalid, Please go back & enter a Valid Tracking Number!</b>';
    }
    }
    ?>
    </body>
    </html>
     
    mby247, Jun 5, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    try to quote your array
    
    $correct_tracking_numbers=array(ECMY4572957, BHSZ006933);
    // ...
    $correct_tracking_numbers2=array(FTYX34555);
    
    PHP:
     
    gapz101, Jun 5, 2010 IP
  3. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply. I guess tats what I ve done. It is working but wen it runs, it show "please go back and enter a valid tracking number" which is the "else" for the first database connect. Can you please help me with that? Why is it showing "please go back and enter a valid tracking number"? or can u help me rewrite the script so I can understand beta?
     
    mby247, Jun 5, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    please change your error message to identify which error, because checking your script would definitely output one error message
     
    gapz101, Jun 5, 2010 IP
  5. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the reply. I guess wen I use any of the tracking number in the arrays, it runs the 2 usertable which is if I track $correct_tracking_numbers2=array(FTYX34555867), it would run the usertable but would also run $correct_tracking_numbers=array(ECMY4572957, BHSZ006933) and output "The tracking number is invalid, Please go back & enter a Valid Tracking Number!" and vice versa. How do I make sure only the required usertable run? Please advice. Thanks.
     
    mby247, Jun 5, 2010 IP
  6. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    
    <?php
    
    
    $track_number = trim($_POST['track_number']);
    
    if(empty($track_number)){
    	die('<b>Please go back & enter tracking number</b>');
    } else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
    	die('<b>Invalid tracking number, please go back</b>');
    }
    
    
    
    $correct_tracking_numbers1=array(ECMY4572957, BHSZ006933);
    $correct_tracking_numbers2=array(FTYX34555);
    
    $track1 = FALSE;
    $track2 = FALSE;
    
    
    if(in_array($track_number,$correct_tracking_numbers1)) {
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='ECMY4543';
    	$track1 = TRUE;
    
    } else if(in_array($track_number,$correct_tracking_numbers2)) { 
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='FTYX345';
    	$track2 = TRUE;
    }
    
    
    //Connect To Database
    mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);
    
    $query = 'SELECT * FROM ' . $usertable;
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    if($track1 == TRUE){
    
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else if($track2 == TRUE) {
    	
    	echo "<b>SHIPMENT HISTORY</b>";
    	echo "<br>";
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else {
    
    	echo '<b>The tracking number is invalid, Please go back & enter a Valid Tracking Number!</b>';
    
    }
    
    ?>
    
    PHP:
    hope that works..
     
    mehmetm, Jun 5, 2010 IP
  7. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Dear friend. Thanks for the help. I tried using the script but it only runs the $track1==True and the $track2==True it would echo error with the MySql Syntax. Any idea what might be the problem? Please help.
     
    mby247, Jun 6, 2010 IP
  8. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8
    I guess the problem was the syntax problem at defining array values. so try the below one please and let me know the result..

    
    <?php
    
    
    $track_number = trim($_POST['track_number']);
    
    if(empty($track_number)){
    	die('<b>Please go back & enter tracking number</b>');
    } else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
    	die('<b>Invalid tracking number, please go back</b>');
    }
    
    
    
    $correct_tracking_numbers1=array('ECMY4572957', 'BHSZ006933');
    $correct_tracking_numbers2=array('FTYX34555');
    
    $track1 = FALSE;
    $track2 = FALSE;
    
    
    if(in_array($track_number,$correct_tracking_numbers1)) {
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='ECMY4543';
    	$track1 = TRUE;
    
    } else if(in_array($track_number,$correct_tracking_numbers2)) { 
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='FTYX345';
    	$track2 = TRUE;
    }
    
    
    //Connect To Database
    mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);
    
    $query = 'SELECT * FROM ' . $usertable;
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    if($track1 == TRUE){
    
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else if($track2 == TRUE) {
    	
    	echo "<b>SHIPMENT HISTORY</b>";
    	echo "<br>";
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else {
    
    	echo '<b>The tracking number is invalid, Please go back & enter a Valid Tracking Number!</b>';
    
    }
    
    ?>
    
    PHP:
     
    mehmetm, Jun 6, 2010 IP
  9. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi friend..thanks for ur help am realy g8tful. I tried the script and it work out perfectly but got a problem with if wrong or any input is inserted to the $track_number, it would echo "please enter a correct tracking number" because for now it jst says

    "Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/t/r/a/transasiagroup/html/trackcheck.php on line 87
    Unable to connect to database! Please try again later.

    Also how do I connect table put to 5 or 6 like u have done for the order 2? eg. I got another table of tablename "859640458". Please also advice. I just want to get this thing straight as it as ben giving me sleepless night and worries. I must also confess am a newbies and I guess with this help and study, I would be good in php. Thanks once again bruv...very g8tful.
     
    mby247, Jun 7, 2010 IP
  10. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #10
    I'm happy to hear that :)
    The below code modified to echo a warning in the situtation of wrong input is inserted.

    
    <?php
    
    
    $track_number = trim($_POST['track_number']);
    
    if(empty($track_number)){
    	die('<b>Please go back & enter tracking number</b>');
    } else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
    	die('<b>Invalid tracking number, please go back</b>');
    }
    
    
    
    $correct_tracking_numbers1=array('ECMY4572957', 'BHSZ006933');
    $correct_tracking_numbers2=array('FTYX34555');
    
    $track1 = FALSE;
    $track2 = FALSE;
    
    
    if(in_array($track_number,$correct_tracking_numbers1)) {
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='ECMY4543';
    	$track1 = TRUE;
    
    } else if(in_array($track_number,$correct_tracking_numbers2)) { 
    	
    	$hostname='fooexample.com';
    	$username='fooexample';
    	$password='xxxxx';
    	$dbname='fooexample';
    	$usertable='FTYX345';
    	$track2 = TRUE;
    	
    } else {
    	die('<b>please enter a correct tracking number</b>');
    }
    
    
    //Connect To Database
    mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);
    
    $query = 'SELECT * FROM ' . $usertable;
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    if($track1 == TRUE){
    
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else if($track2 == TRUE) {
    	
    	echo "<b>SHIPMENT HISTORY</b>";
    	echo "<br>";
    	echo "<table border='5'>
    	<tr>
    	<th>Date/Time</th>
    	<th>Event</th>
    	<th>Location</th>
    	<th>Details</th>
    	<th>Type of Move</th>
    	</tr>";
    	while($row = mysql_fetch_array($result)){
    	echo "<tr>";
    	echo "<td>" . $row['Date/Time'] . "</td>";
    	echo "<td>" . $row['Event'] . "</td>";
    	echo "<td>" . $row['Location'] . "</td>";
    	echo "<td>" . $row['Details'] . "</td>";
    	echo "<td>" . $row['Type of Move'] . "</td>";
    	echo "</tr>";
    	}
    	echo "</table>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	echo "<br>";
    	
    } else {
    
    	echo '<b>The tracking number is invalid, Please go back & enter a Valid Tracking Number!</b>';
    
    }
    
    ?>
    
    PHP:
    However, I don't get your other question. Could you be more clear ?
     
    mehmetm, Jun 7, 2010 IP
  11. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for ur reply. Am really getting it straight n thanks again. On my other question, how do I connect or run other table on same database as the two tables we have. Lets say I have another two table name "34354348" * "TY343478" on same database. How do I outline the 4 or connect all as the former two? I hope you understand. What I am try to say in essence is, I have four table, how I can track each independently like the two you have help me solved.
     
    mby247, Jun 7, 2010 IP
  12. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #12
    if I understand right below code is for your work.
    What I must say about the code is that as I have thought your all tables in the same database, I defined database connection variables once in the code. if you want to connect some tables on another database, you should define that variables under the track_number control loop (if).
    I hope, that was clear :)

    
    <?php
    
    
    $track_number = trim($_POST['track_number']);
    
    if(empty($track_number)){
        die('<b>Please go back & enter tracking number</b>');
    } else if(strlen($track_number) <= 0 || strlen($track_number) > 15){
        die('<b>Invalid tracking number, please go back</b>');
    }
    
    
    $correct_tracking_numbers1=array('ECMY4572957', 'BHSZ006933');
    $correct_tracking_numbers2=array('FTYX34555');
    $correct_tracking_numbers3=array('TRACKING_NUMBERS');
    $correct_tracking_numbers4=array('TRACKING_NUMBERS');
    
    
    if(in_array($track_number,$correct_tracking_numbers1)) {
    
        $usertable='ECMY4543';
    
    } else if(in_array($track_number,$correct_tracking_numbers2)) { 
        
        $usertable='FTYX345';
    	
    } else if(in_array($track_number,$correct_tracking_numbers3)) { 
    
        $usertable='TABLE3';
    	
    } else if(in_array($track_number,$correct_tracking_numbers4)) { 
        
        $usertable='TABLE4';
        
    } else {
        die('<b>please enter a correct tracking number</b>');
    }
    
    
    //Connect To Database
    $hostname='fooexample.com';
    $username='fooexample';
    $password='xxxxx';
    $dbname='fooexample';
    $conn = mysql_connect($hostname,$username, $password) or die('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);
    
    $query = 'SELECT * FROM ' . $usertable;
    $result = mysql_query($query) or die(mysql_error());
    
    
    echo "<b>SHIPMENT HISTORY</b><br />
    	  <table border='5'>
    	   <tr>
    	    <th>Date/Time</th>
    	    <th>Event</th>
    	    <th>Location</th>
    	    <th>Details</th>
    	    <th>Type of Move</th>
    	   </tr>";
    	   
    while($row = mysql_fetch_array($result)) {
    
    	echo "<tr>
    			<td>" . $row['Date/Time'] . "</td>
    			<td>" . $row['Event'] . "</td>
    			<td>" . $row['Location'] . "</td>
    			<td>" . $row['Details'] . "</td>
    			<td>" . $row['Type of Move'] . "</td>
    		  </tr>";
    }
    
    echo "</table><br /><br /><br /><br /><br />";
    
    
    unset($row);
    mysql_free_result($result);
    mysql_close($conn);
       
    ?>
    
    PHP:
     
    mehmetm, Jun 7, 2010 IP
  13. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for the reply. If I understand you very well...your //Connect to database doesn't has $usertable. So in the $query = 'SELECT * FROM ' . $usertable;, should I quote the usertable? as in

    $query = 'SELECT * FROM ' . Table4;?

    Pls confirm and also please explain to me the

    unset($row);
    mysql_free_result($result);

    Thanks.
     
    mby247, Jun 7, 2010 IP
  14. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #14
    hi again

    the value of $usertable variable is dependent to the value of $track_number. So you should define each value of $usertable according to the $track_number. in the code, you should replace TABLE3 and TABLE4 with your 3. and 4. table names.

    you don't have to quote anything.
    $query = "SELECT * FROM $usertable";
    and
    $query = 'SELECT * FROM ' . $usertable;
    are the same.

    unset($row); means destroy the variable $row and its value. So memory would be happy. You can use this function to destroy the variables where the variable work is done.
    mysql_free_result($result); means emptying the $result variable. So by this functions resource usage of your code would be reduced.

    I hope, I was clear :)
     
    mehmetm, Jun 7, 2010 IP
  15. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks for ur patient and attention in tutoring me. I also much appreciate. I would get it into work and see how it goes and I would revert back to you but I don't knw how 2 say a very BIG Thank You 2 U. Thanks very much for the time and all. Do you mind sending me your yahoo id so I can add U up should I case I would need ur knowledge? Thanks very much.
     
    mby247, Jun 7, 2010 IP
  16. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #16
    :) hi brother, you're welcome. the purpose of forums is to share knowledge. You want to learn something and ask patiently and somebody replies to you if he/she knows the answer, that's it :)
    I will pm my yahoo id as well.
     
    mehmetm, Jun 7, 2010 IP
  17. mby247

    mby247 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Hi....the following error msg "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '859640458' at line 1" is echoing in Table 3 & 4

    } else if(in_array($track_number,$correct_tracking_numbers3)) {

    $usertable='TABLE3';

    } else if(in_array($track_number,$correct_tracking_numbers4)) {

    $usertable='TABLE4';
     
    mby247, Jun 7, 2010 IP