Solved

need help little help  on  snippet php code

Posted on 2014-04-03
7
472 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Domain Service Not Responding 14 30
issue with DB import 1 17
message Alert on an empty search 10 17
Bootstrap 3 - Style a dropdown 1 10
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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