Solved

need help little help  on  snippet php code

Posted on 2014-04-03
7
468 Views
Last Modified: 2014-04-03
hello i need your help on a little snippet php code
what i have is a textarea called keyword in this textarea the user wild put several or one only keyword, if there are several keyword it will be seperated by a ','
code will determinated if one or several keyword are used if there are several ones,
insert each one into a table (2 rows ;id, name), id is a numbers which autoincrements, and keep the id into a variable cause it will be use in a other part of the script if there is only one keywrd insert it as well in the table and also keep the id used into a variable, lets call it $used_id


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajoutez une citation</title>

</head>
<body>
<FORM method="post" action="ajout_citations.php">
<TEXTAREA name="keyword" rows=10 COLS=40></TEXTAREA>
<INPUT TYPE="submit" NAME="nom" VALUE=" Envoyer ">
</form></script>

</body></html>

Open in new window




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=2 ;

Open in new window

explain2.jpg
0
Comment
Question by:jerrrrry
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 475 total points
ID: 39974519
This should work:

<?php
$mysqli = new mysqli(SQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME);
$kw = filter_input(INPUT_POST, 'keyword');
if ($kw)
{
	$kwywords = 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);
		}
	}
}

Open in new window

0
 

Author Comment

by:jerrrrry
ID: 39974567
Parse error: parse error in C:\wamp\www\citations\test.php on line 6
are u sure of this?

      $kwywords = explode(',' $kw);
thanks
0
 

Author Comment

by:jerrrrry
ID: 39974594
i tried  
  $kwywords = explode(',' ,$kw);

Open in new window

but then i got Parse error: parse error in C:\wamp\www\citations\test.php on line 11

<?php
$mysqli = new mysqli('localhost', 'root', '', 'citations');
$kw = filter_input(INPUT_POST, 'keyword');
if ($kw)
{
	$kwywords = 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);
		}
	}
}

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 25 total points
ID: 39974640
If you're running the code you posted here, you could not have gotten a parse error on line 11.  You probably got a parse error because of unbalanced parentheses on line 10:

if (!empty($k)

Change it to this:

if (!empty($k))
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39974641
In case you're new to PHP and want to learn something of the basics, this article will help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39974658
oops, I was away. I'm sorry for the typo on the parentheses :)
0
 

Author Comment

by:jerrrrry
ID: 39976154
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question