在mysql表中插入数据

时间:2016-11-26 09:11:19

标签: php mysql

我有通过PHP代码在mysql中插入数据的问题

我使用WAMP服务器执行我的代码,我的数据库集合是utf8_persian_ci

当我在PHP中运行代码时,使用下面的代码,它不起作用。

PHP代码:

$query="insert into lesson_form(lessid,teachid,tvahed,maghta,snazari,samali,skargahi,zamantashkil,ahdaf,masli,mfarei,mosharekat,tamrinat,project,bfaaliat,bmidterm,bfinal)
        values ($lessid,$teacherid,$tvahed,$maghta,$snazari,$samali,$skargahi,$zaman,$ahdaf,$masli,$mfarei,$mosharekat,$tamrinat,$project,$bfaaliat,$bmidterm,$bfinal);";

mysqli_query($con,"set names 'utf8'");        

$run_query=mysqli_query($con,$query);
if (mysqli_query($con, $query))
{
    echo "New record created successfully";
} 
else 
{
    echo "Error: " . "<br>" . mysqli_error($con);
} 

但是当我在PhpMyAdmin页面中执行MySQL语句时,它可以工作:

MySQL声明

INSERT INTO lesson_form(lessid,teachid,tvahed,maghta, snazari,samali,skargahi,zamantashkil,ahdaf,masli,mfarei, mosharekat,tamrinat,project,bfaaliat,bmidterm,bfinal) values (101,27401,3,'کارش',48,48, 48,'شنبه','حضو','www.w3school.com','جز','tamrinat', '','',12,4,4)

1 个答案:

答案 0 :(得分:0)

  

没有在sql查询中添加单引号所以请添加那些替换你的$ query参数,我已经在下面给出了n试试

$query = "insert into lesson_form(lessid,teachid,tvahed,maghta,
    snazari,samali,skargahi,zamantashkil,ahdaf,masli,mfarei,
    mosharekat,tamrinat,project,bfaaliat,bmidterm,bfinal)
    values ('".$lessid."','".$teacherid."','".$tvahed."','".$maghta."','".$snazari."','".$samali."','".$skargahi."'
    ,'".$zaman."','".$ahdaf."','".$masli."','".$mfarei."','".$mosharekat."','".$tamrinat."'
    ,'".$project."','".$bfaaliat."','".$bmidterm."','".$bfinal."');";

希望这会有所帮助:)