使用Php和Mysql插入数据时出错

时间:2015-07-21 06:43:10

标签: php mysql

我有一个包含文件上传和用户名退出检查条件的表单。 我面临的是数据没有在mysql db中插入。文件已成功保存在给定路径中。亲切地帮助我这个已经浪费了2天,我自己尝试了很多。

form.php的

<table style="text-align:right">
    <form id="add" method="POST" action="action.php" enctype="multipart/form-data">
        <tr>
            <h4 class='bg-info'>
                <br/> &nbsp &nbsp &nbsp &nbsp Become a Member of jobportal and find the right job. Create your Profile now, Free!<br/><br/>
            </h4>
        </tr>
        <tr>
            <td></td>
            <td> * Mandatory Fields  </td>
        </tr>
        <tr>
            <div class="col-md-1"></div>
            <td>Enter Your Email-ID: *</td>
            <td><input class="form-control input-sm" placeholder="Email ID" type="textfield" name="email"required></td>
        </tr>
        <tr>
            <td>Choose password *</td>
            <td><input class="form-control input-sm" placeholder="Enter Your Password" type="password" name="password"required/></td>
        </tr>
            <td>Re-Enter Your password *</td>
            <td><input class="form-control input-sm" placeholder="Enter Your Password" type="password" name="repassword"required/></td>
        </tr>
        <tr>
            <td> Please Enter Your Full Name:</td>
            <td> <input class="form-control input-sm" placeholder="Enter Full Name" type="textfield" name="name"required></td>
        </tr>
        <tr>
            <td>Your Current Location: *<td>
            <select class="form-control input-sm" required name="location">
                <option value='' disabled selected style='display:none;'>Select location *</option>
                <option>Andhra Pradesh</option>
                <option>Arunachal Pradesh</option>
                <option>Assam</option>
                <option>Bihar</option>
                <option>Chhattisgarh</option>
                <option>Goa</option>
                <option>Gujarat</option>
                <option>Haryana</option>
                <option>Himachal Pradesh</option>
                <option>Jammu and Kashmir</option>
                <option>Jharkhand</option>
                <option>Karnataka</option>
                <option>Kerala</option>
                <option>Madhya Pradesh</option>
                <option>Maharashtra</option>
                <option>Maharashtra</option>
                <option>Manipur</option>
                <option>Meghalaya</option>
                <option>Mizoram</option>
                <option>Nagaland</option>
                <option>Odisha</option>
                <option>Punjab</option>
                <option>Rajasthan</option>
                <option>Sikkim</option>
                <option>Tamil Nadu</option>
                <option>Telangana</option>
                <option>Tripura</option>
                <option>Uttar Pradesh</option>
                <option>Uttarakhand</option>
                <option>West Bengal</option>
            </select></td>
            </td>
        </tr>
        <tr>
            <td>Enter Your Mobile Number: *</td>
            <td><input class="form-control input-sm" placeholder="mobile number" type="textfield" name="mobilenumber" required/></td>
        </tr>
        <tr>
            <td>Experience:</td>
            <td>
                <select class="form-control input-sm" required name="experience">
                    <option value='' disabled selected style='display:none;'>Select Experience</option>
                    <option>Fresher</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Key Skill: *</td>
            <td>
                <input class="form-control input-sm" placeholder="Enter Your Skill" type="textfield" name="keyskill"/>
            </td>
        </tr>
        <tr>
            <td>Please Select your PG Degree</td>
            <td>
                <select class="form-control input-sm" required name="degree">
                    <option value='' disabled selected style='display:none;'>Select Degree</option>
                    <option>B.sc</option>
                    <option>B.E</option>
                    <option>B.Com</option>
                    <option>others</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Please Select Higher Studies:</td>
            <td>
                <select class="form-control input-sm" required name="hsc">
                    <option value='' disabled selected style='display:none;'>Select Higher Studies</option>
                    <option>HSC</option>
                    <option>Diploma</option>
                    <option>ITI</option>
                    <option>others</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Please Select your Gender: *</td>
            <td>
                <select class="form-control input-sm" required name="gender">
                    <option value='' disabled selected style='display:none;'>Select</option>
                    <option>Male</option>
                    <option>Female</option>
                    <option>others</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Upload your Resume :</td>
            <td><input type="file" name="filep"></td>
        </tr>
        <tr>
            <td> </td>
            <td>by clicking register u accepting our terms and condtions. click here !</td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" name="add" class="btn btn-info btn-sm" id="add" value="Register With JobPortal">
            </td>
        </tr>
    </form>
</table>

action.php的

