关于 php 和 mysql 的另一个问题......如果有意义的话,我正在徘徊..
基本上在下面的代码上方..是表单..我希望它通过错误计数进行验证。(代码如下)如果计数错误为0,则运行“INSERT INTO”代码..如果计数错误为1或更多,显示错误和未发送的数据。
但是有一件事..下面所述的代码实际上并没有“工作”并且没有给出任何错误:(
所有评论都非常感谢!非常感谢
<?php
global $pdo;
$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<<<<<<<FORM GOES HERE>>>>>>>>
<?php
function validatePattern(&$errors, $field_list, $field_name, $pattern)
{
if (!isset($field_list[$field_name]) || $field_list[$firld_name] == '')
$errors[$field_name] = 'Required';
else if (!preg_match($pattern, $field_list[$field_name]))
$errors[$field_name] = 'Invalid';
}
$errors = array();
validatePattern($errors, $_GET, ':firstname', '/^[A-Z][a-zA-Z -]+$/');
validatePattern($errors, $_GET, ':surname', '/^[A-Z][a-zA-Z -]+$/');
validatePattern($errors, $_GET, ':Player1', '/^[0-9a-zA-Z_]$/');
validatePattern($errors, $_GET, ':Player2', '/^[0-9a-zA-Z_]$/');
validatePattern($errors, $_GET, ':Player3', '/^[0-9a-zA-Z_]$/');
validatePattern($errors, $_GET, ':Player4', '/^[0-9a-zA-Z_]$/');
validatePattern($errors, $_GET, ':Player5', '/^[0-9a-zA-Z_]$/');
if (count($errors) >0){
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
}else{
if(("$stmt = $pdo->prepare('INSERT INTO members (firstname, surname, DD, MM, YYYY, email, Player1, Player2, Player3, Player4, Player5)
VALUES (:firstname, :surname, :DD, :MM, :YYYY, :email, :Player1, :Player2, :Player3, :Player4, :Player5)');
$stmt->bindParam(':firstname', $_GET['firstname']);
$stmt->bindParam(':surname', $_GET['surname']);
$stmt->bindParam(':DD', $_GET['DD']);
$stmt->bindParam(':MM', $_GET['MM']);
$stmt->bindParam(':YYYY', $_GET['YYYY']);
$stmt->bindParam(':email', $_GET['email']);
$stmt->bindParam(':Player1', $_GET['Player1']);
$stmt->bindParam(':Player2', $_GET['Player2']);
$stmt->bindParam(':Player3', $_GET['Player3']);
$stmt->bindParam(':Player4', $_GET['Player4']);
$stmt->bindParam(':Player5', $_GET['Player5']);
$stmt->execute();")){
echo "<font face='Verdana' size='2' color=green>Welcome, You have successfully signed up</font>";}
else{ echo "Database Problem, please contact Site admin";
}
?>