Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert data into the data base every time the php script is run. The connection to database is establishing well but nothing is inserting. Where is the problem?

What I have tried:

PHP
<?php
/** Connecting to database */

include "../includes/dbConn.php";

/** Display errors 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

*/

/** Scan directory for files */

$files = glob('*.mp3');

/** Sort files */

usort ($files, function ($a, $b) {
	return filemtime($a) < filemtime($b);
});

/** Insert list of files to database if they don't exist already */

$i = 0;

while ($files[$i]) {
	$track_name = basename($files[$i]);
	$userIp = "";
	$date = ""; 
	echo $track_name."**";
	
	$addQuery = "INSERT INTO `music_downloads` (id, track_name, ip, date, downloads_number) VALUES (default, '$track_name', NULL, NULL, '0')";
	
	mysqli_query($conn, $addQuery);
	
	$i++;
	
}

?>
Posted
Updated 31-May-20 0:44am
v4
Comments
Richard MacCutchan 31-May-20 4:08am    
You should check the return value from your call to mysqli_query, to see if it succeeded or not.

We can't tell - it's too dependant on your data and existing DB content, which we have no access to.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


But ... don't do DB like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
If the id column is auto-incremented, you do not need to include it in your INSERT statement. Moreover, the download_number column suggests it is holding a number, so you may have to provide it a numeric value, instead of a string.
You should also use a prepared statement to prevent any SQL injection attack:
PHP
$stmt = $conn->prepare("INSERT INTO `music_downloads` (track_name, ip, date, downloads_number) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $track_name, NULL, NULL, 0);
$stmt->execute();
 
Share this answer
 
PHP
$addQuery = "INSERT INTO `music_downloads` (id, track_name, ip, date, downloads_number) VALUES (default, '$track_name', NULL, NULL, '0')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900