2016-12-14 82 views
-3

我有以下的php腳本,但我插入到我的文本框中的數據沒有被髮布到數據庫中,一旦按鈕被點擊。我沒有收到錯誤,我不知道我的錯誤在哪裏。數據沒有發佈到mySQL數據庫

<?php 

if (isset($_POST['submit'])){ 
    $con = mysqli_connect("localhost","branchaw","211018556","basketball") or die("Could not connect"); 

    $pId = mysqli_real_escape_string($con,$_POST['pid']); 
    $Hometown = mysqli_real_escape_string($con,$_POST['Hometown']); 
    $Year = mysqli_real_escape_string($con,$_POST['Year']); 
    $Position = mysqli_real_escape_string($con,$_POST['Position']); 
    $Season = mysqli_real_escape_string($con,$_POST['Season']); 
    $Date_Of_Birth = mysqli_real_escape_string($con,$_POST['Date_Of_Birth']); 

    $query = "INSERT INTO `General` (`pId`,`Hometown`,`Year`,`Position`,`Season`,`Date_Of_Birth`) VALUES ('".$pId."','".$Hometown."','".$Year."','".$Position."''".$Season."','".$Date_Of_Birth."')"; 

    $result = mysqli_query($con,$query); 

    if($result){ 
    echo 'Data Inserted'; 
    } else{ 
    echo die(mysqli_error($con)); 
    } 

    //mysqli_free_result($result); 
    mysqli_close($con); 
} 
?> 

<html> 
    <head> 
    <title>Add Data to General Information</title> 
</head> 
<body>  
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <label>pId<input type="text" name="pid"/></label> 
     <label>Hometown<input type="text" name="Hometown"/></label> 
     <label>Year<input type="text" name="Year"/></label> 
     <label>Position<input type="text" name="Position"/></label> 
     <label>Season<input type="text" name="Season"/></label> 
     <label>Date of Birth<input type="text" name="Date_Of_Birth"/></label> 
    <br /> 
     <input type="submit" value="Add new data" name="submit"/>  
    </form> 
</body> 
</html> 
+0

sqli_connect?使用mysqli_connect –

+0

它仍然是不添加到我的數據庫。出於某種原因,我沒有收到數據插入或數據未插入的消息 –

+0

參考http://www.tutorialrepublic.com/php-tutorial/php-mysql-insert-query.php –

回答

1
<?php 

    if (isset($_POST['submit'])){ 
     $con = mysqli_connect("localhost","branchaw","211018556","basketball") or die("Could not connect"); 
     $pId = mysqli_real_escape_string($con,$_POST['pid']); 
     $query = "INSERT INTO `General` (`pId`) VALUES ('".$pId."')"; 

     $result = mysqli_query($con,$query); 

     if($result){ 
     echo 'Data Inserted'; 
     } else{ 
     echo 'Data Not Inserted'; 
     } 

     //mysqli_free_result($result); 
     mysqli_close($con); 
    } 
    ?> 

    <html> 
     <head> 
     <title>Add Data to General Information</title> 
    </head> 
    <body>  
     <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
      <label>pId<input type="text" name="pid"/></label> 
     <br /> 
      <input type="submit" value="Add new data" name="submit"/>  
     </form> 
    </body> 
    </html> 

inserted successful

+0

試試吧:) –

+0

好吧,現在我收到數據未插入的錯誤消息 –

+0

@NateB現在嘗試echo'$ result'來查看您得到的SQL錯誤類型。 –

-1
$con = mysqli_connect("localhost","branchaw","211018556","basketball") or die("Could not connect"); 
    $pId = mysqli_real_escape_string($_POST['pId']); 
    $query = "INSERT INTO General (pId) VALUES ('$pId')" or die ("Query Failed"); 

沒有必要在你的表名和字段名放'。

+1

他沒有在他的表字段和名稱中放入一個''。這是一個倒退'',可以把它放在查詢中。 –

+0

確定不要把反撥「'在查詢中,並嘗試希望它會起作用。 –