1

我有一个带有 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 或程序风格只是一种选择吗?一种风格在某些事情上更好吗?

我主要是复制示例来学习并使这件事起作用,我被困在这里。

4

1 回答 1

1

你的第一个例子看起来不错。不要被错误信息吓倒——它们会提供帮助。“致命错误:调用未定义的方法 mysqli::error() 第 38 行”表示在第 38 行你试图做一些事情,根据 php,这是一个不存在的函数。

“未定义的方法 mysqli::error()”是你的线索。看看php手册中关于如何使用mysqli的示例1,它最终可能会解决你的问题!

http://php.net/manual/en/mysqli.error.php

您到达第 38 行的事实使我相信您的代码到那时为止实际上是好的。但是,我的第一步是用硬编码值替换 INSERT 语句中的所有代码,看看是否可行。如果是这样,这意味着您动态输入的任何数据都会弄乱您的查询(根据我的经验,这是最有可能的答案)。

于 2013-05-31T22:09:30.870 回答