Page 1 of 3

need help with ereg function

Posted: Sat Apr 17, 2004 12:06 pm
by mikewooten
--------------------------------------------------------------------------------

i need help with the ereg function. i am getting this error message:
Fatal error: Cannot redeclare phonecheck() (previously declared in
/home/username/public_html/folder/thanks2.php:257) in /home/username/public_html/folder/thanks2.php
on line 257
i have tried a few different functions to get this to work, but still having the same error message, can someone please help me to get this to work without having this error message?

here is what i am trying to use but coming up with errors. i am not redeclaring this function anywhere else in my code. i also try changing the name of the function, for example i try to change datecheck() to dateck() and still i keep getting the same error message. what else can i try for this to work and the error message to go away?
thanks


i also have these functions inside of a while loop and when i take these functions outside of a while loop, the form keeps looping and makes at least 45 copies of itself on the page. can someone help me in trying to get the page to stop copying itself and get this page to work without any errors and without anything copying itself?
thanks
let me know if you want me to post the whole page that i am working on.
here are the functions i have used:

Code: Select all

<?php

function datecheck($intext) { 
$theresults = ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $intext, $trashed); 
if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; } 
    
echo(""$intext" $isamatch<br>\n"); 
} 



?>

Code: Select all

<?php

if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $expire, $regs)) { 
   echo "$regs[3].$regs[2].$regs[1]"; 
} else { 
   echo "Invalid date format: $expire"; 
} 


?>

Code: Select all

<?php

function dateck($expire) 
{ 
  // check an email address is possibly valid 
  if (ereg("([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})", $expire)) 
    return true; 
  else   
    return false; 
} 


?>

Posted: Sun Apr 18, 2004 2:54 am
by redmonkey
I don't think the problem has to do with your ereg usage.

It looks like either the function declaration is inside a loop or the script file is being included/required more than once.

Posted: Sun Apr 18, 2004 12:28 pm
by mikewooten
the function is inside of a loop, that is why its coming up with that error, but if i take the function outside of the loop, the form keeps looping and makes at least 45 copies of itself on the page. do you know why it is doing that and how it can be fixed? would you like me to post the code for the whole page that i am working on?

thanks

Posted: Sun Apr 18, 2004 12:44 pm
by redmonkey
Much as I don't particularly want to wade through 100's of lines of code, I think that is the best option.

The quick 'hack' would be to encapsulate the function declartion within and if statement like...

Code: Select all

if (!function_exists('phonecheck'))
{
  // declare the function
}
However while that may work it merely swerves the issue, it would be better to post the code and see if we can solve the root of the problem.

Posted: Sun Apr 18, 2004 1:05 pm
by mikewooten
i'll get the code and post it for you to see, but where you have
//declare the function

would the code look like this?

Code: Select all

<?php

if (!function_exists('phonecheck')) 
{ 
  
function datecheck($intext) { 
$theresults = ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $intext, $trashed); 
if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; } 
    
echo(""$intext" $isamatch<br>\n"); 
} 


} 




?>

Posted: Sun Apr 18, 2004 1:21 pm
by mikewooten
here is the code for the page that i am working on.

Code: Select all

<?php

session_start();

       // include function files for this application
require_once('bookmark_fns.php'); 

//create short variable names
$username = $HTTP_POST_VARS['username'];
$passwd = $HTTP_POST_VARS['passwd'];

if ($username && $passwd)
// they have just tried logging in
{
    if (login($username, $passwd))
    {
      // if they are in the database register the user id
      $HTTP_SESSION_VARS['valid_user'] = $username;
    }  
    else
    {
      // unsuccessful login
      do_html_header('Problem:');
      echo 'You could not be logged in. 
            You must be logged in to view this page.';
      do_html_url('login.php', 'Login');
      do_html_footer();
      exit;
    }      
}


$db_name = "database"; 
$table_name = "checkout"; 

$link_id = mysql_connect("localhost", "username", "password")  
or die("Couldn't connect."); 

$db = mysql_select_db($db_name, $link_id) 
    or die("Couldn't select database."); 

