插入不起作用


INSERT not working?

我有一个简单的插入不起作用。你能做一个快速扫描吗?当我处理代码时,它确实说"成功",即"添加了 1 条记录"和"成功创建目录" - 但我在服务器上没有看到新目录,它在数据库中添加了一条记录,但所有字段都是空的?

include("_inc/config.php"); 
$sql = "INSERT INTO members (id, active, namefirst, namelast, email, username, password, birthyear, gender, country, postalcode, education, dimension, referredby, pic_profile, pic_background, origination, customerid) VALUES ('','$_POST[active]','$_POST[namefirst]','$_POST[namelast]','$_POST[email]','$_POST[username]','$_POST[password]','$_POST[birthyear]','$_POST[gender]','$_POST[country]','$_POST[postalcode]','$_POST[education]','$_POST[dimension]','$_POST[referredby]','$_POST[pic_profile]','$_POST[pic_background]','$_POST[origination]','$_POST[customerid]')";
$newdir=$_POST['username'];
if (!mysql_query($sql,$con)){
  die('Error: ' . mysql_error());
  }
echo "1 record added";
echo "<br><br>";
// Build Directories
mkdir("../".$newdir,0777); 
chmod("../".$newdir,0777);
mkdir("../".$newdir."/assets/",0777); 
chmod("../".$newdir."/assets/",0777);
if("../".mkdir($newdir, 0777, true))
{
    echo 'successfully created directory';
} 
else 
{
    die('problem creating directory'); 
} 
mysql_close($con)

形式

<form role="form" name="form" action="mpx_signup_process.php">
                                <div class="form-group">
                                    <label for="nameandemail">Name and Email</label>
                                    <input name="namefirst" class="form-control" placeholder="First Name">
                                </div>
                                <div class="form-group">
                                    <input name="namelast" class="form-control" placeholder="Last Name">
                                </div>
                                <div class="form-group">
                                    <input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                                </div>
                                <div class="form-group"><hr></div>
                                <div class="form-group">
                                    <label for="accountinfo">Account Information</label>
                                    <input name="username" class="form-control" id="exampleInputPassword1" placeholder="User Name">
                                </div>
                                <div class="form-group">
                                    <input name="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                                </div>
                                <div class="form-group">
                                    <input name="password" class="form-control" id="exampleInputPassword1" placeholder="Password Repeat">
                                </div>
                                <hr />
                                <div class="form-group">
                                 <label for="personalinfo">Personal Information</label>
                                 <h3>Country</h3>
                                <select name="country" class="form-control">
                                    <option value="USA">United States</option>
                                    <option value="UMI">United States Minor Outlying Islands</option>
                                    <option value="USA">- - - - - -</option>
                                    <option value="ALA">Åland Islands</option>
                                    <option value="ALB">Albania</option>
                                    <option value="DZA">Algeria</option>
                                    <option value="ASM">American Samoa</option>
                                </select>
                                </div>
                                <div class="form-group">
                                <label for="referredby">Postal Code</label>
                                    <input name="postalcode" class="form-control" id="exampleInputPostalCode" placeholder="Postal Code">
                                </div>
                                <div class="form-group">
                                <label for="gender">Gender</label>
                                <div class="radio">
                                <label>
                                    <input type="radio" name="gender" id="optionsRadios4" value="01">
                                    Male
                                </label>
                                &nbsp;&nbsp;
                                <label>
                                     <input type="radio" name="gender" id="optionsRadios5" value="02">
                                     Female
                                </label>
                                &nbsp;&nbsp;
                                <label>
                                    <input type="radio" name="gender" id="optionsRadios6" value="03">
                                    Other
                                </label>
                                </div>
                                </div>
                                <div class="form-group">
                                <label for="dateofbirth">Date of Birth</label>
                                <select name="birthyear" class="form-control">
                                <?php 
                                   for($i = 2010 ; $i > 1920; $i--){
                                      echo "<option value=".$i.">$i</option>";
                                   }
                                ?>
                                </select>
                                </div>
                                <div class="form-group">
                                <label for="education">Education</label>
                                <select name="education" class="form-control">
                                    <option value="01">Pre High School</option>
                                    <option value="02">High School</option>
                                    <option value="03">Some College</option>
                                    <option value="04">College Graduate</option>
                                    <option value="05">Graduate Degree</option>
                                </select>
                                </div>
                                <div class="form-group">
                                    <label for="referredby">Referred By</label>
                                    <input name="referredby" class="form-control" id="exampleInputreferredby" placeholder="Referred By">
                                </div>

                                <input type="hidden" name="active" value="no">
                                <input type="hidden" name="dimension" value="00">
                                <input type="hidden" name="pic_profile" value="mpx_profilepic.jpg">
                                <input type="hidden" name="pic_background" value="mpx_bsckgroundpic.jpg">
                                <?php
                                    $date = date('l jS 'of F Y h:i:s A');                
                                ?>
                                <input type="hidden" name="origination" value="<?php echo $date; ?>">
                                <button type="submit" class="btn btn-default">Submit</button>
                            </form>

在表单中添加method="POST"

<form role="form" name="form" method="POST" action="mpx_signup_process.php">

因为$_POST["username"]是空的,所以新的目录名称是空的,因此我假设没有创建它的原因。

我还建议添加检查以确定记录是否入。上面的代码将始终返回不正确"1 record added"。使用类似于mysql_rows_affected的东西,但使用mysqli或PDO代替。

if(mysql_affected_rows() > 0)
{
  echo "1 record added";
  echo "<br><br>";
  // Build Directories
  mkdir("../".$newdir,0777); 
  chmod("../".$newdir,0777);
  mkdir("../".$newdir."/assets/",0777); 
  chmod("../".$newdir."/assets/",0777);
}
else
{
  echo "No records added";
}

当然,它将始终"成功创建目录"!

请看,

if("../".mkdir($newdir, 0777, true))

mkdir评估false(失败)时,你会得到

if("../" . false)
// or
if("../false") // maybe
// or just
if("../")      // maybe, I'm unsure. 

而且它是非空字符串,顺便说一句,它总是可以true确保您击中

echo 'successfully created directory';

$sql 您没有正确转义字符串

$sql中的 PHP post 变量如 $_POST[namelast]$_POST[email] 等,而它应该是 $_POST['namelast']$_POST['email'] 等。