Solved

script works in php 4 but not 5

Posted on 2011-03-22
17
363 Views
Last Modified: 2012-05-11
I have a php script which checks a table for user registering but counting the id field from a table , and if the count = 0 then it inserts a new record into mysql, but I have just bought a new laptop and set it up with php5 and also new version of mysql (if that makes a difference) and have discovered that it is no longer inserting a record. I was wondering if someone could point me in the right direction or list the things I need to check?
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'add')
{
	
	
	///Check if email is already registered in the system.
	$user_count = mysql_query("select count(id) sc_count from tblschoollogin where email = '".mysql_real_escape_string($_REQUEST['email'])."'");

	$row_locality = mysql_fetch_assoc($user_count);
	
	///If not registered simply enter into the system
	if($row_locality['sc_count'] == 0){
	mysql_query("insert into tblschoollogin set email = '".mysql_real_escape_string($_REQUEST['email'])."', name = '".mysql_real_escape_string($_REQUEST['name'])."', local_authority_id = ".$_REQUEST['local_authority_id'].", school_learning_est_id = ".$_REQUEST['school_learning_est_id'].", telephonenumber = '".mysql_real_escape_string($_REQUEST['telephonenumber'])."'");


	echo "Thank you for signup. Your profile will be reviewed and password will be emailed to you.<br><br>";
	echo "Please view  invitation ".'<a href="EngaugeUpload/Letter 2 Schools- self assessment pilot study Jan 27 -  010.doc">here</a>'." that is being sent to school's";
	echo '<p><a href="http://engaugeonline.co.uk" class="tab">go to home page</a></p>';
	
// send mail to admin with registration notification message
						$mail = new PHPMailer();
						//$mail->IsSMTP();
						$mail->Host = "auth.smtp.1and1.co.uk";
						$mail->SMTPAuth = false;
						$mail->Username = "info@engaugeonline.co.uk";
						$mail->Password = "frankblack";
						$mail->From = "info@engaugeonline.co.uk";
						$mail->To= "info@engaugeonline.co.uk";
						$mail->FromName = "Registration Administrator";
						$mail->AddAddress("info@engaugeonline.co.uk");
						$mail->IsHTML(true);
						$mail->Subject = "A new user has registered for Engauge Online";
						$mail->Body = "Please go to the Engauge Team Admin webpage to verify user and issue a password";
						if($mail->Send())
                           echo "Registration mail has been sent to the Engauge Team";
						else
							 echo "Mailer Error: " . $mail->ErrorInfo . "<br />";
			
// end of mail notification code
		
		exit;
	}else{
		echo "Email is already registered with us";
		exit;
	}

Open in new window

0
Comment
Question by:jezskill
  • 6
  • 4
  • 4
  • +1
17 Comments
 
LVL 16

Expert Comment

by:Chris Harte
Comment Utility
First, since it is a new install of mysql, make sure the php has access rights to update the database (in my experience this is usually the problem).

If that is okay then either the insert command is failing or the code never calls the insert command. Test the if first

    ///If not registered simply enter into the system

   echo "count is ".$row_locality['sc_count'];

    if ($row_locality['sc_count'] == 0)

if count is anything other that 0 it will never call the insert.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
The other thing that regularly screws up websites moving from PHP4 to PHP5 is register_globals. In PHP5 it is OFF by default whereas in early versions of PHP4 it was ON by default.

On your older server run

<?php
phpinfo();
?>

and scan for 'register_globals' - in most browsers pressing CTRL-F will pop a search box.
0
 
LVL 1

Author Comment

by:jezskill
Comment Utility
bportlock, turning the register_globals on hasnt resolved the issue.
0
 
LVL 5

Expert Comment

by:innotionent
Comment Utility
Where is your error catching?

Try the below code to see what sort of error you are having.
$mysql_connection = @mysql_pconnect("localhost","user","pass") or die("<hr><b>MySQL Connection Failed</b>: ".mysql_error()."<hr>");
	$mysql_querystring = "select * from table";
	$mysql_result = mysql_db_query($mysql_table,$mysql_querystring,$mysql_connection) or die("<hr><b>MySQL Query Failed</b>: ".mysql_error()."<hr>");

Open in new window

0
 
LVL 1

Author Comment

by:jezskill
Comment Utility
hi Munterman, I pasted th code into my code and the value is 0 so that bit seems ok, just wont insert the record into mysql table. I;m sure the access rights are ok as other scripts in this web application are inserting records into other tables. Could it be a Mysql thing?
0
 
LVL 5

Expert Comment

by:innotionent
Comment Utility
@jezskill - Please review the code I posted. You will be able to return a mysql error to better debug your code.
0
 
LVL 1

Author Comment

by:jezskill
Comment Utility
innotionent:

where in your code script do I declare the database name?
0
 
LVL 1

Author Comment

by:jezskill
Comment Utility
innotionent,

this is what I get with your code.

Notice: Undefined variable: mysql_table in C:\websites\sustrade\signup.php on line 143

--------------------------------------------------------------------------------
MySQL Query Failed: No database selected
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:Chris Harte
Comment Utility
"Inserts into other tables"

Have these tables got the same access rights as the ones that are working? Add mysql_error to the query line.


Also, echo the query to the screen then run it against the database to make sure it is correct.


mysql_query("insert into tblschoollogin set email = '".mysql_real_escape_string($_REQUEST['email'])."', name = '".mysql_real_escape_string($_REQUEST['name'])."', local_authority_id = ".$_REQUEST['local_authority_id'].", school_learning_est_id = ".$_REQUEST['school_learning_est_id'].", telephonenumber = '".mysql_real_escape_string($_REQUEST['telephonenumber'])."'") or die('the error is ', mysql_error());

Open in new window

0
 
LVL 5

Expert Comment

by:innotionent
Comment Utility
@jezskill: you need to define the variable $mysql_table. And it should equal the DB you are trying to query  from.
Sorry bout that.
0
 
LVL 1

Author Comment

by:jezskill
Comment Utility
It seems that it was a table column in the database set to not null but had no defuault value assigned. It was a date field and I was hoping to make the default value todays date i.e. now() but I have read this cannot be done for a date field data type, only timestamp. Please suggest how to divvy the points?
0
 
LVL 16

Expert Comment

by:Chris Harte
Comment Utility
Before you do anything else make sure register_globals is off. It was a very bad idea to turn them on, and they are now deprecated.

http://php.net/manual/en/security.globals.php

Other than that, were any of the answers useful? If not, you could ask for a refund.

Though personally I would give them all to me, I need them more than portlock. ;)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
"It was a very bad idea to turn them on, and they are now deprecated."

I wasn't suggesting that they should BE turned on, just that might might have BEEN on for the older server.


"Though personally I would give them all to me, I need them more than portlock. ;)"

:-D

Oh I dunno.... I only need another 50,000 or so to break the 2,000,000 total........ I won't object if Jezskill awards you all the point, my contribution to this was fairly minimal.

Cheers!

BP
0
 
LVL 16

Expert Comment

by:Chris Harte
Comment Utility
I wasn't suggesting that they should BE turned on, just that might might have BEEN on for the older server.

Yes, but he did turn them on, even though that is not what you suggested. I cannot over emphasise this, they must be turned off.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
"I cannot over emphasise this, they must be turned off."

I agree.

0
 
LVL 5

Accepted Solution

by:
innotionent earned 500 total points
Comment Utility
@ jezskill: Correct. Timestamp you can do on update current time stamp.
With a date I suggest using 0000-00-00 00:00:00 as your default.
That way you can filter out those results in your queries.
Though on insert I would just use NOW().
0
 
LVL 1

Author Closing Comment

by:jezskill
Comment Utility
In direct assistance appreciated
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now