连接Php,html和mysql

时间:2011-04-18 07:33:05

标签: php mysql sql html database

有人可以查看我的代码,让我知道它有什么问题吗?

我遇到的问题是,当我向3个字段输入文本并点击提交时,它不会插入我的数据库(mysql,phpmyadmin为gui)。没有错误消息或任何东西;它只是不插入数据..

我一遍又一遍地查看代码,我不能指出它有什么问题。

//---------------------------This is my index.php------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Web Bar Title</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>


<?php
if(isset($_POST['Submit']))
{
   include 'connectdb.php';
   include 'openconnection.php'; 

    $first = $_POST['first'];
    $second = $_POST['second'];
    $third = $_POST['third'];

    $query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";
    mysql_query($query) or die('Error, insert query failed');
}
?>

        <div id="page">

            <tbody>
            <form method="post">
                <table>
                <tr>
                <td ><b>First</b></td>
                <td><input name="first" type="text" id="first"></td>
                <tr>
                <tr>
                <td ><b>Second</b></td>
                <td><input name="second" type="text" id="second"></td>
                <tr>
                <td ><b>Company</b></td>
                <td><input  name="third" type="text" id="third" >  </td>
                </tr>
                </table>
            <input name="submit" type="submit" id="submit" value="Submit" />
            </form>
            </body>
            </html>
            </tbody>

        </div>

//---------------------------------connectdb.php------------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>

//---------------------------------openconnection.php-------------------------------------
<?php
$dbhost = 'localhost';
$dbuser = 'sharkk';
$dbpass = 'pw';
$dbname = 'test';
?>

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname) or die ('No Selection of Database');
?>

编辑:如果有人有这样的话,通过MSN / AIM / Steam / Skype进行通信会更容易,更快捷!

4 个答案:

答案 0 :(得分:2)

将您的第一行更改为

if( isset( $_POST['submit'] ) ) {

我不记得它是否区分大小写

答案 1 :(得分:1)

最好还是将其改为

if($_SERVER['REQUEST_METHOD'] == 'POST')

提交按钮上的isset()方法不可靠,因为如果用户按下回车键提交表单,Internet Explorer将不会将提交按钮作为post变量发送,因此您的代码将不会检测表单提交。

答案 2 :(得分:0)

只需在您自己的MySQL平台中检查MYSQL INSERT查询,只需复制INSERT查询并粘贴到MySQL数据库中,然后检查即可。它会报告你的错。

如果我是对的,那么你的问题就在于INSERT查询部分。你说过:

$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third');";

在上面的部分,不应该有2个分号,Jus one就够了......它看起来像:

$query = "INSERT INTO details (first, last, third) VALUES('$first','$second','$third')";

另外,检查你也包括部分......

它不应该是:

include 'connectdb.php';  //braces r missing
include 'openconnection.php';  // braces r missing 

它应该是:

include ('connectdb.php');
include ('openconnection.php'); 

我希望这可以做得很好....

CHEERS哥们.........

答案 3 :(得分:-1)

尝试将值放在双引号之间而不是单引号

VALUES("$first","$second","$third")