Jump to content

Coming soon page, using Mysqli - Need help for setting it up as Mysql instead


Zerpex

Recommended Posts

Hello everyone, I'm new on this forum, and I really need some help, I've been following a guide on the internet how to make a "Coming Soon" page using PHP, Ajax and Mysql, it works as it should when using LocalHost, like WAMP or MAMP - but when I upload it to my server, it doesn't work, I just get a blank page - I searched on the net for pretty long time, and found that the Mysqli could be the problem, I went to the server and installed Mysqli plugin, but after I have installed the plugin it still don't work.. So I got the idea, making it just Mysql instead Mysqli, I'm not the best for converting stuff, and thats why I'm asking here if any can help me, so I can learn it.

 

<?php

require "includes/connect.php";

$msg = '';

if($_POST['email']){

// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

try{
	if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
		throw new Exception('Invalid Email!');
	}

	$mysqli->query("INSERT INTO email_test
					SET email='".$mysqli->real_escape_string($_POST['email'])."'");

	if($mysqli->affected_rows != 1){
		throw new Exception('This email already exists in the database.');
	}

	if($ajax){
		die('{"status":1}');
	}

	$msg = "Thank you!";

}
catch (Exception $e){

	if($ajax){
		die(json_encode(array('error'=>$e->getMessage())));
	}

	$msg = $e->getMessage();		
}
}
?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX-ed Coming Soon Page with jQuery and PHP | Tutorialzine Demo</title>

<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/nivo-slider.css" />

</head>

<body>

<div id="page">

    <h1>Coming Soon</h1>
    
    <div id="slideshowContainer">
        <div id="slideshow">
            <img src="img/slides/slide1.jpg" width="454" height="169" alt="Coming Soon: Our Awesome Web App">
            <img src="img/slides/slide2.jpg" width="454" height="169" alt="Extensive Functionality">
            <img src="img/slides/slide3.jpg" width="454" height="169" alt="Complete with an iPhone App">
        </div>
</div>
        
    <h2>Subscribe</h2>
    
    <form method="post" action="">
    	<input type="text" id="email" name="email" value="<?php echo $msg?>" />
        <input type="submit" value="Submit" id="submitButton" />
    </form>
    
</div>

<!-- Feel free to remove this footer -->

<div id="footer">
<div class="tri"></div>
<h1>AJAX-ed Coming Soon Page</h1>
<a class="tzine" href="http://tutorialzine.com/2010/10/ajaxed-coming-soon-page/">Read & Download on</a>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/jquery.nivo.slider.pack.js"></script>
<script src="js/script.js"></script>

</body>
</html>

 

I know the code is pretty long, and I know I should only post the most important part, but maybe there should be changed something else in the rest of the code too - as you can see I link to the connect.php, it's my info about username, password, and so, and then some of the Mysqli:

<?php

error_reporting(E_ALL ^ E_NOTICE);

$db_host = 'host';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'email_test';

@$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

if (mysqli_connect_errno()) {
die('<h1>Could not connect to the database</h1><h2>Please try again after a few moments.</h2>');
}

$mysqli->set_charset("utf8");


?>

 

The big problem is to convert it to normal Mysql so I should be able to use the code..

 

If any wanna help I would be so nice! Thank you anyway!

 

Best regards,

 

Zerpex

Link to comment
Share on other sites

How did you install it exactly? Did you restart Apache afterwards?

 

Just to get the terminology right by the way, MySQLi is an 'extension' of PHP.

 

The way I installed MySQLi on the server:

 

First loggin into cPanel, then going to PHP PEAR Packages, Searching for MySQLi and installed "MDB2_Driver_mysqli (1.5.0.b3 beta)

 

I should try restart Apache tho.

 

You should start by either logging or displaying errors, and see exactly what errors are reported.

 

Thats the problem, it don't give any errors, even when I have enabled full error reporting :S

Link to comment
Share on other sites

Oh, and by the way.. Forgot this:

 

MySQL Version: 5.1.51

 

CREATE TABLE `email_test` (

  `email` varchar(64) COLLATE utf8_unicode_ci NOT NULL,

  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`email`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Link to comment
Share on other sites

If you can install the mysqli extension, that would be the best choice, especially since you don't seem to have the necessary experience to convert the code to use mysql.

 

Define: my server? Is this a shared web hosting account, a VPS (Virtual Private Sever), or a dedicated server?

 

You might be able to find or write a database class that emulates mysqli.

 

Also, what does the phpinfo(); statement show for the error_reporting and display_errors settings and the php version?

Link to comment
Share on other sites

If you can install the mysqli extension, that would be the best choice, especially since you don't seem to have the necessary experience to convert the code to use mysql.

 

Define: my server? Is this a shared web hosting account, a VPS (Virtual Private Sever), or a dedicated server?

 

You might be able to find or write a database class that emulates mysqli.

 

Also, what does the phpinfo(); statement show for the error_reporting and display_errors settings and the php version?

 

I have installed mysqli extension now -

 

My server means a dedicated, we share it between 3 people, and on of the people is pretty good at server setup and so, but isn't home at the moment, so no way to get in contact with him tho..

 

Will look on the database class thing, and statement of error_reporting: 22519 - display_error: on - and PHP version is 5.3.3

 

But problem got solved, it was all - Thank you much for helping anyway PFMaBiSmAd :)

Link to comment
Share on other sites

Okay, I have marked this as unsolved again - after running mysqli for around 12 hours - some of the webshop and stuff on the server started to fuck up - so we need to spend few days getting the mysqli off..

 

I know I have a very little exp. with translating/converting mysqli to mysql, and I should read more about it.. but don't know where..

 

If any have the time, or any wanna help then it would be so wonderful!

 

