PHP插入错误..尝试了一切

时间:2013-04-14 21:30:20

标签: php mysql database forms sql-insert

可能是一些愚蠢的东西,因为我的MySQL PHP知识如此生疏......我清理了所有可能的错误,但仍然无法写入我的数据库。任何人都可以对可能出错的事情有所了解,因为它甚至不再打印出错误。感谢:

<?php
$conn=mysql_connect("127.0.0.1","root","","pe_results"); 


        if (isset($_POST['Name'])) { 
        $Name = $_POST['Name'];
        }
        if (isset($_POST['Short'])) { 
        $Short = $_POST['Short'];
        }
        if (isset($_POST['Med'])) { 
        $Med = $_POST['Med'];
        }
        if (isset($_POST['Long'])) { 
        $Long = $_POST['Long'];
        }
        if (isset($_POST['VLong'])) { 
        $VLong = $_POST['VLong'];
        }
        if (isset($_POST['Extreme'])) { 
        $Extreme = $_POST['Extreme'];
        }
        if (isset($_POST['LJump'])) { 
        $LJump = $_POST['LJump'];
        }
        if (isset($_POST['HJump'])) { 
        $HJump = $_POST['HJump'];
        }
        if (isset($_POST['Shotputt'])) { 
        $Shotputt = $_POST['Shotputt'];
        }
        if (isset($_POST['Discuss'])) { 
        $Discuss = $_POST['Discuss'];
        }
        if (isset($_POST['Javelin'])) { 
        $Javelin = $_POST['Javelin'];
        }
        if (isset($_POST['Date'])) { 
        $Date = $_POST['Date'];
        }
        if (isset($_POST['Year'])) { 
        $Year = $_POST['Year'];
        }



            $sql="INSERT INTO results_main (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year)
            VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";

    $result = mysql_query($sql,$conn);
        if($result){
echo"<br/>Everythings been saved";
echo "<BR>";
echo "<a href='index.html'>Back to the main page</a>";
}

else {
echo 'Fatal Error, you information has not been saved';
}

// close connection 
mysql_close($conn);
?>

以下是更新:

查询是:INSERT INTO results_main(Name,Short,Med,Long,VLong,Extreme,LJump,HJump,Shotputt,Discuss,Javelin,Date,Year)VALUES('Chris Davern','10:00', '20:00','40:00','80:00','15:00','100m','10m','15m','50m','400m','2013-03-27 ','9')

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行“Long,VLong,Extreme,LJump,HJump,Shotputt,Discuss,Javelin,Date,Year”附近使用正确的语法

4 个答案:

答案 0 :(得分:1)

这意味着您没有选择数据库。以下是我用来连接mysql_函数的方法:

$host = "localhost";
$databasename = "pe_results";
$databaseusername = "root";
$databasepassword = "password";

$conn = mysql_connect("$host", "$databaseusername", "$databasepassword"); 
mysql_select_db("$databasename", $conn); 

$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
        VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";

$result = mysql_query($sql);

另请注意,更新了列名,因为LONG列名是mysql关键字,因此需要将其识别为列名而不是关键字。

然后你可以执行你的mysql_query。但是,就像我们之前提到的那样不推荐使用mysql_函数,不应再使用它们。

答案 1 :(得分:1)

单词LONG是MySQL中的保留字...尝试用反引号包装所有内容:

        $sql="INSERT INTO `results_main` (`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
        VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";

而不是

        $sql="INSERT INTO results_main (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year)
        VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";

答案 2 :(得分:0)

阅读php doc,你的mysql_connect的第四个参数应该是一个bollean,我想这是你的DB的名字,所以你永远不会选择任何数据库。使用:

mysql_select_db("pe_results");

此代码可能会打印此错误。 :

$result = mysql_query($sql,$conn) or die (mysql_error ());

答案 3 :(得分:0)

创建连接时没有选择数据库,因此可以这样指定:

mysql_select_db("Put your Database Name Here");