我有一个表单,有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或程序风格只是一种选择吗?在某些方面,一种风格更好吗?
我主要是复制示例来学习并让这个东西起作用,我被困在这里。
答案 0 :(得分:1)
你的第一个例子看起来很不错。不要被错误信息吓倒 - 他们随时为您提供帮助。 “致命错误:调用未定义的方法mysqli :: error()第38行”意味着在第38行你试图做一些事情,根据php,它是一个不存在的函数。
“未定义的方法mysqli :: error()”是你的线索。看看关于如何在php手册中使用mysqli的示例1,它可能最终解决您的问题!
http://php.net/manual/en/mysqli.error.php
你进入第38行这一事实让我相信你的代码到目前为止实际上是好的。但是,我的第一步是用硬编码值替换INSERT语句中的所有代码,看看是否有效。如果是这样,那意味着你动态进入的任何数据都会弄乱你的查询(根据我的经验,这是最可能的答案)。