【问题标题】:How to fix MySQL and PHP in WAMP not inserting data?如何修复 WAMP 中的 MySQL 和 PHP 不插入数据?
【发布时间】:2019-04-10 11:38:53
【问题描述】:

我使用 WAMP 作为我的 php 服务器,我使用 PHP-AdminPage 作为我的数据库(使用 MySql),一切正常,我的数据库已连接。除了当我想显示数据时,行已创建但它们是空的!即使我没有让它们空着。我不明白,我很困惑。

PHP 代码:

<html>
<body>
<?php

$conn = new mysqli('localhost','root','');

//check connection
if ($conn-> connect_error) {
        die("Connection failed: ".$conn->connect_error);
}
echo "Database Connected successfully";

//choose db
mysqli_select_db($conn,"catcafe");

//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";

if($conn->query($sql) === true) {
    echo "New record created";
}
else{
    echo "Error, check values".$sql."<br>".$conn->error;
}


mysqli_close($conn);
?> 

</body>
</html>
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";

MySQL 一直告诉我错误就在这里!但我不知道它是什么,这些列与数据库中的列具有完全相同的名称。

我的表单/HTML 代码:

<form action="InsertReservation.php">
        <label for="fname">First Name  </label><br>
        <input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
        <br>
        <label for="lname">Last Name  </label><br>
        <input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
        <br>
        <label for="Email">Email  </label><br>
        <input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
        <br>
        <label for="Mobile">Mobile Number  </label><br>
        <input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
        <br>
        <label for="Time">Time  </label><br>
        <input type="time" id="Time" name="Time" min="8:00"  required style="width:600px; font-size: 30px;">
        <br>
        <input type="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')"> 
  <button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
      </form>

MySQL 代码

SELECT * FROM `customer` ORDER BY `customer`.`FName` ASC 

我想让它插入数据,但它一直在插入空数据。

【问题讨论】:

  • 确切的错误信息是什么?
  • 你应该学会使用准备好的语句而不是替换变量,以防止 SQL 注入。
  • @Renad 要正确识别问题,我们需要准确的错误消息、表架构、$_POST 转储,
  • 尝试使用以下格式:VALUES ('" . $_POST['firstname'] . "', '" . $_POST['lastname'] . "','" . $_POST['电子邮件'] . "','" . $_POST['Mobile'] . "','" . $_POST['Time'] . "')"

标签: php mysql wamp


【解决方案1】:
  1. 始终验证您的输入数据。
  2. 为用户输入使用准备好的语句
  3. Time 是 mysql 的保留字,所以在它周围使用反引号 ``。

    $conn = new mysqli('localhost', 'root', '');
    
    //check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Database Connected successfully";
    
    //choose db
    mysqli_select_db($conn, "catcafe");
    
    //insert query
    $sql = "INSERT INTO customer(FName,LName,Email,PhNum,`Time`) VALUES(?,?,?,?,?)";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssss", $_POST['firstname'], $_POST['lastname'], $_POST['Email'], $_POST['Mobile'], $_POST['Time']);
    
    if ($stmt->execute()) {
        echo "New record created";
    } else {
        echo "Error, check values " . $stmt->error;
    }
    
    
    mysqli_close($conn);
    ?>
    

【讨论】:

  • 这也不行!致命错误:未捕获错误:调用未定义方法 mysqli_stmt::bin_param() (!) 错误:调用未定义方法 mysqli_stmt::bin_param() i
  • 我又试了一次,它说:错误,检查值列'FName'不能为空,即使它不为空,我插入了数据
  • @MasivuyeCokile 怎么做?
【解决方案2】:

您好,请在单个文件上使用此代码,它工作正常。
谢谢

$conn = new mysqli('localhost', 'root', '');

    //check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Database Connected successfully";

    //choose db
    mysqli_select_db($conn, "catcafe");
    if($conn){
    //insert query

    if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){

     $sql = "INSERT INTO customer(FName,LName,Email,PhNum,Time) 
                VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['Email']."','".$_POST['Mobile']."','".$_POST['Time']."')";

        if($conn->query($sql) === true) {
            echo "New record created";
        }
        else{
            echo "Error, check values".$sql."<br>".$conn->error;
        }
    }

    mysqli_close($conn);
    }

    <form method="post">
            <label for="fname">First Name  </label><br>
            <input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
            <br>
            <label for="lname">Last Name  </label><br>
            <input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
            <br>
            <label for="Email">Email  </label><br>
            <input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
            <br>
            <label for="Mobile">Mobile Number  </label><br>
            <input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
            <br>
            <label for="Time">Time  </label><br>
            <input type="time" id="Time" name="Time" min="8:00" style="width:600px; font-size: 30px;">
            <br>
            <input type="submit" name="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')"> 
      <button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
          </form>

【讨论】:

  • 由于某种原因,这也显示了数据库连接失败的错误,并且根本没有插入数据。
  • 请使用正确的凭据连接到数据库。另外,请告诉我您遇到了什么错误。 @Renad
猜你喜欢
  • 1970-01-01
  • 2017-04-29
  • 2017-02-06
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
相关资源
最近更新 更多