0

I followed this tutorial https://www.youtube.com/watch?v=0BoZc5oUioA to create a connection from a registration form to my database. The php code looks like this:

<?php

$con = mysqli_connect('localhost','root','root');

  if(!$con)
  {
    echo 'Not Connected To Server';
  }

  if(!mysqli_select_db($con,'register'))
  {
    echo 'Database Not Selected'
  }

  $Name = $_POST['username'];
  $Email = $_POST['email'];

  $sql = "INSERT INTO users (Name, Email) VALUES ('$name','$email')";

  if(!mysqli_query($con,$sql))
  {
    echo 'Not Inserted';
  }
  else {
    echo 'inserted';
  }

  header("refresh:2; url=index.htm");

?>

However, every time I try out the registration and the new data should be inserted in the database, instead an empty page is displayed in the browser. The database is called "register" and the table is called "users". Has anybody encountered this problem or knows a solution? Or knows a post where somebody targeted this problem as I could not find anything on google. I am helpful for any suggestions!

EDIT:

Here is the html code part where I call the PHP file:

<form action="insert.php" method="post">

    Name : <input type=text" name="username">
      <br/>
    Email : <input type=text" name="email">
      <br/>
      <input type="submit" value="insert">
</form>
Elina
  • 79
  • 8
  • 1
    Have you got error reporting turned on? That'd be a good place to start. – James Elliott Jun 30 '16 at 20:48
  • Have you run any error logs? What do they say? If you don't have error logs then [read this post](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Martin Jun 30 '16 at 20:49
  • Blank HTML page is typical of a fatal PHP error. Can you check your database to see if intended data was inserted? – Martin Jun 30 '16 at 20:50
  • Not your current issue but this is open to SQL injections. Don't use that tutorial.. – chris85 Jun 30 '16 at 20:50
  • 1
    you are vulnerable to [sql injection atacks](http://bobby-tables.com). and if you get a blank page, then your code is crashing/failing somehow, and you've got all the debug options turned off. – Marc B Jun 30 '16 at 20:52
  • 1
    Do you also realise that PHP variables are case sensitive, so you're setting `$Name` and then trying to insert `$name` – Martin Jun 30 '16 at 20:53
  • @chris85 can you recommend another tutorial? I am completely new to this area – Elina Jun 30 '16 at 20:56
  • 1
    @Elina explore what [Net Tuts](http://code.tutsplus.com/tutorials/search?utf8=%E2%9C%93&search%5Bterms%5D=PHP+MySQL&button=) has to offer, they're a fairly reliable basic standard. – Martin Jun 30 '16 at 20:58
  • @Martin I just inserted the error lines but then instead of a blank page it displays my php code. – Elina Jun 30 '16 at 21:01
  • does it display your code or does it display an error? – Martin Jun 30 '16 at 21:05
  • @Martin it displays my code, not an error – Elina Jun 30 '16 at 21:06
  • did you miss an opening or closing PHP tag somewhere? Can you confirm that PHP is running on your testing machine? – Martin Jun 30 '16 at 21:07
  • @Martin opening and closing tags are all there. Though how can I confirm that PHP is running? – Elina Jun 30 '16 at 21:09
  • `` on the page should show that text and no code – Martin Jun 30 '16 at 21:10
  • @Martin ok, it seems like PHP is not running on my machine. It displays the whole code and not simply the text. Do you know how I can change that? – Elina Jun 30 '16 at 21:15
  • 1
    I personally have not locally installed PHP for years, as I'm used to working on live server environments however you can either get yourself some webspace on a server that runs PHP or do a bit of Googling for `XAMPP PHP local machine install`, but it will involve a bit of jabbing around in windows/mac files, I believe. Not bad but a touch of a fiddle. Also do you understand the scope of what PHP actually is? How it is different from HTML and Javascript etc? Add these sort of things to your reading list to understand it :) – Martin Jun 30 '16 at 21:20
  • If you're using MAMP then you should at least have PHP on your local machine, so you'll have to read the MAMP documentation to find out how to set it up and turn it on. you'll probably need a restart too – Martin Jun 30 '16 at 21:22
  • MAMP normally won't let you install until you have PHP setup... – Nhabbott Jun 30 '16 at 21:45

1 Answers1

2

mysqli_connect can take 4 inputs: host, username, password, database or you could still use mysqli_select_db. It would also be better to use mysql prepared statements to stop SQL Injection. Like so:

<?php

$con = mysqli_connect('localhost','root','root', 'database here');

  if(!$con)
  {
    echo 'Not Connected To Server';
  }

  $Name = $_POST['username'];
  $Email = $_POST['email'];

  $sql = "INSERT INTO users (Name, Email) VALUES (?, ?)";

  mysqli_stmt_bind_param($sql, "ss", $Name, $Email); //Bind the name & email vars to the mysql statement
  $query = mysqli_stmt_execute($sql); //Execute the statement
  mysqli_stmt_close($sql); //Close the mysql statement
  mysqli_close($con); //Close the database connection

  if(!$query)
  {
    echo 'Not Inserted';
  }
  else {
    echo 'inserted';
  }

  header("refresh:2; url=index.htm"); //Remove this if you use the jQuery

?>

If you don't want a page refresh you can send a $.ajax request to this php file using jQuery/JavaScript. Here's a simple example:

$(document).ready(function() {
    $(document).on('submit', '#put button name here', function() {
        var name = $('#input name').val();
        var email = $('#input name').val();

        $.ajax({
            type: 'post',
            url: 'scriptname.php',
            data: {
                name:username,
                email:email,
            },
             success: function(res) {
                 //do whatever here after form success
             }
        });
        return false;
      });
    });
Nhabbott
  • 166
  • 1
  • 3
  • 13
  • remove the select_db calll as that's not needed now it's done in the connection, also check the case of the two input variables `$Name` and `$name` and same for the email, – Martin Jun 30 '16 at 20:56
  • I would like to point out the 4th param, database, is optional and he is selecting a db with `mysqli_select_db`. Lets not confuse the OP as technically it can take up to 6 inputs.. last 2 being very situational – Matt Jun 30 '16 at 20:56
  • True, but it would make it easier to combine the two. – Nhabbott Jun 30 '16 at 21:00
  • Yes but im saying lets not say `mysqli_connect takes 4 inputs` it is insinuating that OP was wrong when all he did was use `mysqli_select_db` instead of putting a db param. – Matt Jun 30 '16 at 21:01
  • I added the missing elements from your code but now it shows the code instead of the blank page and still does not insert any data into the database – Elina Jun 30 '16 at 21:13
  • Could you include in your post how you call the php once the button is clicked? Do you use `action="something.php"`? Also, what are you testing your website with? MAMP, WAMP, etc. @Elina – Nhabbott Jun 30 '16 at 21:15
  • @Elina I missed typed, if you didn't catch it `mysqli_stmt_bind_param($query, "ss", $Name, $Email);` should be `mysqli_stmt_bind_param($sql, "ss", $Name, $Email);` – Nhabbott Jun 30 '16 at 21:23