Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.