INSERT无法正常工作?

时间:2015-01-29 03:38:24

标签: php mysql

我有一个不起作用的简单INSERT。你能php / mySQL专家做快速扫描吗?当我处理代码时,它确实说“成功”即“添加了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>

3 个答案:

答案 0 :(得分:0)

确定它总是“成功创建目录”!

请看,

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';

答案 1 :(得分:0)

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

$sql中的php帖子变量为$_POST[namelast]$_POST[email]等,而应为$_POST['namelast']$_POST['email']等。

答案 2 :(得分:0)

在表单中,添加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";
}