Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未输入数据时未定义索引_Php - Fatal编程技术网

Php 未输入数据时未定义索引

Php 未输入数据时未定义索引,php,Php,我正在做一个v2的注册 我在各种输入类型中添加了一些新的输入字段,我在v1中使用的输入字段现在可以正常工作,但新的输入字段不能正常工作。我注意到它只像特殊的输入字段(复选框、选择菜单、单选按钮等等)。我正在测试,当没有输入任何内容时,它只会给我这个未定义的索引错误(当我试图定义它时)注意:不工作的输入不是必填字段 无论如何,下面是PHP代码: // Initializing variables $username = ""; $email = ""; $errors = array();

我正在做一个v2的注册

我在各种输入类型中添加了一些新的输入字段,我在v1中使用的输入字段现在可以正常工作,但新的输入字段不能正常工作。我注意到它只像特殊的输入字段(复选框、选择菜单、单选按钮等等)。我正在测试,当没有输入任何内容时,它只会给我这个未定义的索引错误(当我试图定义它时)注意:不工作的输入不是必填字段

无论如何,下面是PHP代码:

// Initializing variables
$username = "";
$email = "";

$errors = array(); 

// Connect to the database

$conn = mysqli_connect($database['host'], $database['user'],  $database['password'], $database['db'], $database['port']);

