PHP MYSQL QUERY没有插入

时间:2017-04-22 17:44:16

标签: php mysql

我在mysql中为我的表添加一些值来测试它但没有任何反应。它甚至没有给我一个错误。

<?php
//get values from index.php

$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];



// Create connection
$conn = mysqli_connect("localhost", "root", "", "coursework");

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


$sql = "INSERT INTO studentlogin (studentid, password)
VALUES ('John', 'Doe')";




?>

任何人都可以告诉我原因。

3 个答案:

答案 0 :(得分:1)

您必须添加mysqli_query()

//get values from index.php

$lecturerid = $_POST['studentid'];
$password = $_POST['Pass'];



// Create connection
$conn = mysqli_connect("localhost", "root", "", "coursework");

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


$sql = "INSERT INTO studentlogin (studentid, password)
VALUES ('John', 'Doe')";

mysqli_query($conn, $sql);


?>

答案 1 :(得分:1)

很简单,

    <html>
    <body bgcolor="lightblue">
    <h1>Data fetched from register.php</h1>
    <table  border=1 cellspacing=0 cellpadding=10px>
    <tr>
    <th>id</th>
    <th>fullname</th>
    <th>email</th>
    <th>username</th>
    <th>dob</th>
    <th>password</th>
    <th>gender</th>
    <th>lanuage</th>
    <th>country</th>
    </tr>
    <?php
    $xyz=mysqli_connect("localhost", "root", "", "myprogrammes");
    $abc=mysqli_query($xyz, "select * from table");
    while ($bb=mysqli_fetch_array($abc)){
          $id1=$bb['id'];
          $fullname1=$bb['fullname'];
          $email1=$bb['email'];
          $username1=$bb['username'];
          $dob1=$bb['dob'];
          $password1=$bb['password'];
          $gender1=$bb['gender'];
          $lanuage1=$bb['lanuage'];
          $country1=$bb['country'];
    ?>
    <tr>
    <th><?php echo $id1 ; ?></th>
    <th><?php echo $fullname1 ; ?></th>
    <th><?php echo $email1 ; ?></th>
    <th><?php echo $username1 ; ?></th>
    <th><?php echo $dob1 ; ?></th>
    <th><?php echo $password1 ;  ?></th>
    <th><?php echo $gender1 ; ?></th>
    <th><?php echo $lanuage1 ; ?></th>
    <th><?php echo $country1 ; ?></th>
    </tr>
<?php  } ?>
    </body>
    </html>

答案 2 :(得分:0)

您需要使用以下方法插入

$firstname = "John";
$lastname = "Doe";
$stmt = $conn->prepare("INSERT INTO studentlogin (studentid, password) 
VALUES (:studentLogin, :password)");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->execute();