dongyou2714 2015-10-21 17:59 采纳率: 0%
浏览 145
已采纳

如何将数据插入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);
  • 写回答

3条回答 默认 最新

  • dongzhong5573 2015-10-21 18:40
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?