如何使用mysqli_query

时间:2014-07-11 12:25:10

标签: php mysql

我输入了代码,但它显示了查询中的错误。

<?php
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$class=$_POST['class'];
$gen=$_POST['gender'];
$email=$_POST['email'];
$dbc= mysqli_connect('localhost','sam','xxx','student') or die('error in mysql');           
$query="INSERT INTO student(firstname,lastname,class,gender,email)"
    ."VALUES('$firstname','lastname','class','gen','email')";
$result= mysqli_query($dbc,$query) or die('error in querying');
mysqli_close($dbc);
echo 'your name:'.$name.'<br>';
echo 'your class:'.$class.'<br>';
echo 'your email:'.$email.'<br>';
echo 'your gender:'.$gen.'<br>';
?>

2 个答案:

答案 0 :(得分:1)

应该是:

$query="INSERT INTO student(firstname,lastname,class,gender,email)"
    ." VALUES('$firstname','lastname','class','gen','email')";

VALUES之前的空格。

我不知道您是否要将其他变量值插入数据库或只是将字符串放在此处仅供测试,所以也许您也想要:

$query="INSERT INTO student(firstname,lastname,class,gender,email)"
    ." VALUES('$firstname','$lastname','$class','$gen','$email')";

您想要做的是PHP的基础知识。所以看看PHP手册来学习如何使用变量和mysqli

此外,此代码非常不安全。详细了解SQL Injection

答案 1 :(得分:0)

尝试下面的代码,它将起作用

$query="INSERT INTO student(firstname,lastname,class,gender,email) VALUES('$firstname','$lastname','$class','$gen','$email')";