MariaDB - PHP - 提交表单时未将数据插入表中

时间:2016-10-02 06:33:23

标签: php html sql mariadb

我对PHP很陌生,所以请耐心等待。我有一个注册表单,我提交在该表单上输入的值并将它们插入到Maria Database表中,但数据没有插入到表中。

我从profileinformation做了一个select *;在桌子上,数据不在那里。

任何帮助表示赞赏,积分将被授予!

这是我的HTML表单:

<!DOCTYPE html>
<html>

<head>

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<title>Registration Page</title>

<script>
function validateForm() {
    var x = document.forms["myForm"]["netID"].value;
    if (x == null || x == "") {
    alert("NetID must be filled out");
    return false;
    }

    var y = document.forms["myForm"]["email"].value;
    if (y == null || y == "") {
    alert("Email must be filled out");
    return false;
    }

    var n = document.forms["myForm"]["fname"].value;
    if (n == null || n == "") {
    alert("First Name cannot be blank");
    return false;
    } else if (n.length < 2) {
        alert("First name cannot be less than 2 characters!");
        return false;
    }

    var b = document.forms["myForm"]["lname"].value;
    if (b == null || b == "") {
    alert("Last Name cannot be blank");
    return false;
    } else if (b.length < 2) {
        alert("Last Name cannot b less than 2 characters!");
        return false;

    }

}

</script>



</head>
<body>

<ul>
  <br>
  <br>
  <br>
  <br>

  <center><img src="KSUlogo.PNG" alt="logo" style="width:100px;height:50px;"></center>
  <br>
  <br>
  <br>
  <br>
  <br>
  <li><a class="active" href="Welcome.html">Home</a></li>
  <br>
  <br>
  <br>
  <br>
  <li><a href="CSERegistrationPage.html">Registration</a></li>
  <br>
  <br>
  <br>
  <br>
  <li><a href="#contact">Search</a></li>
  <br>
  <br>
  <br>
  <br>
  <li><a href="#about">About</a></li>
  <br>
  <br>
  <br>
  <br>
</ul>


<h1 style="text-align:center;">CCSE Community Profile Page</h1>

<br>
<br>
<br>
<br>
<br>


<h2 style="text-align:center;">Enter your Registration Information</h2>

<div style="text-align:center">

<form name="myForm" action="RegistrationValues.php" 
onsubmit="return validateForm()" method="post">

<center>NetID: <input type="text" name="netID"></center>
<br>
<center>Email: <input type="text" name="email"></center>
<br>
<center>First Name: <input type="text" name="fname"></center>
<br>
<center>Last Name: <input type="text" name="lname"></center>
<br>
<br>
Services You Can Provide the CSE Community</center><br>
<br>
 <input type="checkbox" name="radio" value="Java"> Java Tutoring<br>
 <input type="checkbox" name="radio" value="Computer" checked> Computer Fixing<br>
 <input type="checkbox" name="radio" value="PHP" checked> PHP Tutoring<br>
<br><br>
<select name="availabilty">
    <option value="blank"></option>
    <option value="Java">Morning</option>
    <option value="Computer">Evening</option>
    <option value="Service">Afternoon</option>
</select>
<br><br>

<center><input type="submit" value="Submit"></center>
</form>

</div>


</body>
</html>

这是我的PHP表单:

<!DOCTYPE html>
<html>

<head>

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<title>Registration Page</title>

</head>

<body>

<?php include "header.html";?>
<?php include "navigation.html";?>

<div style="text-align:center">

<p>netID: <?php echo $_POST["netID"]?></p>

<p>Email: <?php echo $_POST["email"]?></p>

<p>First Name <?php echo $_POST["fname"]?></p>

<p>Last Name: <?php echo $_POST["lname"]?></p>

<?php

$netID = $email = $fname = $lname = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $netID = test_input($_POST["netID"]);
  $email = test_input($_POST["email"]);
  $fname = test_input($_POST["fname"]);
  $lname = test_input($_POST["lname"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

$servername = "localhost";
$username = "user";
$password = "newpassword";
$dbname = "project";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$conn->query("insert into ProfileInformation (netID, email, fname, lname, radio, availabilty) 
                values
            ( '{$_POST['netID']}', '{$_POST['email']}', '{$_POST['fname']}', '{$_POST['lname']}', '{$_POST['radio']}', '{$_POST['availabilty']}' )") or die(mysql_error());

            echo "Done!!!!";

$conn->close();
?>


</body>
</html>

任何帮助都表示赞赏,并提前致谢!

1 个答案:

答案 0 :(得分:1)

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error);
}

if(!$conn->query("insert into ProfileInformation (netID, email, fname, lname, radio, availabilty) 
                values
            ( '{$_POST['netID']}', '{$_POST['email']}', '{$_POST['fname']}', '{$_POST['lname']}', '{$_POST['radio']}', '{$_POST['availabilty']}' )")){
    echo "Invalid query: ".$conn->error;
}else{
    echo "Done!!!!";
}

$conn->close();