php - Insert data in mysql table

676

I have problem with inserting data in mysql by PHP code

I use a WAMP server for executing my codes and my database collection is utf8_persian_ci

When I run the code in PHP, using the code below, it doesn't work.

PHP Code:

$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);
} 

But when I execute the MySQL statement in PhpMyAdmin page it works:

MySQL Statement

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)
349

Answer

Solution:

You have not added single quotes into sql query so please add those replace your $query parameter with i have given below n try

$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."');";

Hope this will help :)

People are also looking for solutions to the problem: php - 960 Grid issues with nesting grids within other grids

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.