0

这是我将页面连接到数据库(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

4 回答 4

0

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

.php使用扩展名保存该文件的副本并测试它是否有效。

于 2013-02-05T07:20:43.990 回答
0

将文件扩展名更改为 .php

此外,在您的表单操作中还包括一个 PHP 扩展。像下面

<form method="post" action="home.php">
于 2013-02-05T09:23:07.113 回答
0

为什么你试图在 保存你的文件扩展名中执行你的php代码肯定会起作用。HTMl.php

于 2013-11-30T17:31:25.047 回答
0

除了其他建议之外,我看到您在 -> 几个位置之后放置了一个“空格”;

例如

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

应该

$mysqli->escape_string($_POST['ayear']);
于 2013-02-05T10:32:52.697 回答