mysql_query ("INSERT INTO $table_name (billfirstname, billlastname, billstreetaddr, billcity, billstate, billzipcode, billcountry, billcounty, email, phone, payment, creditnum, expire, questions, shipfirstname, shiplastname, shipstreetaddr, shipcity, shipstate, shipzipcode, shipcountry, shipcounty)  
			VALUES  ('$billfirstname', '$billlastname', '$billstreetaddr', '$billcity', '$billstate', '$billzipcode', '$billcountry', '$billcounty', '$email', '$phone', '$payment', '$creditnum', '$expire', '$questions', '$shipfirstname', '$shiplastname', '$shipstreetaddr', '$shipcity', '$shipstate', '$shipzipcode', '$shipcountry', '$shipcounty')"); 

 
echo "$select<br>";
					
	$host    ="localhost";
	$db_user="username";
	$db_pass="password";
	$database="database";
					
mysql_connect($host,$db_user,$db_pass);
mysql_select_db($database) or die ("Unable to select database");    


echo "<center>Thank You For Shopping With Us!</center><br>"; 
echo "<center>";  
check_valid_user(); 

echo "</center>"; 
echo "<br>";
echo "<center><a href='logout.php'>Click here to Logout</a></center>";
echo "<br>";
include("check.php");
echo "<br>"; 
echo "<strong>Your Billing and Shipping information!</strong>";

$query="SELECT * FROM $table_name WHERE billfirstname='$billfirstname' and billlastname='$billlastname' and billstreetaddr='$billstreetaddr' and billcity='$billcity' and billstate='$billstate' and billzipcode='$billzipcode' and billcountry='$billcountry' and billcounty='$billcounty' and email='$email' and phone='$phone' and payment='$payment' and creditnum='$creditnum' and expire='$expire' and questions='$questions' and shipfirstname='$shipfirstname' and shiplastname='$shiplastname' and shipstreetaddr='$shipstreetaddr' and shipcity='$shipcity' and shipstate='$shipstate' and shipzipcode='$shipzipcode' and shipcountry='$shipcountry' and shipcounty='$shipcounty'";  

$result=mysql_query($query);
$num=mysql_numrows($result);  

function datecheck($theresults) {
   $theresults = ereg("([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})", $intext, $trashed);
   if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; }
 
  echo("$isamatch");
} 

function phonecheck($intext) {
   $theresults = ereg("([0-9]{3})-([0-9]{3})-([0-9]{4})", $intext, $trashed);
   if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; }
  
 echo(""$intext" $isamatch<br>\n");
} 

function ccnumcheck($intext) {
   $theresults = ereg("([0-9]{16})", $intext, $trashed);
   if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; }
  
 echo(""$intext" $isamatch<br>\n");
} 

function emailcheck($intext) {
   $theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $intext, $trashed);
   if ($theresults) { $isamatch = ""; } else { $isamatch = "No - NOT A MATCH"; }
  
 echo(""$intext" $isamatch<br>\n");
} 

$result = mysql_query($query, $link_id);

while($query_data = mysql_fetch_row($result)) {   

echo "<table>"; 

echo "<tr>";
echo "<td>billfirstname</td>";
echo "<td>",$query_data[0],"</td>";
echo "</tr>";

echo "<tr>";
echo "<td>billlastname</td>";
echo "<td>",$query_data[1],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>billstreetaddr</td>";
echo "<td>",$query_data[2],"</td>";
echo "</tr>";  

echo "<tr>";
echo "<td>billcity</td>";
echo "<td>",$query_data[3],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>billstate</td>";
echo "<td>",$query_data[4],"</td>";
echo "</tr>";  

echo "<tr>";
echo "<td>billzipcode</td>";
echo "<td>",$query_data[5],"</td>"; 
echo "</tr>"; 

echo "<tr>";
echo "<td>billcountry</td>";
echo "<td>",$query_data[6],"</td>";
echo "</tr>";

echo "<tr>";
echo "<td>email<br>"; 
echo "</td>";
echo "<td>",emailcheck("$email"),"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>phone</td>";
echo "<td>",phonecheck($phone),"</td>";
echo "</tr>";

echo "<tr>";
echo "<td>payment</td>";
echo "<td>",$query_data[10],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>creditnum</td>";
echo "<td>",ccnumcheck($creditnum),"</td>";
echo "</tr>";

echo "<tr>";
echo "<td>expire</td>";
echo "<td>",datecheck($expire),"</td>";
echo "</tr>";  

echo "<tr>";
echo "<td>questions</td>";
echo "<td>",$query_data[13],"</td>";
echo "</tr>";   

echo "<tr>";
echo "<td>shipfirstname</td>";
echo "<td>",$query_data[14],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>shiplastname</td>";
echo "<td>",$query_data[15],"</td>";
echo "</tr>";  

echo "<tr>";
echo "<td>shipstreetaddr</td>";
echo "<td>",$query_data[16],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>shipcity</td>";
echo "<td>",$query_data[17],"</td>";
echo "</tr>";

echo "<tr>";
echo "<td>shipstate</td>";
echo "<td>",$query_data[18],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>shipzipcode</td>";
echo "<td>",$query_data[19],"</td>";
echo "</tr>"; 

echo "<tr>";
echo "<td>shipcountry</td>";
echo "<td>",$query_data[20],"</td>";
echo "</tr>"; 

echo "</table>";     
                        
                        if(isset($_COOKIE['cartId']))  { 
                        
                        echo "<center><strong>----- cookie is set ----- </strong></center>";
                        
                        }
                        
                        else {
                        
                        echo "<center><strong>----- cookie is NOT set ----- </strong></center>";                        
                        }
}



