본문 바로가기
MySql

MySQL 텍스트 상자에서 데이터베이스로 사용자 입력 삽입 (mysql을 사용하여 PHP에서 PHPMYADMIN으로)

by 베이스 공부 2020. 11. 10.
반응형

mysql을 사용하여 텍스트 상자 (html / php)에서 데이터베이스 (phpmyadmin)로 사용자 입력을 어떻게 삽입합니까?

"삽입하지 못했습니다"라는 오류가 계속 발생합니다. 내 코드에 뭔가 누락되어 있습니다.

나는 그것을 고치는 방법에 대해 온라인으로 검색했지만 아무것도 작동하지 않습니다. 내 코드에 뭔가 빠진 것 같아서 지적 할 수 없습니다.

아래의 모든 파일은 index.php라는 1 개의 php 파일에 있습니다.

<!DOCTYPE html>
 <?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'dad_trading';

$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db);




if (isset($_POST['submit']))
{
    $Lastname   = $_POST['LastName'];
    $firstname  = $_POST['FirstName'];
    $Middlename = $_POST['MiddleName'];
    $address    = $_POST['Address'];
    $city       = $_POST['City'];
    $zipcode    = $_POST['ZipCode'];
    $email      = $_POST['email'];
    $number     = $_POST['number'];


     $query = ("INSERT INTO customer ([LName], [FName], [MName], [Street], [City], [ZipCode], [Email], [ContactNo]) VALUES ('$Lastname', '$firstname', '$Middlename', '$address', '$city','$zipcode', '$email', '$number')");

if(mysql_query($query))
 {
echo "<script>alert('INSERTED SUCCESSFULLY');</script>";
}
else
 {
 echo "<script>alert('FAILED TO INSERT');</script>";
 }

 }
?>

<html>
    <head>
        <meta charset="UTF-8">
        <title>sample</title>
    </head>
    <body>
        <form action="" method = "POST">

   First name:   
  &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  Middle Name:
  &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  Last Name:<br>
  <input name="FirstName" size="15" style="height: 19px;"  type="text" required>
      &nbsp; &nbsp; &nbsp; 
  <input name="MiddleName" size="15" style="height: 19px;"  type="text" required>
      &nbsp; &nbsp; &nbsp; 
  <input name="LastName" size="15" style="height: 19px;"  type="text" required>

  <br><br>

    Email Address:<br>
  <input name="email"  type="text" required placeholder="Enter A Valid Email Address" style="height: 19px;" size="30"><br><br>

  Home Address: <br>
  <input name="Address" type="text" required placeholder="Enter your home Address" style="height: 19px;" size="30" maxlength="30"><br><br>

  City:
   &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
  Zipcode:
   <br>
  <input name="City" size="7" style="height: 19px;"  type="text" required>
    &nbsp; &nbsp; 
    <input name="ZipCode" size="7" style="height: 19px;"  type="text" required>
    <br><br>

  Telephone/Mobile Number: <br>
  <input name="number" type="text" required id="number" placeholder="Mobile Number" style="height: 19px;">

<br>
<br>

<button type ="submit" name="submit" value="send to database"> SEND TO DATABASE </button>
</form>
    </body>
</html>   

 

해결 방법

 

서버 변수를 사용하여 양식 조치를 추가하십시오.

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

 

참조 페이지 https://stackoverflow.com/questions/35813147

 

 

반응형

댓글