$email_show = $_POST['emailshow'];

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // Receive all input values from the form
  $username = mysqli_real_escape_string($conn, $_POST['username']);
  $email = mysqli_real_escape_string($conn, $_POST['email']);
  $email_show = mysqli_real_escape_string($conn, $_POST['emailshow']);
  $password_1 = mysqli_real_escape_string($conn, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($conn, $_POST['password_2']);
  $fname = mysqli_real_escape_string($conn, $_POST['fname']);
  $fname_show = mysqli_real_escape_string($conn, $_POST['fnameshow']);
  $lname = mysqli_real_escape_string($conn, $_POST['lname']);
  $lname_show = mysqli_real_escape_string($conn, $_POST['lnameshow']);
  $sex = mysqli_real_escape_string ($conn, $_POST['sex']);
  $bday = mysqli_real_escape_string($conn, $_POST['bday']);
  $country = mysqli_real_escape_string($conn, $_POST['country']);
  $private_account = mysqli_real_escape_string($conn, $_POST['privateacc']);
  $profile_style = mysqli_real_escape_string($conn, $_POST['profilestyle']);

  // Form validation: ensure that the form is correctly filled ...
  // By adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if (empty($fname)) { array_push($errors, "Firstname is required"); }
  if (empty($lname)) { array_push($errors, "Lastname is required"); }
  if (empty($sex)) { array_push($errors, "What is your gender?"); }
  if (empty($bday)) { array_push($errors, "When is your birthday?"); }
  if (empty($_POST['country'])) { array_push($errors, "Test"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }
HTML输入:

            <div class="box-title"><h1>Sign up</h1></div>
            <div class="required-fields"><em>- Required fields are marked with *</em></div>
              <div class="account-wrapper">
                  <div class="account-title">YOUR ACCOUNT</div>
                    <form method="post" class="accountform" action="/sign-up">
                       <input type="text" placeholder="Username*" name="username" onfocus="showDesc1()">
                       <div id="desc1">How others will see you <i id="close" onclick="closeDesc1()" class="fas fa-times-circle"></i></div>
                       <input type="email" placeholder="Email*" name="email" onfocus="showDesc2()">
                       <div id="desc2">We need this for important use <i id="close" onclick="closeDesc2()" class="fas fa-times-circle"></i><br><a style="color: blue;"> Read more (soon)</a></div>
                       <div id="emailshow"><input type="checkbox" id="emailshow-input" name="emailshow" value="showemail"><label style="text-decoration: underline;" data-tooltip="When this option is enabled, other users will be able to see your email on your profile." for="emailshow-input">Show on profile</label></div>
                       <input id="passwordfield" type="password" placeholder="Password*" name="password_1">
                       <div onclick="passwordVisb()" class="visb-innerhtml"><i id="visbcon" class="fas fa-eye"></i><label style="cursor: pointer;" id="visb-innerhtml"> Show password</label></div>
                       <input type="password" placeholder="Confirm password*" name="password_2" onfocus="showDesc3()">
                       <div id="desc3">Remember the entered password? <i id="close" onclick="closeDesc3()" class="fas fa-times-circle"></i></div>
                       </div>
                    </div>
                       <div class="details-wrapper">
                         <div class="details-title">YOUR DETAILS</div>
                           <div class="details-fields1">
                             <input type="text" placeholder="Firstname*" name="fname" onfocus="showDesc4()">
                             <div id="desc4">Your firstname <i id="close" onclick="closeDesc4()" class="fas fa-times-circle"></i></div>
                             <div id="fnameshow"><input type="checkbox" id="fnameshow-input" name="fnameshow" value="1"><label style="text-decoration: underline;" data-tooltip="When this option is enabled, other users will be able to see your firstname on your profile." for="fnameshow-input">Show on profile</label></div>
                             <input type="text" placeholder="Lastname*" name="lname" onfocus="showDesc5()">
                             <div id="desc5">Your lastname  <i id="close" onclick="closeDesc5()" class="fas fa-times-circle"></i></div>
                             <div id="lnameshow"><input type="checkbox" id="lnameshow-input" name="lnameshow" value="1"><label style="text-decoration: underline;" data-tooltip="When this option is enabled, other users will be able to see your lastname on your profile." for="lnameshow-input">Show on profile</label></div>
                           <div class="gendersec">
                             <input id="genm" type="radio" name="sex" value="male">
                             <label for="genm">Male</label>
                             <input id="genf" type="radio" name="sex" required value="female">
                             <label for="genf">Female</label>
                             <input id="genc" type="radio" name="sex" value="custom">
                             <label for="genc" style="text-decoration: underline;" data-tooltip="Select this option if you do not want to tell us what your gender is or if you do have another gender not listed here." data-tooltip-location="top">Custom</label>
                           </div>
                             <input type="text" onkeypress="return false;" name="bday" autocomplete="off" placeholder="Birthday*" id="birthdaypicker">
                         </div>
                          <div class="details-fields2">
                            <?php include_once 'C:/xampp2/htdocs/includes/lang-select.php' ?>
                            <!-- <div id="tags-input">
                              <span>Socialhub</span>
                               <input type="text" disabled value="" placeholder="Interests (coming soon)">
                            </div> !-->
                            <p style="color: orange;">More options coming soon!</p>
                          </div>
                          </div>
                         <div class="settings-wrapper">
                           <div class="settings-title">QUICK SETTINGS</div>
                             <div class="settings-fields1">
                               <input type="checkbox" id="privateacc-input" name="privateacc" value="privateacctrue"><label for="privateacc-input" style="text-decoration: underline;" data-tooltip="If you enable this option, then the content of your account will be hidden for other users (they can still visit your account but they can't see the content). From your profile picture to your latest post will be hidden.">Private account</label>
                               <select class="profilestyle" name="profilestyle">
                                 <option selected disabled>Select a style for your profile...</option>
                                 <option value="default">Default (orange & green)</option>
                                 <option value="red">Red</option>
                                 <option value="black">Black</option>
                                 <option disabled>These are coming soon for sure, stay tuned!</option>
                                 <option disabled>Yellow</option>
                                 <option disabled>Blue</option>
                                 <option disabled>Pink</option>
                                 <option disabled>...</option>
                                </select>
                               <p style="margin-top: 50px; color: orange;">More settings coming soon!</p>
                            </div>
                         </div>
              <div class="bottom-wrapper">
                <div class="bottom-topframe">
                   <a href="/" class="btn-back"><i class="fas fa-arrow-left"></i> Nevermind!</a>
                   <div class="agree-wrapper">
                     <input type="checkbox" id="agreetos-input" name="agreetos" value="tosagreed" onchange="document.getElementById('btnsubmit_reg').disabled = !this.checked;"><label for="agreetos-input">I agree with the Terms of Service (coming soon)</label>
                     <input type="checkbox" id="verifyage-input" name="verifyage" value="ageverified"><label for="verifyage-input">I am 13 years or older</label>
                    </div>
                    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response">
                    <button type="submit"  disabled name="reg_user" id="btnsubmit_reg" class="btnsubmit_reg">Sign up</button>
                </div>
注册
-必填字段标记为*
你的帐户
别人会怎么看你
我们需要这个重要的用途
我同意服务条款(即将推出) 我13岁或更大 注册

已经谢谢你了。(很抱歉搜索,大表单)

我建议您,为了解决此问题并提高安全性,因为此代码可以在Mysql注入中轻松使用,您必须从POST中转义所有值,然后您必须检查它是否为空并为其提供默认值

这会对你有所帮助,就像@Moayad.AlMoghrabi说,如果是本地的,你真的应该使用更好的安全性。始终学习编写安全代码

$email = mysqli_real_escape_string($conn, $_POST['email']);

$email = isset($_POST['email']) ? mysqli_real_escape_string($conn, $_POST['email']) : '';

通过像这样检查post值,变量将始终具有值或空字符串

你能给我一个例子代码或一个有用的链接来做这件事吗?(SQL注入在很大程度上并不重要,它是一个本地项目,而且永远都是。但是np也希望了解更安全的方法)转义如何解决未定义索引的问题?此外,请分享一些例子,以供参考that@NicoHaase,我上面提到过要有更多的安全性!!您可以像这样转义值
$username=mysqli\u real\u escape\u string($conn,$\u POST['username')已编辑为您的代码。若要修复未定义的索引,我认为应使用复选框输入类型,因此必须检查
if(empty($value)){/*do stuff*/}
,就像为设置默认值一样it@Moayad.AlMoghrabi我确实已经逃走了,所以我想我的密码在第一条领带上是安全的。我试着用空的($value)来回答你的问题,但没有解决问题。它可以通过在数组上推送一个错误来修复,但因为它不是必填字段,所以我不能这样做。@Sow。您不必推送错误,您必须为sex字段设置一个值(例如,默认情况下(如果未选中Radio和Checkbox输入,则不会在POST数组中推送它们),并检查您正在接收的任何字段,然后您希望将其值插入数据库,如下所示:
$sex='male'
如果(!empty($\u POST['sex']){$sex=mysqli\u real\u escape\u string($conn,$\u POST['sex']);}
这能回答你的问题吗?试图实现它,但没有成功。也许我实施错了?它是这样的:``//初始化变量$username=“”$电子邮件=”$电子邮件_show=“”;“`”和``$email\u show=isset($\u POST['emailshow'])?mysqli_real_escape_string($conn,$_POST['emailshow']):''$email\u show=mysqli\u real\u escape\u string($conn,$\u POST['emailshow']);`(您设置的电子邮件字段是否正常工作或是否不安全?)如果不起作用,则会出现未定义的错误,因为您试图获取不存在的post数组的值。我编写的检查需要对您试图从post arrayMy示例中获取的每个变量进行检查。我的示例仅显示如何防止未定义的post变量,为了安全起见,您需要进行其他检查。比如filter_var来检查给定的电子邮件是否真的是电子邮件地址,还有一些检查来预防sql注入和其他安全风险。试着学习准备好的语句它易于使用并且有更好的默认安全性是否仍然需要像我以前那样初始化变量(检查问题的顶部)@Baracuda078@Sow你需要它们做什么?我看不出在你的post请求被使用之前你会使用它们