1

I feel like I'm getting closer to figuring out why PHP is not saving data to my database. I've tried learning PHP and MySQL from numerous tutorials and all have failed me.

So... I feel like there may be something that I haven't been specifying when trying to connect to a MySQL database.

In a recent tutorial that simply outputs text from an input to a table in MySQL, I got an Error stating that the server "localhost" was not found. My Apache has been installed on port 60 (not the default port 80). So I figured that that might be the problem. I tried adding localhost:60 to the mysqli_connect rather than localhost itself and the error disappeared!

There is still a problem though: 1. It takes forever to load the demo.php page (see code below). 2. The data still isn't being added....

Here is the code (I converted it from the original MySQL on the video, to MySQLi and added comments):

demo.php:

   <?php

    define('DB_NAME', 'forms1');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost:60');

    // stored in a variable to TEST if it's working
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_HOST);

    // TEST if a link has been established (connection)
    if (!$link) {
        die('Could not connect:' . mysqli_error($link));
    }
    // same as above
    $db_selected = mysqli_select_db($link,DB_NAME);

    if(!$db_selected) {
        die('Can\t use ' . DB_NAME . ': ' . mysqli_error($link));
    }
    // Check SUCCESS with commented command below
    // echo 'Connected successfully.';

    // stored in a variable to shorten
    $value = $_POST['input1'];

    // stored in a variable to TEST
    $sql = "INSERT INTO demo (input1) VALUES ('$value')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    mysqli_close($link);
?>

demo-form.php:

<form action="demo.php" method="post" />
<p>Input 1: <input type="text" name="input1" /></p>
<input type="submit" value="Submit" />
</form>

I've also had the same problem with another code, see the thread here: PHP database won't save data

I really hope that someone can help me here. It's a shame that I haven't even gotten the basis to work yet...

Thanks!

12
  • 2
    wide open to SQL injection attack Commented Mar 17, 2014 at 20:54
  • 1
    Change all mysql_error to mysqli_error you're mixing SQL functions which, well... don't "mix". ;-) Commented Mar 17, 2014 at 20:54
  • 1
    @Dagon This is for learning purposes, I realize it is vulnerable, Fred-ii- I'll edit the code, thanks. Commented Mar 17, 2014 at 20:57
  • 1
    You're welcome. Fixing those may very well fix your code. I've seen that happen before. Commented Mar 17, 2014 at 20:58
  • 1
    Also change $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_HOST); to $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); you're repeating DB_HOST Commented Mar 17, 2014 at 20:58

2 Answers 2

1

Try this out: (your present code did not work for me) HTML form and PHP/SQL are all-in-one.

<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

if(isset($_POST['submit'])){

    // stored in a variable to shorten
    $value = mysqli_real_escape_string($link,$_POST['input1']);

    // stored in a variable to TEST
    $sql = "INSERT INTO demo (input1) VALUES ('$value')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    else { echo "Success"; }

} // if(isset($_POST['submit']))

    mysqli_close($link);
?>

<form action="" method="post" />
<p>Input 1: <input type="text" name="input1" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
Sign up to request clarification or add additional context in comments.

10 Comments

I got a Success message but nothing was added to the DB. I'm guessing that this has got to be a problem with phpmyadmin or the way I setup my tables.
Your screenshot shows localhost instead of localhost:60 did you use define('DB_HOST', 'localhost); instead of define('DB_HOST', 'localhost:60');? @WalkOfLife
And what about localhost:3306 @WalkOfLife
I don't know if this helps, but phpmyadmin may not have been correctly setup. I have the following notifs: i.imgur.com/SoVBnFS.png
That could very well be the case @WalkOfLife
|
0

MySQL used port 3306 as the default. Does PHP connect directly to the database? If yes, try making your port match.

Did you add permissions to MySQL to allow your app to connect and interact with the database? You should read about GRANT and permissions.

But the comment by Dagon above is a serious one: exposting a database directly to the Internet should only be done if you're willing to have the data stolen, trashed, or both.

3 Comments

I'll read about that and get back to you. This is only for testing purposes and it's not directly exposed to the Internet. I appreciate your concern though.
Under 'Privileges" I have Grant Access to ALL.
So does everyone on the Internet, then. Why not get it over with and put the root password in plain text so we can all see it when we "view source"?

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.