Solved

need help little help  on  snippet php code

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

Accepted Solution

by:
Marco Gasi earned 475 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 25 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 30

Expert Comment

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

Author Comment

by:jerrrrry
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now