0

I am trying to connect to a MySQL database to save information passed via a registration form on my website. for some reason, it won't connect. I have searched in Stack Overflow but can't find the right answer.

Here is my PHP code:

<?php
$con = mysqli_connect("mysite","my username","my password","My database");

if($con === false){
    die("ERROR: Could not connect. ". mysqli_connect_error());
}

$sql = "CREATE TABLE clients(client_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, username CHAR(30) NOT NULL, password CHAR(30) NOT NULL)";

if (mysqli_query($con, $sql)){
    echo "Table clients created successfully";
}
else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}

$user_info = “INSERT INTO clients(username, password) VALUES ( '$_POST["username"]','$_POST["password"]')”;
mysqli_query($con,$user_info);

if (!mysqli_query($con, $user_info)){ 
    die('Error: ' . mysql_error());
}

echo “Your information was added to the database.”;
mysqli_close($con); 
?>

And here is my HTML code:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>

<div class="register">
  <h1>Register Here</h1>
  <form action="register.php" method="post">
    <input type="text" id="username" placeholder="Username" required="required" /><br><br>
    <input type="password" id="password" placeholder="Password" required="required" /><br>
    <button id="btnreg" type="submit">Let Me In</button>
  </form>
</div>


</body>
</html>

When I press the submit button, it just shows me a black page, with no errors, and in phpMyAdmin, the table has not been created and of course the information has not been passed. I'm using GoDaddy hosting if it has any signification for this issue, and i have checked like 25 times that I'm using the correct hostname and database information.

Thanks.

Luca Jung
  • 1,313
  • 10
  • 23
GabMic
  • 1,302
  • 13
  • 30

3 Answers3

0

Try new mysqli instead of mysqli_connect. If this still doesn't work, I'd check to make sure that you're entering the correct values for the username, password, database name, and servername. Also, I'm unsure why you are using this line:

 $sql = "CREATE TABLE clients(client_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, username CHAR(30) NOT NULL, password CHAR(30) NOT NULL)";

^ That will create a new table each time the page is loaded. What you should do is create the table first by running that code in phpmyadmin, and then insert into it with php. Try this:

<?php
    $servername = "your server name";
    $username = "your db username";
    $password = "your DB password";
    $dbname = "name of your database";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $username = $_POST['username'];
    $password = $_POST['password'];
    //As jens pointed out, password is a reserved word in mysql. So change the value of the password column to pass_word or something else.
    $user_info = “INSERT INTO clients(username, pass_word) VALUES ( '".$username"', '".$password"')";
    $conn->query($user_info);
    echo "Your information was added to the database.";
?>

Also add name= to your HTML:

  <form action="register.php" method="post">
    <input type="text" id="username" name="username" placeholder="Username" required="required" /><br><br>
    <input type="password" name="password" id="password" placeholder="Password" required="required" /><br>
    <button id="btnreg" name="btnreg" type="submit">Let Me In</button>
  </form>
BotHam
  • 17
  • 5
  • There shouldn't be any real difference between the procedural and object-oriented style! – FirstOne Jul 31 '16 at 19:24
  • I have tried it, it did not work also. and as i told, i have checked the information many times. its correct. – GabMic Jul 31 '16 at 19:24
  • Are you sure your problem is that it's not connecting, or perhaps your query just isn't being executed? You mentioned it is just showing you a blank page with no errors, so perhaps it is connecting but just not executing the query you want it to? – BotHam Jul 31 '16 at 19:27
0

First of all you need to start using prepered statements. Be aware of SQL njections.

Second you need to change your column name "Password", it is a reserve word and you will get errors.

Third. Your inputs don't have any name, you have assign a id to your inputs but not a name.

<input type="text" id="username" name='username' placeholder="Username" required="required" /><br><br>
<input type="password" id="password" name='password' placeholder="Password" required="required" /><br>

after submiting your form use mysqli_real_escape string().

$username = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,$_POST['password']);
Rafael Shkembi
  • 746
  • 6
  • 16
0

I think it is an empty index problem. I have a solution for you to try:if(isset('$_POST["btnreg"]')){$query = "your query;mysqli_query($con,$query);"}

  • This question is 8 months old. Do you really think your answer adds something that's not already been said? – Martin Mar 30 '17 at 22:00