Solved

need help little help  on  snippet php code

Posted on 2014-04-03
7
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 110

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 110

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

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

696 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