如何插入mysql数据库,html表单?

时间:2017-01-29 14:06:48

标签: php mysql

我尝试运行此脚本,但它回显了一个无法插入表中的错误。请帮我解决这个问题。

这是我的剧本:

<?php
$con = mysqli_connect('localhost','*******','*******');
$db= mysqli_select_db($con,'registration_form');

//check connection
if (!$con==TRUE)
    die("Connection failed: " . mysqli_connect_error());

if(isset($_POST['register_btn'])){

    $fullname = $_POST['fname'];
    $username = $_POST['user'];
    $email = $_POST['email'];
    $password = $_POST['passcode'];
    $cpass = $_POST['cpasscode'];
    $account = $_POST['accountname'];
    $accountnum = $_POST['accountno'];
    $name_of_bank = $_POST['bankname'];
    $phone_no = $_POST['phoneno'];                                  

    //attempt insert query execution

    $sql = "INSERT INTO sitemembers (fullname, userName, email, passcode, cpasscode, accountName, accountNo, nameOfbank, phoneNo) VALUES ('$fullname','$username','$email','$password','$cpass','$account','$accountnum','$name_of_bank','$phone_no')";


    if(mysql_query($sql)){
        echo "account created successfully."; 

    }
    else{
        echo "Error: Could not execute $sql. " . mysqli_error($con);
    }

    // Close connection
    mysqli_close($con);                 

}
?>

1 个答案:

答案 0 :(得分:0)

A)您正在使用mysqli连接并mysql来执行查询。尝试使用 删除前两行并放入 -

     $con=mysqli_connect('localhost','root','','registration_form');

在执行查询时使用它:  mysqli_query($con, $query)

B)您的代码安全性很差。使用此:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

C)向我们展示完整的错误,我们不是那些做出假设并使其正确的魔术师。