简单的形式,试图张贴到数据库


Simple form, trying to post to database

我有一个表单,与jquery验证,和验证码,我有麻烦张贴到数据库。我在这方面是新手,我的代码很丑,我已经尝试过两次了。两个失败。我认为我混淆了面向对象和过程风格。

1代码

<?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,有更好的东西吗?

是面向对象还是过程式风格只是一种选择?一种风格在某些方面是否更好?

我主要是复制例子来学习和得到这个东西的工作,我被困在这里。

您的第一个示例看起来非常好。不要被错误信息吓倒——它们是用来提供帮助的。"致命错误:调用未定义的方法mysqli::error() Line 38"意味着在第38行,您试图做一些根据php不存在的函数。

"undefined method mysqli::error()"是你的线索。查看php手册中关于如何使用mysqli的示例1,它可能最终解决您的问题!

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

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