sql 语法错误;

Error in sql syntax;

这是我得到的错误;

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

'mysql_query("INSERT INTO `user`(fname,lname,email,'user-id') VALUES ('$fnam' at line 1

谁能帮我找出问题所在?

<?php

//Database Connection
require_once ("../common/dbconnection_inc.php");
//Query to select the data
$sql="SELECT * FROM `user-role`";
$result=mysqli_query($conn,$sql)




?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>ADZ Insurance Brokers | Add User</title>

<link href="common/css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<link href="common/js/facebox/facebox.css" media="screen" rel="stylesheet" type="text/css" />

<script src="common/js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="common/js/jquery-ui.js" type="text/javascript"></script>
<script src="common/js/facebox/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox({
        loadingImage : 'common/js/facebox/loading.gif',
        closeImage   : 'common/js/facebox/closelabel.png'
      });
    })
  </script>
</head>

<body>

    
    
    <!--Content start-->
    <h3 class="heading4 text-center"><strong>Add User</strong></h3> 

 <div class="addusercontainer centerit">
 
    <form name="adduser" method="post"> 
     <div class="field floatleft">
         <div class="fieldname floatleft">First Name:</div>
            <div class="textfield floatleft"><input type="text" id="fname" name="fname" /></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">Last Name:</div>
            <div class="textfield floatleft"><input type="text" id="lname" name="lname" /></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">Email:</div>
            <div class="textfield floatleft"><input type="email" id="email" name="email" required="required"/></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">User Role:</div>
            <div class="textfield floatleft">
             <select name="urole" id="urole">
    <?php while($row=mysqli_fetch_array($result)){?>
                    <option value="<?php echo $row['user-role-id'];?>">
                    <?php echo $row['user-role-name']; ?>  
                    </option>
                <?php } ?>
                </select>
            </div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">User Name:</div>
            <div class="textfield floatleft"><input type="text" id="username" name="username" /></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">NIC/ Business Registeration No:</div>
            <div class="textfield floatleft"><input type="text" id="nic" name="nic" /></div>
        </div>
        <div class="clearit"></div>
        
        
        <div class="field floatleft">
         <div class="fieldname floatleft">Password:</div>
            <div class="textfield floatleft"><input type="password" id="pass" name="pass" /></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <div class="fieldname floatleft">Confirm Password:</div>
            <div class="textfield floatleft"><input type="password" id="cpass" name="cpass" /></div>
        </div>
        <div class="clearit"></div>
        
        <div class="field floatleft">
         <input type="submit" class="btn btn-primary btnmarginleft" value="Submit"/>
  </div>
    
    
    <?php
if (isset($_POST['submit']))
 {    
 
     $fname=$_POST['fname'];
      $lname=$_POST['lname'] ;
     $email= $_POST['email'] ;     
     $nic= $_POST['nic'] ;
            
   mysql_query("INSERT INTO `user`(fname,lname,email,'user-id') 
   VALUES ('$fname','$lname','$email','$nic')"); 
    
    
         }
?>
    </form>    
 
    </div>
    
    <!--Content end-->
    



<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
</body>


</html>

user-id:

使用反引号代替单引号
mysql_query("INSERT INTO `user`(fname,lname,email,`user-id`) 
     VALUES ('$fname','$lname','$email','$nic')"); 

使用mysqli有什么问题

Mysqli php.net

dbconnection.inc.php:

define("DB_USER","username");
define("DB_PASSWORD","password");
define("DB_HOST","127.0.0.1:3306");
define("DB_DATABASE","db_name");
$db = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
if ($db->connect_errno) {
    echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
}

然后你的电话:

$res = $db->query("SELECT * FROM `user_role`");

结果:

<?php 
while($row = $res->fetch_assoc()) { ?>
    <option value="<?php echo $row['user-role-id'];?>">
        <?php echo $row['user-role-name']; ?> 
    </option>
<?php } ?>

确保字段 "user-role-id" 和 "user-role-name" 存在于您的数据库中。

第二次调用:

if(isset($_POST['submit']))
    {
         $fname = $_POST['fname'];
         $lname = $_POST['lname'];
         $email = $_POST['email'];
         $nic = $_POST['nic'];
         $res = $db->query("INSERT INTO `user` (fname,lname,email,`user-id`
         VALUES ('$fname','$lname','$email','$nic')");
         if(!$db->error)
             {
                 //Call successfull
             }
    }

确保在调用中使用反引号 `。 "user_role" 和 "user-id".

顺便说一下,尝试为变量名采用类似的模式。例如,决定使用“-”表示空格或“_”。如果你把它们混在一起,以后只会让你感到困惑,因为你必须一直查找你使用的是哪种垫片...