【问题标题】:inserting form data to mysql database (possible scope issue)将表单数据插入 mysql 数据库(可能的范围问题)
【发布时间】:2014-06-17 10:00:21
【问题描述】:

好的,所以我相信我遇到了范围问题;但是,我已经尝试了一些我在堆栈上看到的解决方案,这些解决方案与我试图做的事情没有成功相关。

我还尝试将所有三个文件合并为一个,看看是否可行——也没有成功。

当我尝试提交表单数据时,我收到了一些错误,例如:

错误:

警告:mysqli_real_escape_string() 期望参数 1 为 mysqli,给定为空

警告:mysqli_query() 期望参数 1 为 mysqli,给定为空

这里的任何建议将不胜感激

设置:

  1. settings.php(包含各种定义的常量, 包括主机、用户和数据库名称)。

  2. config.php(包含连接函数)。

  3. process.php(处理表单数据)。

所有三个文件都位于包含文件夹中。

settings.php:

 define ('HOST',   'localhost');       //default HOST (change to live server information)
 define ('DB',     'db_name');   //default database (change to live server information)

 //WRITE ACCESS USER
 define ('USER_W', 'uwrite');      //default user name (change to live server information)
 define ('PASS_W', '1234');       //default password (change to live server information)

 //READ ACCESS USER
 define ('USER_R', 'uread');       //default user name (change to live server information)
 define ('PASS_R', '1234');       //default password (change to live server information)

config.php:

 include ('settings.php');
 function connect($userType) {
    if ($userType == 'write') {
        $link = mysqli_connect(HOST, USER_W, PASS_W, DB);
        }
        elseif ($userType == 'read') {
            $link = mysqli_connect(HOST, USER_R, PASS_R, DB);
            }
            if ($link -> connect_error) {
                die('OOPS SOMETHING WENT WRONG!: (' . $link -> connect_errno . ')' . $link -> connect_error);
                }
}

process.php:

 include ('config.php');

 $con = connect('write');
 // Check connection
 if (mysqli_connect_errno())
 {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 } 
 else {
     echo "HELLO WORLD!";
     } //remove after testing

 // escape variables for security
 $item        = mysqli_real_escape_string($con, $_POST['item']);
 $description = mysqli_real_escape_string($con, $_POST['description']);
 $price       = mysqli_real_escape_string($con, $_POST['price']);

 $sql="INSERT INTO `menus` (`item`, `description`, `price`)
 VALUES ('$item', '$description', '$price')";

 if (!mysqli_query($con,$sql))
 {
   die('Error: ' . mysqli_error());
 }
 echo "1 record added";

表格:

  <form id="insertForm" action="./includes/process.php" method="post">
  <fieldset>
  <legend>Add New Item</legend>

    <p class="red form_P_position">* Denotes required field...</p>
    <p><label for="item" class="mainLabels"><span class="red">*&nbsp;</span>Item:</label>
    <input name="item" id="item" class="mainInputs" type="text" /></p>

    <p><label for="description" class="mainLabels">Description:</label>
    <textarea name="description" id="description" class="mainInputs">
    </textarea></p>

    <p><label for="price" class="mainLabels"><span class="red">*&nbsp;</span>Price:</label>
    <input name="price" id="price" class="mainInputs" type="text" /></p>

    <p><button name="submit" id="submit" class="button" type="submit">
    Add Item
    </button></p>

  </fieldset>

  </form>  

【问题讨论】:

  • 你的函数connect 没有返回任何东西。
  • 从你的连接方法返回 $link,也不要在你的查询中使用 $_POST[item] 而是 $item
  • 是的,$_POST 是一个版本的复制和粘贴,我已经注释掉了 escape_string 行以缩小问题范围

标签: php forms mysqli


【解决方案1】:

你没有在 connect 函数中返回 $link,所以 $con 不包含 mysqli 对象。您必须添加 return $link;在你的 connect() 函数的底部,那么它应该可以正常工作。

function connect($userType) {
if ($userType == 'write') {
    $link = mysqli_connect(HOST, USER_W, PASS_W, DB);
}
elseif ($userType == 'read') {
    $link = mysqli_connect(HOST, USER_R, PASS_R, DB);
}
if ($link -> connect_error) {
    die('OOPS SOMETHING WENT WRONG!: (' . $link -> connect_errno . ')' . $link -> connect_error);
}
return $link;
}

【讨论】:

  • 废话,你摇滚.... 不敢相信我已经盯着这个看了 3 个小时的大部分时间,我没有注意到丢失的回报。问题解决了.. 非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 2019-01-12
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多