Link to home
Start Free TrialLog in
Avatar of jerrrrry
jerrrrry

asked on

help in php snippet code;insert into table2, ids from table1

hello i would like to improve my code i have two textareas one called texte and a other called keyword
what the script would do is as long as there are keywords insert it to a table `mots_clefs (rows: id, id is a autoincrement number ,Nom) then insert data into a table citation  (`id`, `auteur`, `texte`, `mots_clefs`), with  in mots_clefs the value of the ids inserted in keywords

table mot_clefs

CREATE TABLE IF NOT EXISTS `mots_clefs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Nom` varchar(80) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

Open in new window


table citations

CREATE TABLE IF NOT EXISTS `citations` (
  `id` int(11) unsigned NOT NULL,
  `auteur` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `texte` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `mots_clefs` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Open in new window




my uncompleted test
$mysqli = new mysqli('localhost', 'root', '', 'citations');


$kw = filter_input(INPUT_POST, 'keyword');
$citation = filter_input(INPUT_POST, 'texte');
if ($kw)
{
	$keywords = explode(',', $kw);
	$ids = array();
	foreach ($keywords as $k)
	{
		if (!empty($k))
		{
			$query = "INSERT INTO mots_clefs (Nom) VALUES('$k')";
			if ($result = $mysqli->query($query) or die("Error: " - mysqli_error()))
				array_push($ids, $mysqli->insert_id);
		}
	}
	$query = "INSERT INTO `citations` (`id`, `auteur`, `texte`, `mots_clefs`) VALUES
(1, '1', 'À elle seule,La vie est une citation.', '$kw')";

	
}

Open in new window


thanks
explain2.jpg
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Notice how you used explode() to break the comma-separated list of keywords apart (line 8)?  PHP has implode() to turn an array into a comma-separated string.  So maybe you want to add this after line 18:
$mots_clefs = implode(',', $ids);

Open in new window

And change line 20 to this:
(1, '1', 'À elle seule,La vie est une citation.', '$mots_clefs')";

Open in new window

And then, of course, you'll want to run that query you created in the $query variable.

I still think you would benefit greatly from taking some time to study PHP instead of just trying to copy code you found on the internet.  In a couple of months of structured study and practice you could put yourself a couple of years ahead of the trial-and-error path.

You may also want to learn how to use the MySQLi Class.  This article has good examples for the most common uses.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you're familiar with MySQL and you're moving to MySQLi, this page on my site has a function mapping across the extensions.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Not sure exactly where you're going with this application (it almost looks like schoolwork) but you might want to consult a data base expert about the design of the citations table.  When I see a column that contains more than one data point, I find myself wondering if that's a design that makes sense.
Avatar of jerrrrry
jerrrrry

ASKER

it doesn't work now
the insert into citation query is not done ?

i don't understand this  
When I see a column that contains more than one data point, I find myself wondering if that's a design that makes sense.

can u explain me what is more than one data point whit a example please, i really don't understand what u mean, tks
We can't write your application for you, so if it does not work you will need to show us the URL link to the web page that has the script installed.  Then we can see the error messages.  And if you can show us the exact code that is running at that URL, we can line up the error messages with the failing lines of code.  In this way we can probably provide more concrete help.

An example of a column with more than one data point is exactly what you have in the table citations and the column mots_clefs

Seriously, I think you will be so much better off if you take some time to work through a structured program of learning, such as those indicated in this article.  You've got a lot of moving parts here and I can tell you don't understand most of them.  There is no shame in ignorance -- we were all new to these technologies once and we all had to learn the basics first.  This should be easier for you and if you get a foundation in how PHP works, then build upon that foundation, you will get better results, I promise.
<?php
$mysqli = new mysqli('localhost', 'root', '', 'citations');


$kw = filter_input(INPUT_POST, 'keyword');
$citation = filter_input(INPUT_POST, 'texte');
if ($kw)
{
	$keywords = explode(',', $kw);
	$ids = array();
	foreach ($keywords as $k)
	{
		if (!empty($k))
		{
			$query = "INSERT INTO mots_clefs (Nom) VALUES('$k')";
			if ($result = $mysqli->query($query) or die("Error: " - mysqli_error()))
				array_push($ids, $mysqli->insert_id);
		}
	}
	$mots_clefs = implode(',', $ids);
	$query2 = "INSERT INTO `citations` (`id`, `auteur`, `texte`, `mots_clefs`) VALUES
(1, '1', 'À elle seule,La vie est une citation.', '$mots_clefs')";
$result2 = $mysqli->query($query2) or die("Error: " - mysqli_error());
                                            

	
}

Open in new window



Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\citations\test.php on line 23


i've begun to read your explaination on php...
Great!  While you read, I'll look over this code snippet.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you rock
Thanks for using EE and thanks for your kind words!  Best, ~Ray