?>

Posted: Sun Apr 18, 2004 1:51 pm
by redmonkey
Regarding the function_exists(), yes.

The first thing with the code you posted is that it doesn't match with the errors you quoted earlier e.g. there is no line 257?

Another point is your use of the function results is not quite correct. While your functions and calls probably work, your calls to functions are looking for a returned value, whereas your functions are echoing a value, while this in many cases works, it is not quite correct.

Now, I don't see that the function declaration is in a loop, it is only declared once and is not part of the loop. I would suspect that the file 'bookmark_fns.php' probably includes/requires this script file, or perhaps it includes/requires another file which in turn includes/requires this script file.

Posted: Sun Apr 18, 2004 2:21 pm
by mikewooten
how can i get the function results to be correct, what would i need to do to get it correct?

when i posted the code, i took the functions outside of the loop. the bookmark_fns.php file includes other files of the site

Posted: Sun Apr 18, 2004 2:53 pm
by redmonkey
The more correct way is to have your function return a value rather than echo it staright out.

e.g.

Code: Select all

function ret_upper_case($text){

  $text = strtoupper($text);
  return $text;

}

echo 'Just ', ret_upper_case('a quick return'), ' test';
Obviously this is not the most useful function in the world but should suffice as an example.

So where are we at with your code? what is the error you are experiencing at the moment?

Are you experiencing multiple results being displayed? If so, then that would be down to your SQL query, I assume you actually only want one row, have you confirmed how many rows are actually returned (try echoing $num). I am not quite sure why you don't assign each row a unique id then query for the unique id as opposed to checking all those fields.

Posted: Sun Apr 18, 2004 3:32 pm
by mikewooten
there are multiple results being displayed. i echoed $num to see how many rows are being returned. i ran the page it it displayed 51 rows are being displayed. if it is down to my sql query, then what should be the sql query i should use for the results to display? in my database, i do have a column for id and every time a user checks out, it assigns them an id.
for querying the uniqu id, what would be the query for that?

Posted: Sun Apr 18, 2004 4:30 pm
by redmonkey
After initially inserting the info into the database you can use mysql_insert_id() which (assuming you are using a mySQL database and have an auto incrementing field) will return the value of the last auto incremented id, you can use this as your unique id.

Posted: Sun Apr 18, 2004 5:02 pm
by mikewooten
what would i need to do after i used the mysql_insert_id(), i have put it in the page and it displayed on my page as "Last inserted record has id 136"
once i put that in my code, how would i use it? or what else would i need to do to stop the multiple results being displayed?
thanks

Posted: Sun Apr 18, 2004 5:20 pm
by redmonkey
Assign the value of the mysql_insert_id to a variable, then replace....

Code: Select all

$query="SELECT * FROM $table_name WHERE billfirstname='$billfirstname' and billlastname='$billlastname' and billstreetaddr='$billstreetaddr' and billcity='$billcity' and billstate='$billstate' and billzipcode='$billzipcode' and billcountry='$billcountry' and billcounty='$billcounty' and email='$email' and phone='$phone' and payment='$payment' and creditnum='$creditnum' and expire='$expire' and questions='$questions' and shipfirstname='$shipfirstname' and shiplastname='$shiplastname' and shipstreetaddr='$shipstreetaddr' and shipcity='$shipcity' and shipstate='$shipstate' and shipzipcode='$shipzipcode' and shipcountry='$shipcountry' and shipcounty='$shipcounty'";
with....

Code: Select all

$query = "SELECT * FROM `$table_name` WHERE `NAME_OF_AUTO_INCREMENT_FIELD` = $insert_id_variable";
Although, essentially, you do not need to query the database (unless to provide a quick sanity check) as you have the info already in the various variables.

Posted: Sun Apr 18, 2004 5:38 pm
by mikewooten
i tried your example and this is the error message that i got, how would you fix this?
do i need to get rid of the query data and results?

line 216 is $num=mysql_numrows($result);

line 282 is while($query_data = mysql_fetch_row($result)) {

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/username/public_html/folder/thanks2.php on line 216

and

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/username/public_html/folder/thanks2.php on line 282

Posted: Sun Apr 18, 2004 6:03 pm
by redmonkey
That probably means that there is an error in the query somewhere.

try replacing....

Code: Select all

$result=mysql_query($query);
with....

Code: Select all

$result=mysql_query($query) or die ('ERROR : '. mysql_error());
Which should give a better indication as to any errors.

I'm assuming you did substitute 'NAME_OF_AUTO_INCREMENT_FIELD' and '$insert_id_variable' for their actual values?