如何将数据插入PHP中的列到MariaDB?

如何将数据插入PHP中的列到MariaDB?

问题描述:

I am familiar with MySql databases but there is this new database called MariaDB. I try to insert data there from PHP code and I can't, so can you help me insert the data? My PHP on the server is 5.4.32 and my MySQL version is 10.0.20-MariaDB-cll-live.

This is the code that I am using to try to insert data into MariaDB.

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("database", $connection); // Selecting Database from Server

if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
    $name = $_POST['name'];
    $email = $_POST['email'];
    if($name !=''||$email !=''){
       //Insert Query of SQL
       $query = mysql_query("insert into VIP Membership(Name, Email) values ('$name', '$email')");
       echo "<br/><br/><span>Data Inserted successfully...!!</span>";
    }
    else{
       echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}

mysql_close($connection); // Closing Connection with Server

EDIT: I used the mysqli code now but it throws me an error 500.

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$email = $_POST['email'];
if($name !=''||$email !=''){

$sql = "INSERT INTO 'VIP Membership' (Name, Email)
VALUES ('$name', '$email')";

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

mysqli_close($conn);

我熟悉MySql数据库,但有一个名为MariaDB的新数据库。 我尝试从PHP代码插入数据而我不能,所以你可以帮我插入数据吗? 我在服务器上的PHP是5.4.32,我的MySQL版本是10.0.20-MariaDB-cll-live。 p>

这是我用来尝试将数据插入MariaDB的代码 。 p>

  $ connection = mysql_connect(“localhost”,“root”,“”);  //与服务器建立连接
 $ db = mysql_select_db(“database”,$ connection);  //从服务器中选择数据库
 
if(isset($ _ POST ['submit'])){//获取以URL格式传递的表单变量
 $ name = $ _POST ['name']; 
 $  email = $ _POST ['email']; 
 if($ name!=''|| $ email!=''){
 //插入SQL查询
 $ query = mysql_query(“插入VIP会员 (姓名,电子邮件)值('$ name','$ email')“); 
 echo”&lt; br /&gt;&lt; br /&gt;&lt; span&gt;数据已成功插入... !!&lt;  / span&gt;“; 
} 
其他{
 echo”&lt; p&gt;插入失败&lt; br /&gt;某些字段为空.... !!&lt; / p&gt;“; 
} 
  } 
 
mysql_close($连接);  //关闭与服务器的连接
  code>  pre> 
 
 

编辑:我现在使用了mysqli代码,但它引发了我500错误。 p>

  &lt;?php 
 $ servername =“localhost”; 
 $ username =“username”; 
 $ password =“”; 
 $ dbname =“db_table”; 
 
 //创建连接 
 $ conn = mysqli_connect($ servername,$ username,$ password,$ dbname); 
 //检查连接
if(!$ conn){
 die(“连接失败:”。mysqli_connect_error()); \  n} 
if(isset($ _ POST ['submit'])){//获取以URL格式传递的表单变量
 $ name = $ _POST ['name']; 
 $ email = $ _POST ['  email']; 
if($ name!=''|| $ email!=''){
 
 $ sql =“INSERT INTO'VIP会员'(姓名,电子邮件)
VALUES('$ name',  '$ email')“; 
 
if(mysqli_query($ conn,$ sql)){
 echo”新记录创建成功“; 
} else {
 echo”错误:“。  $ sql。  “&LT峰; br&gt;” 中 。  mysqli_error($ conn); 
} 
} 
 
mysqli_close($ conn); 
  code>  pre> 
  div>

You need to use backticks (not single quotes) to surround your table name, since it has a space in it.

$sql = "INSERT INTO `VIP Membership` (Name, Email) VALUES ('$name', '$email')";

UPDATE: You should really be using prepared statements. As it stands, this code is very unsafe.

$sql = mysqli_prepare($conn, "INSERT INTO `VIP Membership` (Name, Email) VALUES (?, ?)");
if($sql !== FALSE){
    mysqli_stmt_bind_param($sql, "ss", $name, $email);
    if(mysqli_stmt_execute($sql)){
        echo "New record created successfully";
    } else {
        echo mysqli_stmt_error($sql);
    }
} else{
    echo mysqli_error($conn);
}

MariaDB is a backward compatible, binary drop-in replacement of MySQL. That means MariaDB version will work exactly like MySQL. You don't have to do anything specific to make your MySQL code to work on MariaDB. But in your code I would use the mysqli function (or PDO) instead of the old deprecated mysql one.

As you may encounter some incompatibilities you can check this official document for differences between the two. MariaDB vs MySQL

check your brackets { insert code here } if this thing is not arranged properly it brings out an error