使用PHP将数据插入mysql数据库会出错 [英] Inserting data into mysql database using PHP gets an error

查看:110
本文介绍了使用PHP将数据插入mysql数据库会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将HTML表单和PHP脚本中的数据插入MySQL数据库。



我可以从脚本连接到MySQL服务器但是我无法连接到数据库和表。因此我无法插入数据。



从PHP脚本触发的错误。错误是:未选择数据库!但我已正确宣布了这个名字。



我在这里上传了完整解决方案



下面我添加了HTML表单和PHP脚本。



如果可能的话,请你试着解决问题。



我尝试过的事情:



HTML表格



I am trying to insert data from a HTML form and PHP script to a MySQL Database.

I am able to connect to the MySQL server from the script but I cannot connect to the Database and table. So that I cannot insert data.

The error that triggers from the PHP script. The error is : Database is not selected !. But I have declared the name correctly.

Here I have uploaded the full solution .

Below I have added the HTML form and the PHP Script.

Could you please try to solve the issue if possible.

What I have tried:

HTML Form

<html>
<head>
    <title>Registration Form</title>
</head>
<body>
    <br>
    <h2 align="center">Register a new customer</h2>
    <h3 align="center">Fill all fields</h3>
    <form action="insertdata.php" method="post">
        <table align="center">
            <tbody><tr>
                <td>First Name:</td>
                <td><input type="text" id="fname" name="fname" /></td>
            </tr>
            <tr>
                <td>Middle Name:</td>
                <td><input type="text" id="mname" name="mname" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" id="lname" name="lname" /></td>
            </tr>
            <tr>
                <td>NIC / Driving License No:</td>
                <td><input type="text" id="NICNo" name="nicno" /></td>
            </tr>
            <tr>
                <td>Permanent Address Name:</td>
                <td><input type="text" id="paddress" name="paddress" /></td>
            </tr>
                <tr>
                <td>E-Mail Address:</td>
                <td><input type="text" id="email" name="email" /></td>
            </tr>
                <tr>
                <td>Telephone:</td>
                <td><input type="text" id="telno" name="telno" /></td>
            </tr>
            <tr>
                <td>Mobile No:</td>
                <td><input type="text" id="mobileno" name="mobileno" /></td>
            </tr>
                <tr>
                <td>Date of Registration:</td>
                <td><input type="text" id="regdate" name="regdate" /></td>
            </tr>
                <tr><td></td></tr>
                <tr><td></td></tr>
                <tr><td></td></tr>
                <tr><td></td></tr>
                <tr><td></td></tr>
                <tr><td></td></tr>
            <tr>
            <td align="center"><input type="submit" />     <input type="reset" /></td>

            </tr>
        </tbody></table>
    </form>

  </body>





InsertData.php





InsertData.php

<?php

$dbserver = 'localhost';
$username = 'root';
$password = '';

$dbname = 'car_rental';

$con = mysqli_connect($dbserver, $username, $password);

if(!$con)
{
    echo "Not connected to server !";
   
    
}else{
    echo "Server connected !";
 
}

if(!mysqli_select_db($con, $dbname));
{
    echo "Database not selected !";
   
}

$fname = $_POST["fname"];
$mname = $_POST["mname"];
$lname = $_POST["lname"];
$nicno = $_POST["nicno"];
$paddress = $_POST["paddress"];
$email = $_POST["email"];
$telno = $_POST["telno"];
$mobileno = $_POST["mobileno"];
$regdate = $_POST["regdate"];

$sql = "INSERT INTO new_customers (FirstName, MiddleName, LastName, NICNo, PermanentAddress, EmailAddress, Telephone, Mobile, RegDate) VALUES ('$fname','$mname','$lname','$nicno','$paddress','$email','$telno','$mobileno','$regdate')";

if (!mysqli_query($con,$sql))
{
    echo "Data not inserted !";
   
}
else
{
    echo "Data inserted !";
}

header("refresh:300; url=form.php");

?>





谢谢!



Thanks!

推荐答案

dbserver = ' localhost' ;
dbserver = 'localhost';


用户名 = ' ;
username = 'root';


密码 = ' ';

password = '';


这篇关于使用PHP将数据插入mysql数据库会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