使用PHP将HTML页面与MySQL连接

时间:2013-02-05 07:11:17

标签: php html mysql

这是我将页面连接到数据库(MySQL)的代码:

<html>
<body>
<title>Home</title>

<script language="javascript">


function checkTextField(field) {
    if (field.value == '') {
        alert("Field is empty");
    }
}


function a(id) {
    var ay = document.getElementById(id).value;

var pattern = /\d{4}-\d{2,4}/;

    if(pattern.test(ay))

    {

    ay.style.backgroundColor="#52F40C";
    return true;
        } 
    else 
    {
     window.alert ("Enter in YYYY-YY or YYYY-YYYY format");
    ay.style.backgroundColor="red";
     ay.focus();
         ay.value="";        
     return false;

    }
}


function c()
{


    var n=document.getElementById("name");

    var re=/^[a-zA-Z]+ ?[a-zA-Z]*$/;

    if(re.test(n.value))
    {

               n.style.backgroundColor="#52F40C";
    }
    else
    {
        window.alert("Invalid place name");
               n.style.backgroundColor="#F40C0C";
               n.focus();
               n.value="";

    }
}

function d()
{
 var n= document.getElementById("date");
 var re=/^(?:(0[1-9]|1[012])[\- \/.](0[1-9]|[12][0-9]|3[01])[\- \/.](19|20)[0-9]{2})$/;
  if (re.test(n.value))
  {
     n.style.backgroundColor="#52F40C";

  }
  else
  {
    window.alert("enter in MM DD YYYY format");
               n.style.backgroundColor="#F40C0C";
               n.focus();
               n.value="";

  }
}

</script>
<body style="background-color:#708090;">

<?php

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

    $mysqli= new mysqli("localhost","admin","admin", "nba" );

    if($mysqli === false)
    {
        die("could not connect:" . mysqli_connect_error());
    }

    if ($inputError != true && empty($_POST['ayear']) )
    {
        echo 'ERROR: Please enter a valid year';
        $inputError = true;
    }
    else
    {
        $ayear = $mysqli-> escape_string($_POST['ayear']);
    }

    if ($inputError != true && empty($_POST['fyear']) )
    {
        echo 'ERROR: Please enter a valid year';
        $inputError = true;
    }
    else
    {
        $fyear = $mysqli->  escape_string($_POST['fyear']);
    }   

    if ($inputError != true)
    {
        $sql="INSERT INTO year VALUES ('$ayear','$fyear')";
        if ($mysqli-> query($sql)==true)
        {
            echo'Added';
        }
        else
        {
            echo"ERROR: Could not execute query : $sql. " . $mysqli-> error;
        }
    }

    $mysqli-> close();

}
?> 







<h1 style="font-family:Verdana;text-align:center;">National Board of Accrediation <br>of<br> Programme</h1>
<br>

<div id="menu" style="background-color:#800000;height:25px;width:1000px">
<b><font "style="font-family:Verdana"><a href="part1.html" title="Institutional Summary"style="color: #000000">Part I</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="part2.html"title="Departmental/Program Summary"style="color: #000000">Part II</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="part3.html"title="Curricula,Syllabi,PEOs and POs"style="color: #000000">Part III</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="part4.html" title="List of Documents to be made available during the visit"style="color: #000000">Part IV</a></font></b>
</div>



</p>

<h4 style="font-family:Verdana;text-align:center;"><b><u>Declaration</u></b></h4>

<form method="post" action="home.html">

<p> (<input type="text" size="9"  name="ayear" id="ayear" onChange="a('ayear');" onBlur="checkTextField(this);">)  (<input type="text" size="9"  name="fyear" id="fyear" onChange="a('fyear');" onBlur="checkTextField(this);">).</p>




<p>Place:<input type="text" size="20" name="name" id="name" onChange="c();" onBlur="checkTextField(this);"></p>

<p>Date:<input type="text" size="10" name="date" id="date" onChange="d();" onBlur="checkTextField(this);">


<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

数据库连接无效。我怎样才能解决这个问题?代码有什么问题?

4 个答案:

答案 0 :(得分:0)

除非您将HTML设置为像PHP代码一样执行,否则您的动态代码都不会起作用。

使用.php扩展程序保存该文件的副本,并测试其是否有效。

答案 1 :(得分:0)

将文件扩展名更改为.php

此外,您的表单操作包括PHP扩展。如下所示

<form method="post" action="home.php">

答案 2 :(得分:0)

除了其他建议外,我看到你在 - &gt;之后放了一个'空格'。在几个地方;

e.g。

$mysqli-> escape_string($_POST['ayear']);

应该是

$mysqli->escape_string($_POST['ayear']);

答案 3 :(得分:0)

为什么要尝试在php中执行HTMl代码 保存.php扩展名的文件肯定会有效。