我有一个带有 jquery 验证和验证码的表单,但我无法发布到数据库。我是新手,我的代码很丑,我有两次尝试。都失败了。我想我正在混合 OO 和程序风格。
第一个代码
<?php
require_once('recaptchalib.php');
$privatekey = "x";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$mysqli = new MySQLi("x","x","x","x");
if(mysqli_connect_errno()){
echo "failed to connect to db" . mysqli_connect_error();
exit();
}
$cf_firstname=$_POST[cf_firstname];
$cf_lastname=$_POST[cf_lastname];
$cf_address=$_POST[cf_address];
$cf_address2=$_POST[cf_address2];
$cf_city=$_POST[cf_city];
$cf_state=$_POST[cf_state];
$cf_zipcode=$_POST[cf_zipcode];
$cf_contact1=$_POST[cf_contact1];
$cf_contact2=$_POST[cf_contact2];
$cf_contact3=$_POST[cf_contact3];
$cf_message=$_POST[cf_message];
$cf_email=$_POST[cf_email];
$cf_phone=$_POST[cf_phone];
$cf_sale=$_POST[cf_sale];
$sql="INSERT INTO Contacts (`cf_firstname`, `cf_lastname`, `cf_address`, `cf_address2`, `cf_city`, `cf_state`, `cf_zipcode`, `cf_contact1`, `cf_contact2`, `cf_contact3`, `cf_message`, `cf_email`, `cf_phone`, `cf_sale`) VALUES ($cf_firstname,$cf_lastname,$cf_address,$cf_address2,$cf_city,$cf_state,$cf_zipcode,$cf_contact1,$cf_contact2,$cf_contact3,$cf_message,$cf_email,$cf_phone,$cf_sale)";
$result = $mysqli->query($sql);
if (!$result) {
printf("%s\n", $mysqli->error());
exit();
}
echo "query run" ;
$stmt->execute();
$stmt->close();
}
?>
致命错误:调用未定义的方法 mysqli::error() 第 38 行
第二次尝试
<?php
require_once('recaptchalib.php');
$privatekey = "x";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$mysqli = new MySQLi("x");
if(mysqli_connect_errno()){
echo "failed to connect to db" . mysqli_connect_error();
exit();
}
$stmt = $mysqli->prepare("INSERT INTO Contacts (cf_firstname, `cf_lastname`, `cf_address`, `cf_address2`, `cf_city`, `cf_state`, `cf_zipcode`, `cf_contact1`, `cf_contact2`, `cf_contact3`, `cf_message`, `cf_email`, `cf_phone`, `cf_sale`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
echo $mysqli->error;
$stmt->bind_param("ssssssssssssss",$cf_firstname,$cf_lastname,$cf_address,$cf_address2,$cf_city,$cf_state,$cf_zipcode,$cf_contact1,$cf_contact2,$cf_contact3,$cf_message,$cf_email,$cf_phone,$cf_sale);
$cf_firstname=$_POST[cf_firstname];
$cf_lastname=$_POST[cf_lastname];
$cf_address=$_POST[cf_address];
$cf_address2=$_POST[cf_address2];
$cf_city=$_POST[cf_city];
$cf_state=$_POST[cf_state];
$cf_zipcode=$_POST[cf_zipcode];
$cf_contact1=$_POST[cf_contact1];
$cf_contact2=$_POST[cf_contact2];
$cf_contact3=$_POST[cf_contact3];
$cf_message=$_POST[cf_message];
$cf_email=$_POST[cf_email];
$cf_phone=$_POST[cf_phone];
$cf_sale=$_POST[cf_sale];
echo $mysqli->error;
$stmt->execute();
$stmt->close();
}
?>
我找不到任何错误,但不会发布到数据库。
我正在使用DW5.5,这种东西有更好的东西吗?
OO 或程序风格只是一种选择吗?一种风格在某些事情上更好吗?
我主要是复制示例来学习并使这件事起作用,我被困在这里。