数据不插入我使用的是php mysql

时间:2014-12-21 05:33:23

标签: php mysql

   <?php
    error_reporting(0);
    //connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "visionci";

    // Create connection
    $conn = new mysqli($servername, $username, $password,$dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    //file properties
    //if (isset($_POST['image']))  

     $file = $_FILES["image"]["tmp_name"];


    if (!isset($file))
         {//echo "Please Select an Image";
         }

    else
        {   $name = $_POST["name"];
            $rollno = $_POST["rollno"];
            $address = $_POST["address"];
            $duration = $_POST["duration"];
            $course=$_POST["course"];
            $fname = $_POST["fname"];
            $mname = $_POST["mname"];
            $image=addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
            $image_name= addslashes($_FILES["image"]["name"]);
            $image_size=  getimagesize($_FILES["image"]["tmp_name"]);

            if($image_size==FALSE)
                {echo "That's not an Image";}
            else
            {
                $sql = "INSERT INTO insert (rollno,name,image,address,duration,fname,mname,course)VALUES('$rollno','$name','$image','$address','$duration','$fname','$mname','$course')";

                if ($conn->query($sql) === TRUE) 
                {
                    //$lastid= mysqli_insert_id($conn);
                    echo "Record Inserted Successfully!";
                } 
                else 
                {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }


            }
        }

以上是我的代码。我得到一些垃圾值后跟一个错误[你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'insert(rollno,name,image,address,duration,fname,mname,course)附近使用正确的语法VALUES('','','ÿØ'在第1行]

让我知道我在哪里遇到问题我被困住了。

2 个答案:

答案 0 :(得分:0)

insert更改您的表名。这与命令insert无法区分,并且您无法从名为insert的表中进行选择而无需转义

INSERT INTO something_else

INSERT INTO `insert`

答案 1 :(得分:0)

$ sql =“INSERT INTO insert (rollno,name,image,address,duration,fname,mname,course)VALUES('$ rollno','$ name','$ image' , '$地址', '$持续时间', '$ FNAME', '$ MNAME', '$当然')“;

在这里,您使用了“插入”作为表名。它是MySQL中的保留关键字,因此将该名称更改为另一个值,然后重试。