从MySQL转换后PDO脚本不起作用

时间:2014-11-25 01:53:32

标签: php mysql ajax heroku pdo

提前感谢您的帮助。我在第一部分中有一个带有AJAX电子邮件收集表单的登录页面,我已根据此Tutsplus tutorial进行了改编。

但是,表单的PHP部分是用MySQL编写的,与我的Heroku服务器不兼容,因为它已被弃用。在stackoverflow上接受其他人的建议,我尝试将我的代码中的MySQL语句转换为PDO。但是很难让表格得以运作。

这是原始工作的MySQL代码

<?php
if($_GET['action'] == 'signup'){
// if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

	mysql_connect('hostname','username','password');
	mysql_select_db('databasename');

	$email = mysql_real_escape_string($_POST['emailfield']);

	//validate email address - check if input was empty
	if(empty($email)){
		$status = "error";
		$message = "You did not enter an email address!";
	}
	else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
			$status = "error";
			$message = "You have entered an invalid email address!";
	}
	else {
		$existingSignup = mysql_query("SELECT * FROM table WHERE signup_email_address='$email'");   
		if(mysql_num_rows($existingSignup) < 1){

			date_default_timezone_set('Asia/Singapore');
			$date = date('Y-m-d');
			$time = date('H:i:s');
			
			$insertSignup = mysql_query("INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
			if($insertSignup){ //if insert is successful
				$status = "success";
				$message = "You have been signed up!";	
			}
			else { //if insert fails
				$status = "error";
				$message = "Oops, Theres been a technical error!";	
			}
		}
		else { //if already signed up
			$status = "error";
			$message = "This email address has already been registered!";
		}
	}
	
	//return json response
	$data = array(
		'status' => $status,
		'message' => $message
	);

	echo json_encode($data);
	exit;
}
?>

这是我将上述MySQL代码转换为PDO失败的尝试

<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

	try {
		$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
	}
	catch(exception $e) {
		echo "Error, connection failure";
	}

	$email = $_POST['emailfield'];

	//validate email address - check if input was empty
	if(empty($email)){
		$status = "error";
		$message = "You did not enter an email address!";
	}
	else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
			$status = "error";
			$message = "You have entered an invalid email address!";
	}
	else {

		$existingSignup = "SELECT COUNT(*) FROM table WHERE signup_email_address='$email'";
		if ($res->fetchColumn() < 1) {

			date_default_timezone_set('Asia/Singapore');
			$date = date('Y-m-d');
			$time = date('H:i:s');
			
			$insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";

		    $query = $db->prepare($insertSignup);
		    $query->bindParam(':signupemail', $email);
		    $query->bindParam(':signupdate', $date);
		    $query->bindParam(':signuptime', $time);
		    $results = $query->execute();

			if($insertSignup){ //if insert is successful
				$status = "success";
				$message = "You have been signed up!";	
			}
			else { //if insert fails
				$status = "error";
				$message = "Oops, Theres been a technical error!";	
			}
		}
		else { //if already signed up
			$status = "error";
			$message = "This email address has already been registered!";
		}
	}
	
	//return json response
	$data = array(
		'status' => $status,
		'message' => $message
	);

	echo json_encode($data);
	exit;
}
?>

您能否给我建议纠正我的PDO代码?谢谢!

更新:

这是我更新的PDO代码:

<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){

	try {
		$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		error_reporting(-1);
	}
	catch(PDOexception $e) {
		echo "Error, connection failure";
	}

	$email = $_POST['emailfield'];

	//validate email address - check if input was empty
	if(empty($email)){
		$status = "error";
		$message = "You did not enter an email address!";
	}
	else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address
			$status = "error";
			$message = "You have entered an invalid email address!";
	}
	else {
			$existingSignup = "SELECT * FROM table WHERE signup_email_address='$email'";
			$stmt = $db->query($existingSignup);
			$row_count = $stmt->rowCount();
			if ($row_count < 1) {

			date_default_timezone_set('Asia/Singapore');
			$date = date('Y-m-d');
			$time = date('H:i:s');
			
			$insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";

		    $query = $db->prepare($insertSignup);
		    $query->bindValue(':signupemail', $email);
		    $query->bindValue(':signupdate', $date);
		    $query->bindValue(':signuptime', $time);
		    $results = $query->execute();

			if($insertSignup){ //if insert is successful
				$status = "success";
				$message = "You have been signed up!";	
			}
			else { //if insert fails
				$status = "error";
				$message = "Oops, Theres been a technical error!";	
			}
		}
		else { //if already signed up
			$status = "error";
			$message = "This email address has already been registered!";
		}
	}
	
	//return json response
	$data = array(
		'status' => $status,
		'message' => $message
	);

	echo json_encode($data);
	exit;
}
?>

但这是我得到的错误:致命错误:在第27行的index.php中的非对象上调用成员函数query()。

第27行:

$stmt = $db->query($existingSignup);

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的$existingSignup查询未执行。你必须使用类似的东西:

$stmt = $db->query($existingSignup);
$row_count = $stmt->rowCount();
echo $row_count.' rows selected

然后继续:if ($row_count < 1) {...}

了解更多信息。请参阅此链接:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers