MySql插入查询无法在PHP中运行? [关闭]

MySql插入查询无法在PHP中运行?  [关闭]

问题描述:

<?php
            $con = mysql_connect("localhost:3306","root","");
            try
            {
                if (!$con)
                 {
                     die('Could not connect to MySql Server : ' . mysql_error());
                }

                $db_found = mysql_select_db("registerdb");  

                if ($db_found) 
                {
                    print "Database Found";
                }
                else 
                {
                    print "Database NOT Found";
                }
                mysql_query("INSERT INTO register VALUES ('$_POST[Field2]','$_POST[Field4]','$_POST[Field7]','$_POST[Field8]','$_POST[Field9]','$_POST[Field13]','$_POST[Field14]','$_POST[Field15]')");

                mysql_close($con);
            }
            catch(Exception $e)
            {
                echo 'Message: ' .$e->getMessage();
            }
?>

I am not getting any error at run time, select query working fine for same database. Here is the table defination for register

 Name          Type           Null      Key
 id       bigint(10)      NO        PRI     auto_increment
sname     varchar(20)     NO             
dob       date  YES       NULL   
email     varchar(30)     NO             
gender    varchar(7)      NO             
mobile    int(10)     YES            
country       varchar(15)     NO             
state     varchar(15)     YES            
city      varchar(15)     YES 

I am not getting any sysntax error in php.

Sorry for my mistakes, I edited it.

Actually the syntax was wrong,

Insert into register ('field1','field2',...,'field n') values ('value1','value2',..,'value n');

You messed up your 4th variable:

POST[Field4] [Should Be] $_POST[Field4]

You also missed a closing quote after Field15

To avoid any erros, it is also a good idea to include your schema in the query:

INSERT INTO example_table (field1, field2, field3) VALUES (value1, value2, value3);

EDIT

How about we try for an all out escaped Query...

mysql_query('INSERT INTO register VALUES ("'.$_POST['Field2'].'","'.$_POST['Field4'].'","'.$_POST['Field7'].'","'.$_POST['Field8'].'","'.$_POST['Field9'].'","'.$_POST['Field13'].'","'.$_POST['Field14'].'","'.$_POST['Field15'].'")');

,'$_POST[Field15])'");
                  ^_______________ Missed quote.

Use parameters instead POST data concatenation to avoid potential SQL Injection.

I cannot see any problems here, but I guess the 'POST[Field4]' is a typo - but as others have said, you should really not use the mysql_ functions.

mysql_query does not throw exceptions, you have to check it's return value and call mysql_error for further information.

As stated in the comments, mysql ist deprecated.

It should be like:

$query = "INSERT INTO table(column 1, column 2, column 3) VALUES('".$value1."','".$value2."', '".$value3."')";

Try this.