【问题标题】:Simple form, trying to post to database简单的表格,试图发布到数据库
【发布时间】:2013-05-31 22:00:20
【问题描述】:

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

我主要是复制示例来学习并让这件事发挥作用,我被困在这里了。

【问题讨论】:

    标签: php oop mysqli


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 另外,验证输入!不过,这完全是另一个话题,所以我在这里就说这么多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2012-06-28
    • 1970-01-01
    • 2014-06-05
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多