1

I have read a bunch of the other posts about similar issues but I am very new to this so I am likely missing something. Most of the other questions I found were a lot more complicated than mine. I am trying to follow the w3schools tutorial for this and am testing locally using XAMPP. Right now I am just trying to get this to work successfully and eventually I am planning to submit data into a mySQL database from a web form.

<?php
$servername="localhost";
$dbname="mysql";

// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO owner_table (ownerFirst, ownerLast, mobile)
VALUES ('John', 'Doe', '1111111111')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

?>

NOTE: The connection is not failing from what I can tell. Removing everything after the first if statement, and adding else { echo "success" } outputs success. Again though, I am new to PHP.

If someone knows that the answer has been given in another post PLEASE PLEASE comment here and ill close this out.

http://www.w3schools.com/php/php_mysql_insert.asp

2
  • 1
    RTFM? php.net/manual/en/mysqli.construct.php what makes you think you can just make up your own argument ordering for a function call? Commented Aug 29, 2016 at 16:15
  • No need to be rude. As mentioned I am new to PHP and was trying to follow w3chools. Commented Aug 29, 2016 at 16:20

1 Answer 1

1

You should follow the proper syntax:

$conn = mysqli_connect(<host Name>, <username>, <password>, <database name>);

Reference: http://php.net/manual/en/function.mysqli-connect.php#refsect1-function.mysqli-connect-examples

Add two more parameters- username and password here:

$username = "root";  // Default values
$password = "";      // Default values

$conn = mysqli_connect($servername, $username, $password, $dbname);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I didn't know that you had to have those defined since I didn't have those set for my database. Looks like that fixed it and I feel like a moron :D thank you!

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.