数据未发布到mySQL数据库

时间:2016-12-14 04:58:42

标签: php mysql mysqli

我有以下php脚本,但是一旦单击按钮,我插入到文本框中的数据就不会发布到数据库中。我没有收到错误,我不知道我的错误在哪里。

<?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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-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");

无需在表名和字段名中加上''。