programing

MariaDB - PHP - Form 제출 시 테이블에 데이터가 삽입되지 않음

yellowcard 2023. 10. 2. 13:54
반응형

MariaDB - PHP - Form 제출 시 테이블에 데이터가 삽입되지 않음

제가 PHP를 처음 접하니까 참고해주세요.저는 등록 양식을 가지고 있고 그 양식에 입력된 값을 제출하고 마리아 데이터베이스 테이블에 삽입하고 있지만 데이터가 테이블에 삽입되지 않고 있습니다.

프로필 정보에서 *를 선택했는데 테이블에 데이터가 없습니다.

도움을 주시면 감사하겠습니다!

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>

어떤 도움이든 감사드리고 미리 감사드립니다!

$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();

언급URL : https://stackoverflow.com/questions/39814479/mariadb-php-data-not-being-inserted-into-the-table-when-the-form-is-submitte

반응형