简体   繁体   English

试图在MySQL中保存PHP函数的结果

[英]Trying to save result from PHP function in MySQL

I've created a simple customer signup form (signup.html) to capture 3 fields (email, subdomain and plan). 我创建了一个简单的客户注册表单(signup.html)来捕获3个字段(电子邮件,子域和计划)。

I also want to assign them a random password, I've lifted the code to generate this from this SO article ( Generating a random password in php ). 我还想为他们分配一个随机密码,我已经提取了代码来生成这篇SO文章( 在php中生成一个随机密码 )。

My PHP code (insert.php) is saving the form data fine into MySQL, but not the result from the randomPassword function, where it places "()" in the field instead of the randomly generated password I am hoping for. 我的PHP代码(insert.php)将表单数据保存到MySQL中,但不是randomPassword函数的结果,它在字段中放置“()”而不是我希望的随机生成的密码。

I gather I'm not calling the result from the randomPassword() function properly. 我收集我没有正确调用randomPassword()函数的结果。 What am I doing wrong here? 我在这做错了什么?

SIGNUP.HTML SIGNUP.HTML

<form action="insert.php" method="post" class="inline-form">
  <div class="form-group">
    <label for="email">Your email address</label>
    <input type="email" name="email" class="form-control input-lg" id="email" placeholder="Enter email">
  </div><br><br>
          <label>Select your plan</label><br>
  <div class="radio">
     <label>
        <input type="radio" name="plan" id="plan" value="optionA" checked>
        Option A
    </label>
  </div><br>
  <div class="radio">
     <label>
        <input type="radio" name="plan" id="plan" value="optionB">
        Option B
     </label><br><br>
  </div>
  <div class="form-group">
    <label for="subdomain">Pick your subdomain
     </label>
     <input type="text" name ="subdomain" class="form-control input-lg" id="subdomain">
  </div>
  <br><br>
  <button type="submit" class="btn btn-teal" name="Sign Up">Sign me up!</button>
</form>

INSERT.PHP INSERT.PHP

<?php
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

$sql="INSERT INTO accounts (email, plan, subdomain, password)
VALUES
('$_POST[email]','$_POST[plan]','$_POST[subdomain]','$randomPassword()')";

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

mysqli_close($con);
?>

It doesn't look like you assign a variable to contain the password at all. 它看起来不像你指定一个变量来包含密码。 Functions don't just execute on their own. 函数不仅仅是自己执行。 Use the following: 使用以下内容:

$myPass=randomPassword();

$sql="INSERT INTO accounts (email, plan, subdomain, password)
VALUES
('$_POST[email]','$_POST[plan]','$_POST[subdomain]','$myPass')";

A function on it's own is just sitting there WAITING to be executed, but doesn't fire off on it's own. 它自己的一个功能就是坐在那里等待执行,但不会自行启动。 In this case, the function returns a value (the password it makes). 在这种情况下,函数返回一个值(它所做的密码)。 To actually get it, you write code like $myPass=randomPassword(); 要实际获得它,你可以编写类似$myPass=randomPassword();代码$myPass=randomPassword(); which then executes the function and the value is passed into the variable. 然后执行该函数,并将值传递给变量。

As you don't appear to be a veteran, I will expand some more. 由于你似乎不是一个老将,我会扩展一些。 If you aren't sure why to have a function rather than just execute the code in the first place, a function can be used over and over. 如果您不确定为什么要使用函数而不是仅仅执行代码,则可以反复使用函数。 Lets say I did the following: 让我们说我做了以下事情:

$myPass1=randomPassword();
$myPass2=randomPassword();

With that one function I now have two totally different passwords stored in the variables. 有了这个功能,我现在在变量中存储了两个完全不同的密码。 You can do all sorts of other fancy things, but think of a function as a snippet of code that is to be re-used within your code, hopefully on a number of occasions - without the need to have it written many times. 你可以做各种其他花哨的事情,但是把一个函数想象成一段代码片段,可以在你的代码中重复使用,希望在很多场合 - 不需要多次编写代码。

Perhaps this would work 也许这会奏效

$sql="INSERT INTO accounts (email, plan, subdomain, password)
VALUES ('$_POST[email]','$_POST[plan]','$_POST[subdomain]','randomPassword()')";

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

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