2012-08-17 3 views
1

mysql 데이터베이스에 연결하는 PHP 페이지가 있습니다. 웹 페이지에 데이터베이스의 정보를 표시하는 PHP 코드가 있기 때문에 데이터베이스 연결이 좋다는 것을 알고 있습니다. 데이터 베 이스에 새 데이터를 삽입하려고하면 페이지가 새로 고침되고 데이터가 삽입되지 않습니다. 삽입 명령에 올바른 값이 있는지 확인했습니다.PHP가 mysql에 데이터를 전달하지 않음

 <?php 

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

      include "connect_to_mysql.php"; 

      $name = mysql_real_escape_string($_POST["Name"]); 

      $sql = mysql_query("SELECT TestID FROM test WHERE Name='$name' LIMIT 1")or die (mysql_error()); 
      $productMatch = mysql_num_rows($sql); 

      if ($productMatch > 0) 
       { 
        echo 'Sorry you tried to place a duplicate "User Account" into the system, <a href="index.php">click here</a>'; 
        exit(); 
       } 
       else 
       { 

        $sql = mysql_query("INSERT INTO test (TestID,Name) 
         VALUES('', '$name')") or die (mysql_error()); 
        $uid = mysql_insert_id(); 

        header("location: index.php"); 
        exit(); 
       } 
     } 
?> 
<?php 

    include "connect_to_mysql.php"; 
    $User_list = ""; 
    $sql = mysql_query("SELECT * FROM test"); 
    $UserCount = mysql_num_rows($sql); 
    if ($UserCount > 0) 
     { 
      while($row = mysql_fetch_array($sql)) 
       { 
         $id = $row["TestID"]; 
         $name = $row["Name"]; 

         $User_list .= "Users ID: $id - <strong>$name</strong>&nbsp; &nbsp; &nbsp;<br />"; 
       } 
     } 
     else 
     { 
      $User_list = "You have no users listed in the database."; 
     } 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<div align="center" id="mainWrapper"> 
    <div id="pageContent"><br /> 
    <div align="right" style="margin-right:32px;"><a href="index.php#UserForm">+ Add New User</a></div> 
<div align="left" style="margin-left:24px;"> 
     <h2>User list</h2> 
     <?php echo $User_list; ?> 
    </div> 
    <hr /> 
    <a name="UserForm" id="UserForm"></a> 
    <h3> 
    &darr; Add New User Form &darr; 
    </h3> 
    <form action="index.php" enctype="multipart/form-data" name="myForm" id="myform" method="post"> 
    <table width="90%" border="0" cellspacing="0" cellpadding="6"> 
     <tr> 
     <td width="20%" align="right">Name</td> 
     <td width="80%"><label> 
      <input name="name" type="text" id="name" size="50" /> 
     </label></td> 
     </tr> 
     <tr> 
     <td>&nbsp;</td> 
     <td><label> 
      <input type="submit" name="button" id="button" value="Add This Name Now" /> 
     </label></td> 
     </tr> 
    </table> 
    </form> 
    <br /> 
    <br /> 
    </div> 
</div> 
</body> 
</html> 
+0

어쩌면 당신은 MySQL의 인스턴스를 열어 거기에서 바로 데이터를 삽입 해 볼 수 있을까요? PHP를 사용하여 DB에 물건을 삽입하려고하면 실제로 무엇이 잘못되었는지는 알려주지 않지만 MySQL을 직접 사용하면 잘못 된 것 (아마도 이스케이프 처리되지 않은 따옴표 또는 마이너 등)을 알려줍니다 그러나 붙잡기 단단한). – Raymond

+1

이전의'mysql _ *()'함수를 더 이상 사용하지 않는 것이 좋습니다. 지원 중단 예정입니다. 대신 PDO 또는 MySQLi와 같은 준비된 문을 지원하는 최신 API 사용을 고려하십시오. –

답변

0

두 가지 문제가 즉시 발생합니다 (그 이상의 경우가 있음). 첫째, PHP 배열 키는 대소 문자를 구분합니다. $_POST['Name']에 액세스하고 있지만 양식 입력은 name입니다. 둘째, 당신은 어디서나 존재 나타나지 않는 $_POST['User_Name']에 대한 테스트 : 테이블이 TestID에 AUTO_INCREMENT 아이디가있는 경우

나중에
// Look for name in the $_POST 
if (isset($_POST['name'])) 
{ 
    include "connect_to_mysql.php"; 
    // name is case-sensitive 
    $name = mysql_real_escape_string($_POST["name"]); 

, 당신이 그것을 생략하거나 삽입 한 Statment에 NULL을 삽입해야 하나 :

// Don't include TestID if it is AUTO_INCREMENT. That will happen automatically 
$sql = mysql_query("INSERT INTO test (Name) 
        VALUES('$name')") or die (mysql_error()); 
+0

도움을 주신 분들께 Thnx가 고쳐 주셨습니다. 내가 그렇게 간단한 것을 놓친 것 같아요. 하루 종일 코드를 쓰는 것이 좋은 이상적이지는 않습니다. LOL. – Jacob

0

나는 당신이 변경되면 작동합니다 생각

if (isset($_POST['User_Name'])) 

if (isset($_POST['Name'])) 

귀하의 양식에 존재하지 않는 것이 있는지 확인하십시오.

0

추가 : TestID가 자동 증가가

경우 오류를 얻을 수없는 경우, 오류가 의미

"INSERT INTO test (Name) VALUES('$name')" 
0

"INSERT INTO test (TestID,Name) VALUES('', '$name')" 

아래의 변경 당신의 MySQL 구문에서 그것을 테스트하는 두 가지 방법은 구문을 PH로 복사하는 것입니다. PMyAdmin 또는 기본 MySQL 명령 줄이 무엇이든간에 출력 오류가 발생하는지 확인하십시오. 또는 당신이 할 수있는 또 다른 일은 당신의 모든 mysql_query()을 수정하는 것입니다; 함수를 추가하여 mysql_query()or die(mysql_error());

관련 문제