[英]mysql insert query fails in php
i am working with databases for almost one year and this time when i tried mysql and php it does not work and i tried using mysqli_error() function and that does not work too i created the database and that's very simple table with 2 columns and it does not work either i am wondering what can i do about that here is what i tried 我使用数据库将近一年,这一次,当我尝试使用mysql和php时,它不起作用,并且尝试使用mysqli_error()函数,这也行不通,我创建了数据库,这是一个非常简单的带有2列的表,它不起作用或者我想知道我该怎么办,这就是我尝试过的
<?php
$cnn=mysqli_connect("localhost","root","password");
if(!$cnn){
die("data base connection failed :" . mysqli_error());
}
//select a kimai fekr db
$db_select=mysqli_select_db($cnn,"testi");
if (!$db_select){
die("database selection failed:" . " " . mysqli_error());
}
?>
<?php
$uname="morteza";
$id=1;
$query="insert into test ('id','uname') values ($id,'{$uname}')";
$result=mysqli_query($cnn,$query);
if($result){
echo "<p>user added</p>";
}
else{
echo "<p>user not added </p>" . mysqli_error('$cnn');}
mysqli_close($cnn);
?>
Those are not the right column identifiers 这些不是正确的列标识符
('id','uname')
^ ^ ^ ^
either remove them 要么删除它们
(id,uname)
or use backticks 或使用反引号
(`id`,`uname`)
Add error reporting to the top of your file(s) right after your opening <?php
tag error_reporting(E_ALL); ini_set('display_errors', 1);
在打开
<?php
标签error_reporting(E_ALL); ini_set('display_errors', 1);
之后立即将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);
which would have signaled the error 这将表明错误
as well as or die(mysqli_error($cnn))
to mysqli_query()
以及
or die(mysqli_error($cnn))
mysqli_query()
Plus, your present code is open to SQL injection . 另外,您当前的代码可以进行SQL注入 。 Use prepared statements , or PDO with prepared statements , they're safer .
使用预处理语句或将PDO与预处理语句 一起使用会更安全 。
Do not use single quotes around your column identifiers. 不要在列标识符周围使用单引号。 Use ticks or nothing at all:
完全不使用刻度线:
$query="insert into test (id,uname) values ($id,'{$uname}')";
or 要么
$query="insert into test (`id`,`uname`) values ($id,'{$uname}')";
你可以试试看吗
$query="insert into test (`id`,`uname`) values ('$id','{$uname}')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.