Issues inserting data into MYSQL table via PHP file! help!

I got it working, then slightly changed the syntax, and now it won’t work. No errors tho. :frowning: This is my exact code:

require_once('config/connect.php');

if($link === false){
die("ERROR: Could not connect to DB!. " . mysqli_connect_error());
}

$sql = "INSERT INTO referrals (username, ip, email) VALUES ('$yourname', '$ip', '$friendmail')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

$mysqli->close();

Am I doing something wrong? I’m trying to use object-oriented. Thanks!!

aaand… what did you change?

Use Prepared Statements.
Ask explicit for errors on development environments

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

At a minimum, your php error related settings are not set up to show you errors. You are using $link in the procedural style code, but using $mysqli in the OOP style close() statement. Since it is doubtful you have a connection in both of those variables, you should be getting a php error from one of them. Also, the three variables being put directly into the sql query statement don’t exist in the posted code and should be producing php errors.

You should ALWAYS have php’s error_reporting set to E_ALL (or even better a -1) and set display_errors to ON when learning, developing, and debugging code, and set display_errors to OFF and log_errors to ON when running code on a live/public server. These settings should be in the php.ini on your system so that you don’t need to remember to set/change them in your code and they can be changed at a single point.

Next, if you are going to the trouble of changing all your database code, you should instead learn and use the much simpler PDO extension, use exceptions for errors, and use a prepared query when supplying external/unknown data values to an sql query statement.

Edit: the code you posted, using these three suggestions, would simply be -

require 'pdo_connection.php';

$sql = "INSERT INTO referrals (username, ip, email) VALUES (?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$yourname, $ip, $friendmail]);

echo "Records inserted successfully.";
1 Like

Okay, I figured it out. In my DB settings file the DB connection is referenced as: “mysqli” where I was using “link” in the other php file. I changed it, and it worked perfectly! Thanks! :smiley:

@phdr Okay thanks. I’m already getting used to object oriented so I think I’ll stick with that though. And it’s working now too! :smile:

I’m trying again on this project. I’m using object oriented. Basically, I’m using an HTML form to collect some data, then process it via PHP. The portion below is supposed to insert this data into the DB. It doesn’t work, but produces no errors of any kind. I have no idea what is going on. I’m very new to PHP. Please help! Thanks!

require_once('config/connect.php');

$yourname = mysqli_real_escape_string($link, $_REQUEST['yourname']);
$ip = mysqli_real_escape_string($link, $_REQUEST['ip']);
$yourmail = mysqli_real_escape_string($link, $_REQUEST['yourmail']);
$friendmail = mysqli_real_escape_string($link, $_REQUEST['friendmail']);

$sql = "INSERT INTO node1 (username, ip, yourmail, friendmail) VALUES ('$yourname', '$ip', '$yourmail', '$friendmail')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

$mysqli->close();

That’s not object oriented, that’s procedural, just FYI.

https://www.w3schools.com/php/php_mysql_prepared_statements.asp

And here is the difference: https://www.w3schools.com/php/php_mysql_insert.asp

1 Like

Please read the replies you already have for this problem here - Issues inserting data into MYSQL table via PHP file! help!

Next, you are not using OOP or even mysqli’s OOP notation. The only OOP statement is the $mysqli->close(). The rest of this is procedural mysqli statements.

1 Like

The variables are assigned via an HTML form then processed via PHP. I simply included only the related MYSQL code for simplicity. That particular code is now:

$pdo = new PDO("mysql:host=localhost;dbname=account", "quad1", "Q=+zoldos11~Q"); 

$sql = "INSERT INTO node1 (username, ip, yourmail, friendmail) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$yourname, $ip, $yourmail, $friendmail]);

echo "Records inserted successfully.";

unset($pdo);

It simply produces a blank page and the script breaks. :frowning:

I don’t understand. I tried the PDO method without success, then an object-oriented, again without success. I copied the tutorial I’ve been using precisely, being very careful, and it just doesn’t work. :frowning: :frowning:

I can’t recall how I got it working the first time. I even recreated the original script exactly as before, and nothing… *sighs

Don’t unset $pdo, it isn’t needed.

$pdo = new PDO("mysql:host=localhost;dbname=account", "quad1", "Q=+zoldos11~Q"); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO node1 (username, ip, yourmail, friendmail) VALUES (?, ?, ?, ?)"; 
try {
    $stmt = $pdo->prepare($sql); $stmt->execute([$yourname, $ip, $yourmail, $friendmail]);
} catch (PDOException $e) {
    echo $e->getMessage();
}

Blank php pages can be caused by -

  1. Php parse/syntax errors.
  2. Fatal php runtime errors.
  3. The php language is not installed or is not being invoked when/how the page gets requested.
  1. The php code is not outputting anything at all, is conditionally outputting content and the conditional logic is false, or is outputting content/php non-fatal runtime errors and is performing a header() redirect with php’s output_buffering being turned on.

For items #1 and #2, the errors that get reported and displayed or logged depend on php’s error related settings. The two paragraphs I wrote about the php error related settings need to be followed so that php will help you. Otherwise, you might as well be trying to program with your computer’s screen turned off. For item #1, you cannot put these settings into your code file and have them ‘work’ since the code in any file never runs when there is this type of error. Simply put these settings into the php.ini on your system.

For item #3, are you sure php is installed where you are executing this code, that you are using a URL, such as http://localhost/your_file.php, to request the file on the web server, and that the file has a .php extension? If the raw php code is not being executed on the server, it will be output to the browser. You may need to do a ‘view source’ in the browser to see the raw php code for a blank page.

For item #4, since the code does have an echo statement in it, it would require having all the relevant code needed to reproduce the problem, the form and the form processing code, in order to determine what is the most likely cause of the problem. When you are making the changes to the php.ini for the error related settings, you should also set output_buffering to OFF.

For the PDO connection code, you should set the error mode to exceptions (see the line that astonecipher included in his post above), set emulated prepared queries to false, and set the default fetch mode to assoc. For these later two settings, add this -

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // run real prepared queries
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); // set default fetch mode to assoc
1 Like

Okay, it worked. Thanks very much!!

Also thanks to everyone else for the polite, and kind assistance. :smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service