简体   繁体   English

初学者PHP连接-错误排查

[英]Beginner PHP connection -error troubleshoot

I'm just learning PHP and SQL and need some help. 我只是在学习PHP和SQL,需要一些帮助。 I'm sure it's a very easy fix if you know what you're doing, but I don't yet. 如果您知道自己在做什么,我敢肯定这是一个非常简单的解决方法,但我还没有。 There are several similar questions on here already but the weren't able to help me. 这里已经有几个类似的问题,但是无法帮助我。

I have a splash page I'm creating with 5 fields. 我有一个使用5个字段创建的启动页面。 I'd like for these fields to be sent to a SQL database on my server after the user hits submit. 我希望在用户点击提交后将这些字段发送到服务器上的SQL数据库。 Here's the pertinent code: 以下是相关代码:

<form action="insert.php" method="post">
First and last name: <input type="text" name="Name"><br>
Email: <input type="text" name="Email"><br>
Debt owed:           <select name="Debt">
  <option>Under $5,000</option>
  <option>$5001-$15,000</option>
  <option>$15,001-$50,000</option>
  <option>$50,000+</option>
</select> <br>
Phone Number ex. (555) 123-4567: <input type"text" name="Number"><br>
State: <select name="State"> 
<option value="" selected="selected">Select a State</option> 
<option value="AL">Alabama</option> 
<option value="WY">Wyoming</option>
</select> (Some opportunities are based on your state)
<input type="submit">
</form>

So it calls insert.php. 因此它调用insert.php。 This is what that file looks like: 该文件如下所示:

<?php
    $con=mysqli_connect("DebtLeads2.db.9463978.hostedresource.com","DebtLeads2", "XXXXXXXXXXXX");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();

    }

    $sql="INSERT INTO Persons (Name, Email, Debt, Number, State)
    VALUES
    ('$_POST[Name]','$_POST[Email]','$_POST[Debt]','$_POST[Number]','$_POST[State]')";


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

    mysqli_close($con);
    ?> 

I have successfully created the "Persons" table in SQL with 5 fields. 我已经用5个字段在SQL中成功创建了“人员”表。 It is connecting to my database. 它正在连接到我的数据库。 However, I get the following error and I just don't know what to do. 但是,出现以下错误,我只是不知道该怎么办。

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/content/78/9463978/html/MM/gifts/MonetaryMind/insert.php on line 16 Error: 警告:mysqli_error()恰好需要1个参数,/ home / content / 78/9463978 / html / MM / gifts / MonetaryMind / insert.php中第16行给出的0错误:

Your guidance would be much appreciated, I'm pulling my hair out over here. 您的指导将不胜感激,我正在这里拉头发。

mysqli_error()

should be: 应该:

mysqli_error($con);

Also, one of the main advantages of mysqli over mysql is that it has prepared statements, which prevent most SQL-injection problems. 另外,与mysql相比,mysqli的主要优点之一是它具有准备好的语句,可以防止大多数SQL注入问题。 You should use them instead of interpolating strings into your queries. 您应该使用它们,而不是在查询中插入字符串。

The answer is simple. 答案很简单。 There is nothing to pull your hair for. 没有什么可以拉扯你的头发的。

An error message and a manual page are your friends. 错误消息和手册页是您的朋友。
You have a message already, says something wrong with the way you are calling mysqli_error() . 您已经有一条消息,说您调用mysqli_error()的方式有问题。 Well, time for the manual page. 好了,时间到了手册页。

Open your browser, type php.net/ and then the function name. 打开浏览器,输入php.net/ ,然后输入函数名称。 A few keystrokes. 几次按键。

Now a page http://php.net/mysqli_error opened. 现在打开了一个页面http://php.net/mysqli_error
There you can read everything you need to know on this function call: 在那里,您可以阅读此函数调用所需的所有知识:

string mysqli_error ( mysqli $link ) 字符串mysqli_error(mysqli $ link)

link Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init() 仅限链接过程样式:mysqli_connect()或mysqli_init()返回的链接标识符

Quite simple, eh? 很简单,是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM