php - 在mysql中使用GET插入数据

标签 php mysql database insert get

<分区>

我想在表“test”中插入数据,但在 mysql 中插入数据时遇到问题 我的数据库名mytesttable

该代码使用 GET 插入数据:

<?php
include_once('confi.php');

echo "hi there this is a test";
//Get the variables here
$username= isset($_GET['username']) ? mysql_real_escape_string($_GET['username']) :  "";
$email = isset($_GET['email']) ? mysql_real_escape_string($_GET['email']) :  "";
$password = isset($_GET['password']) ? mysql_real_escape_string($_GET['password']) :  "";
$insertstatement = 'INSERT INTO `test`(`id`,`username`,`email`,`password`) VAlUES (NULL,"'.$username.'","'.$email.'","'.$password.'")';

$query123 = mysql_query($insertstatement) or trigger_error(mysql_error()." ".$insertstatement);

echo "$query123";


?>

我的代码与 MySQL 连接:

<?php header('Access-Control-Allow-Origin: *'); ?>
<?php header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); ?>
<?php header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT'); ?>

<?php

    $con = mysqli_connect("localhost","root","root","mytesttable","8889");

    if(mysqli_connect_errno())
    {
        echo "Error occured while connecting with database ".mysqli_connect_errno();
    }


?>

我的代码有什么问题?

最佳答案

使用mysqli_prepare .

<?php
include_once('confi.php');

$username= isset($_GET['username']) ? mysql_real_escape_string($_GET['username']) :  "";
$email = isset($_GET['email']) ? mysql_real_escape_string($_GET['email']) :  "";
$password = isset($_GET['password']) ? mysql_real_escape_string($_GET['password']) :  "";

$stmt = mysqli_prepare($con, "INSERT INTO `test`(`username`,`email`,`password`) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sss', $username, $email, $password);

$query123 = mysqli_stmt_execute($stmt);

?>

学习Prepared Statements

[注意: Mysql is deprecated ]

关于php - 在mysql中使用GET插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746456/

相关文章:

php - Mysql:当加入字段等于条件时选择

php - 我应该使用什么格式在 MySQL 表中存储用户的首选时区?

mysql - 非唯一外键

php - 如何使用 PHPMailer 发送包含 CSS 的 HTML 邮件?

php - Ajax 响应在服务器上是 JSON,而在本地计算机上使用 CakePHP 时是 STRING,为什么?

php - cURL 和 PHP 显示 "1"

java - 京都橱柜安装问题

php - 从 CSV 中抓取,序列化选定的字段并修改 CSV

python - 优化 Python 代码以进行数据库访问

php - 如何抓取网站上的动态内容并保存?