Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!Please help me to fix this, why my script is not working on php7?


What I have tried:

PHP
<?
include ('sqlbase.php');

$file_handle = fopen("keys.txt", "r");
while (!feof($file_handle)) 
{
$line = fgets($file_handle);
$line = trim($line);

$sql = "INSERT INTO `content` 
		   SET
                       `keyword`=\'$line\'";

					   
  $result = mysqli_query($sql) 
      or die('Query error: <code>'.$sql.'<br>'.mysqli_error().'</code>');
}

?>
Posted
Updated 19-Jun-18 1:02am
Comments
[no name] 17-Jun-18 11:54am    
Have a look here, seems to be much more easy: Importing CSV data using PHP/MySQL - Stack Overflow[^]

You did not told us what is not working.

But if the PHP code is not executed at all it is probably due to using the short open tag <? which is not recommended because it requires to be enabled with the PHP: Description of core php.ini directives: short_open_tag - Manual[^].

So either enable that option or (better) use the long open tag <?php.
 
Share this answer
 
Are you sure about all the single quotes everywhere, the \' look weird too. You should check SQL syntax.
PHP
$sql = "INSERT INTO `content` SET `keyword`=\'$line\'";

Not 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[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Let me judge your code.

<?
// does sqlbase.php connect with db?
// if it does what is the variable name for database handle?
// have you selected a database?
include ('sqlbase.php'); // may be you should use include_once in this particular case

$file_handle = fopen("keys.txt", "r");
while (!feof($file_handle)) 
{
$line = fgets($file_handle);
$line = trim($line);

$sql = "INSERT INTO `content` SET `keyword`=\'$line\'"; // you are escaping Single Quote, unnecessary. What would happen if $line has a single quote in it?
// the following query is not good
// INSERT INTO `content` SET `kewyword` = 'word with 'quote';

// This 
	   
  $result = mysqli_query($sql) // reference clearly says you need to pass database handl
      or die('Query error: <code>'.$sql.'<br>'.mysqli_error().'</code>');
}

?>



Follow the procedure style: PHP: mysqli::query - Manual[^]
 
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