Thank you very much already

 

Regards,

 

Zerpex

Link to comment
Share on other sites

How were they not 'fucking up' up before you even had MySQLi installed?

 

First of all, sorry about the 2 words.. but, things just don't work anymore, it's like mysqli really affects the webshops and some other sites.. Thats really weird :S

 

But we're trying to take it off, and we can't just enable it on 1 account on dedicated, without installing it as 2-3 VPS - tho, I may find a solution to convert mysqli to mysql instead..

 

anyway, thank you for fast reply

Link to comment
Share on other sites

Ha doesn't bother me ;)

 

I'm a little confused though. Were you using MySQLi previously in the past, but on a different server? The only 'conversion' needed would be to just change the way you query the database through the interface, i.e. the function/method calls. If you have PDO available on the server the conversion shouldn't be too difficult, as there's just some variation in the naming and handling.. But they're relatively similar to use, and they're both object orientated. Standard MySQL doesn't support OO though, so it would be a bit more tedious to convert to.

Link to comment
Share on other sites

No, it was a guide I was following on a site - and they used mysqli - I have now converted the most of the script, it writes to the database, but the problem I have now is when it need to tell if the email already is in the DB, or if it isn't - like if it isn't in the DB it should write "Thank you!" And when it is in the DB it should write "The email already exists in the Database!"

 

But when I'm using a mail that isn't in the DB it still write "The email already exists in the Database!"

 

So, it should be something like:

if($_POST['email']){

// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

try{
	if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
		throw new Exception('Invalid Email!');
	}

	mysql_query("INSERT INTO slcoding_emails
					SET email='".mysql_real_escape_string($_POST['email'])."'");

	if(mysql_affected_rows != 1){
		throw new Exception('This email already exists in the database.');
	}

	if($ajax){
		die('{"status":1}');
	}

	$msg = "Thank you!";

}
catch (Exception $e){

	if($ajax){
		die(json_encode(array('error'=>$e->getMessage())));
	}

	$msg = $e->getMessage();		
}
}

 

The problem is, that when the email isn't wrote to the DB yet, it still write the code:

throw new Exception('This email already exists in the database.');

 

hmm..

Link to comment
Share on other sites

You need to execute a SELECT query first to determine whether or not the email already exists.

 

So it should be something like this?

mysql_query("INSERT INTO slcoding_emails
					SET email='".mysql_real_escape_string($_POST['email'])."'");

	$result = mysql_query("SELECT email FROM slcoding_emails");

	if("$result != 1"){
		throw new Exception('This email already exists in the database.');
	}

Link to comment
Share on other sites

Okay, Now I have tried anything - off that small experience I have with php and mysql - and can't still get it working.

 

 

I made this:

$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

try{
	if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
		throw new Exception('Invalid Email!');
	}

	$result = mysql_query("SELECT email FROM slcoding_emails");
	$num_rows = mysql_num_rows($result);
	mysql_query("INSERT INTO slcoding_emails
					SET email='".mysql_real_escape_string($_POST['email'])."'");


	if("$num_rows != 1"){
		throw new Exception('This email already exists in the database.');
		}

	if($ajax){
		die('{"status":1}');
	}

	$msg = "Thank you!";

}
catch (Exception $e){

	if($ajax){
		die(json_encode(array('error'=>$e->getMessage())));
	}

	$msg = $e->getMessage();		
}
}

 

I know that there must be huge errors, how I set it up

 

like etc. when I Select the email col, from the tabel slcoding_emails, then I say

$num_rows = mysql_num_rows($result);

if("$num_rows != 1"){
		throw new Exception('This email already exists in the database.');
		}

 

then it should throw the message.. But don't work, because I think I need something, or have made some big mistakes..

 

I really want this to work, but yeah - it's hard, and if I get it fixed, I'll learn that to next time, and  remember it

 

Link to comment
Share on other sites

You should be developing and debugging your code on a WAMP/MAMP development system. You should also have error_reporting set to at least E_ALL or a -1 in your master php.ini so that all the php errors will be reported (it would have helped find the error with the mysql_affected_rows line of code.) You should also remove the line of code in that script that is setting error_reporting as that will hide some of the errors and make it take longer to troubleshoot your code.

 

Using mysql_affected_rows() (the function, not just the name mysql_affected_rows) after the INSERT query will tell you if the email got inserted it it returns a 1, but if it does not return a 1 that does mean that the email did not get inserted but that does not unconditionally mean that it was a duplicate email address. You would need to test if the mysql_errno() returned the value for a duplicate key (I think it is 1062 but you would need to test to make sure what value corresponds to a duplicate key.)

Link to comment
Share on other sites

Thank you very much all!

 

Got all problems fixed

 

final code:

<?php

require "includes/connect.php";

$msg = '';

mysql_select_db($db_name, $connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

if($_POST['email']){

// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

try{
	if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
		throw new Exception('Invalid Email!');
	}

	$result = mysql_query("SELECT email FROM test_emails");
	if(mysql_num_rows($result)){
		throw new Exception('This email already exists in the database.');
	} else {
		mysql_query("INSERT INTO test_emails SET email='".mysql_real_escape_string($_POST['email'])."'");{
			if($ajax){
				die('{"status":1}');
			}
		}

		$msg = "Thank you!";
	}
	}
	catch(Exception $e){
		if($ajax){
			die(json_encode(array('error'=>$e->getMessage())));
		}

		$msg = $e->getMessage();
		}
	}
?>

 

the reason to it use ajax is because the ajax search in the DB for dublicates, no no need to make the dublicate thing.

 

But really, without your guys help, I would not be able to fix it I think!

 

Thank you very much - Problem solved!

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.