简体   繁体   English

将数据插入MySQL数据库

[英]Inserting data into MySQL database

I'm struggling with inserting data from the form to database. 我正在努力将数据从表单插入数据库。 I managed to establish the connection (at least not getting any errors), but when comes to inserting values I facing error, I'm not sure if rows/columns of the table should be in ' ' or not, I've seen some examples both with quotation marks and without, and also if variables should have those as well. 我设法建立了连接(至少没有收到任何错误),但是在插入值时遇到错误,我不确定表的行/列是否应位于'',我已经看到一些带有引号和不带引号的示例,以及变量是否也应带有引号的示例。

connecting to the database (connect.php): 连接到数据库(connect.php):

<?php
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","Bookshop");

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if(!$connection){
        die("error: unable to connect to database " . mysql_error());
    }
    $select_db = mysql_select_db(DB_NAME, $connection);
    if(!$select_db){
        die("error: unable to connect to database " . mysql_error());
    }
?>   

including connections: 包括连接:

<?php
    include ("connect.php"); // connects to database
?>

and inserting data from the form: 并从表单中插入数据:

$query = "INSERT INTO customer 
                (CUST_ID, CUST_NAME, CUST_SURNAME, HOUSE_NO, STREET, POSTCODE, PHONE_NO, EMAIL, OGIN, PASSWORD)
                VALUES 
                ('10101', '$forename', '$surname', '$address1', '$address2', '$postcode', 
                '$phone_no', '$email', '$login', '$password')"; 
mysql_query($query, $connection);

    if(!mysql_query($query, $connection)){
        echo "Error!!!!";
    }

mysql_close($connection);   

It seems you are a newbie.. Start with mysqli or pdo extensions. 看来您是新手。.从mysqli或pdo扩展名开始。 Visit W3schools.com for a detailed explanantion with examples. 请访问W3schools.com ,以获取有关示例的详细说明。 Below is an example of how to use mysqli to connect and insert a row in your database 以下是如何使用mysqli连接并在数据库中插入行的示例

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

And For your query.. 并为您查询。

I'm not sure if rows/columns of the table should be in ' ' or not, I've seen some examples both with quotation marks and without, and also if variables should have those as well. 我不确定该表的行/列是否应位于'内,我已经看到了一些带引号和不带引号的示例,以及变量是否也应带有引号。

As far as insert queries are concerned, 1).Wrap up column names with ` back ticks. 就插入查询而言,1)。用反斜杠结束列名。 2).Wrap your Variables with single quote nothing wrong in that 2)。用单引号引起来的变量没有错

For more understanding about single and double quote usages, 要进一步了解单引号和双引号的用法,

Single quotes does not look for variables while double quote does 单引号不查找变量,而双引号则查找变量

Case 1 : 情况1 :

    $value = 10;
    $sql = 'SELECT * FROM `table_name` WHERE `column_name` = $value';
    echo $sql;

output is 输出是

SELECT * FROM `table_name` WHERE `column_name` = $value

Here if you see single quote does not look for a variable within it. 在这里,如果看到单引号,则不会在其中查找变量。 Whatever there is inside single quotes, it is considered as a string and returned as such. 无论单引号内有什么,都将其视为字符串并按原样返回。

Case 2: 情况2:

    $value = 10;
    $sql = "SELECT * FROM `table_name` WHERE `column_name` = $value";
    echo $sql;

Output is 输出是

 SELECT * FROM `table_name` WHERE `column_name` = 10

Here Since the query is inside double quotes, That variable is read. 由于查询位于双引号内,因此将读取该变量。 but considered as int. 但被视为int。

Case 3: 情况3:

    $value = 10;
    $sql = "SELECT * FROM `table_name` WHERE `column_name` = '$value'";
    echo $sql; 

Output is 输出是

SELECT * FROM `table_name` WHERE `column_name` = '10'

Here Since the query is inside double quotes, That variable is read. 由于查询位于双引号内,因此将读取该变量。 but considered as string as it is encapsulated with single quotes. 但视为字符串,因为它用单引号引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM