Jump to content

[SOLVED] explode help


rachae1

Recommended Posts

Hi I am trying to add comma seperating categories into the database by a form and then exploding them before they are submitted but I am doing the code incorrently and I cant figure it out. At the moment it just adds the word 'array' to the database. Can someone tell me how I do it please.

 

Thanks in advance.

 

if ($submit == 'Addtag') {

   $tags = "tags";
       $tags = explode(",",$_POST['tags']); 

	//begin error reporting
        $error_msg = array();
        if(empty($tags))
        {
            $error_msg[] = "No tags entered!<br />";
        }
        //print errors, if any
        if(count($error_msg)>0)
        {
            echo "<strong>ERROR:</strong><br>n";
            foreach($error_msg as $err)
                echo "$err";
        }
        //else, no errors, insert to the DB!
        else
        {
            $query = mysql_query("INSERT INTO category_tags (tags) VALUES ('$tags')") or die(mysql_error());
            echo "Tags Added!";
        }
    }



Link to comment
https://forums.phpfreaks.com/topic/124087-solved-explode-help/
Share on other sites

Hi I have just changed my code taken from a tutorial it now looks like this :

 

if ($submit == 'Addtag') {
$taginput = $_POST["tags"];
$tagarray = explode(",",$taginput);

for($i=0;$i<count($tagarray);$i++){
$tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i]))));
if($tags == "") continue;
$query = "INSERT INTO category_tags (tags) VALUES ($tags')";
mysql_query($query);
}
23  	Array
24 	Array
25 	Array
}

 

I entered 3 keywords but each of those keywords now says Array instead of the keyword name.

 

My database looks like this

Link to comment
https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640642
Share on other sites

eugh sorry

if ($submit == 'Addtag') {
$taginput = $_POST["tags"];
$tagarray = explode(",",$taginput);

for($i=0;$i<count($tagarray);$i++){
$tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i]))));
if($tags == "") continue;
$query = "INSERT INTO category_tags (tags) VALUES ($tags')";
mysql_query($query);
}

 

Database

 

23  	Array
24 	Array
25 	Array
}

 

Link to comment
https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640643
Share on other sites

Give this a try:

<?php
if($submit == 'Addtag') {
$taginput = $_POST["tags"];
$tagarray = explode(",",$taginput);
$count=count($tagarray);
for($i=0; $i<$count; $i++){
	$tags = mysql_real_escape_string(stripslashes(ltrim(rtrim($tagarray[$i]))));
	if(strlen($tags)>0){
		$query = "INSERT INTO category_tags (tags) VALUES ('$tags')";
		mysql_query($query) or die(mysql_error());
	}
}
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/124087-solved-explode-help/#findComment-640664
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.