$con = mysqli_connect('localhost','root','');
if (!$con) {
    die('Could not connect: ' . mysql_error());
} else {
    echo 'connected';
}
if (isset($_POST['add']) ) {
    if (!get_magic_quotes_gpc() ) {
        $email = addslashes ($_POST['email']);
    } else {
        $email = $_POST['email'];
    }
    $email = $_POST['email'];
    $password = md5 ($_POST['password']);
    $name = $_POST['name'];
    $location = $_POST['location'];
    $mobilenumber = $_POST['mobilenumber'];
    $experience = $_POST['experience'];
    $keyskill  = $_POST['keyskill'];
    $degree = $_POST['degree'];
    $hsc = $_POST['hsc'];
    $gender = $_POST['gender'];
    $resume = $_FILES['filep']['name'];
    $folder = "C:/wamp/www/userlogin/pic/";
    $name="SELECT emailid FROM userregistration WHERE emailid='$email'";
    mysqli_select_db($con, 'login');
    $result = mysqli_query($con, $name);
    if (mysqli_num_rows($result)!=0) {
       echo "Username already exists";
    } else {
        echo"data entered done";
    }

    if (move_uploaded_file($_FILES["filep"]["tmp_name"], $folder . $_FILES["filep"]["name"])) {
        echo "images moved sus";
    } else {
        echo "not done";
    }

    echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

    $sql = "INSERT INTO userregistration "
        . "(email, password, name, location, mobilenumber, experience, keyskill, degree, hsc, gender, resume)"
        . "VALUES('$email', '$password', '$name', '$location', '$mobilenumber', '$experience', '$keyskill',
        '$degree', '$hsc', '$gender', '$resume')";
    mysqli_select_db($con, 'login');
    $retval = mysqli_query($con, $sql);
    if (!$retval) {
        die('Could not enter data: ' . mysql_error());
    }
    echo "Entered data successfully\n";
    echo'<a href="index.php"> insert more data </a>';
    mysqli_close($con);
}

我真正需要的是:我想将带有文件网址的表单数据上传到数据库中,并且需要检查已经退出的电子邮件ID或名称。

我只会在$sql = "insert into"部分得到错误,而不是正常工作。

提前致谢。

2 个答案:

答案 0 :(得分:0)

错误

  1. 缺少数据库名称

    mysqli_connect("localhost","root","","login");
    
  2. $sql查询

  3. 中的错误

    所以最后很好 - 来自代码是

    <?php
        $con= mysqli_connect("localhost","root","","login");;//missing database
    
        if (! $con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else{
            echo 'connected';
        }
        if(isset($_POST['add']))
        {
            if(! get_magic_quotes_gpc() )
            {
                $email = addslashes ($_POST['email']);
            }
            else
            {
                $email = $_POST['email'];
            }
            $email  = $_POST['email'];
            $password  = md5 ($_POST['password']);
            $name    = $_POST['name'];
            $location = $_POST['location'];
            $mobilenumber = $_POST['mobilenumber'];
            $experience  = $_POST['experience'];
            $keyskill    = $_POST['keyskill'];
            $degree      = $_POST['degree'];
            $hsc     = $_POST['hsc'];
            $gender  = $_POST['gender'];
            $resume = $_FILES['filep']['name'];
            $folder = "C:/wamp/www/userlogin/pic/";
    
            $query001="SELECT emailid FROM userregistration WHERE emailid='$email'";
    
            $result = mysqli_query($con, $query001);
            if(mysqli_num_rows($result)!=0){
    
                echo "Username already exists";
            }
            else
            {
                echo"data entered done";
                if (move_uploaded_file($_FILES["filep"]["tmp_name"], $folder . $_FILES["filep"]["name"]))
                {
                    echo "images moved sus";
                }
                else
                {
                    echo "not done";
                }
    
                echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
    
                $sql = "INSERT INTO userregistration (email, password, name, location, mobilenumber, experience, keyskill,
        degree, hsc, gender, resume) VALUES('$email','$password','$name','$location','$mobilenumber','$experience','$keyskill','$degree','$hsc','$gender','$resume')";
                $retval = mysqli_query($con, $sql);
                if(!$retval )
                {
                    die('Could not enter data: ' . mysql_error());
                }
                else
                {
                    echo "Entered data successfully\n";
                    echo'<a href="index.php"> insert more data </a>';
                    mysqli_close($con);
                }
            }
    
        }
    
    ?>
    

    并注意MySQL Injection

    只需使用mysqli_real_escape_string()

    即可

    示例

    $name   = mysqli_real_escape_string($_POST['name']);
    
      

    提示(评论)
      您在代码中声明了两次$ name - 重命名$ name select语句。 ($name = $_POST['name'];$name="SELECT emailid FROM userregistration WHERE emailid='$email'";) - Jesse C

答案 1 :(得分:0)

之前回显你的sql查询 class MyObserver: NSObject { override init() { super.init() // app listeners NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "SwitchedApp:", name: NSWorkspaceDidActivateApplicationNotification, object: nil) } func SwitchedApp(notification: NSNotification!) { print(notification) } } let observer = MyObserver() // simply to keep the command line tool alive - as a daemon process NSRunLoop.mainRunLoop().run() 并在你的Mysql phpmyadmin中执行...

我猜你的查询形成存在一些问题,单词之间的间距或其他问题。