0

im adding some values to my table in mysql to test it but nothing happens. it doesnt even give me an error.

<?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')";




?>

can anyone tell me why.

4
  • 2
    You're not executing the query. mysqli::prepare() (php.net/mysqli.prepare) / mysqli::query() (php.net/mysqli.query) Commented Apr 22, 2017 at 17:44
  • how to execute ? im new to this Commented Apr 22, 2017 at 17:45
  • Use mysqli::prepare() if you're using variables in the query, mysqli::query() can be used for static queries. Commented Apr 22, 2017 at 17:46
  • Can you mark one of the answer below as 'solved' since you wrote 'thanks it works'. Commented Apr 22, 2017 at 18:07

3 Answers 3

1

You must add 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);


?>
Sign up to request clarification or add additional context in comments.

1 Comment

It's ok. Welcome
1

Quite simple,

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

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

   //execute query
   mysqli_query($conn,$sql) or die(mysqli_error($conn));

?>

1 Comment

Pleasure is mine @Java_NewBie :)
0

You need to use the below methods to insert it

$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();

1 Comment

isn't this PDO syntax? the OP is using mysqli. Good call, bad number :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.