Jump to content

save array from inputs at mysql as array separated with comma


UnknownPlayer

Recommended Posts

How can i save array from inputs, witch is $igraci and it have 5 values, to mysql base, i tryed but in mysql it shows Array.

This is form:

        <form action="dodaj_klan.php" method="post" >
            <p>
            <label>Naziv klana:</label>
            <input name="naziv" type="text" size="20%" />
            <label>Web Sajt:</label>
            <input name="website" type="text" size="20%" />
            <label>E-Mail:</label>
            <input name="email" type="text" size="20%" />
            <br /><br />
            <label>Igraci klana(5):</label>
            <input name="lider" type="radio" value="1" />
            <input name="igraci[]" type="text" size="20%" /><br />
            <input name="lider" type="radio" value="2" />
            <input name="igraci[]" type="text" size="20%" /><br />
            <input name="lider" type="radio" value="3" />
            <input name="igraci[]" type="text" size="20%" /><br />
            <input name="lider" type="radio" value="4" />
            <input name="igraci[]" type="text" size="20%" /><br />
            <input name="lider" type="radio" value="5" />
            <input name="igraci[]" type="text" size="20%" />
            <label><i>(obelezi lidera klana)</i></label>
            <br /><br />
            <input class="button" type="submit" name="submit" value="Dodaj" />  
            </p>
        </form>

Now i wonna save this igraci[] array in mysql, i tried like this but it doesn't work:

			$igraci = $_POST['igraci'];
		$query = "INSERT INTO klanovi (naziv, website, email, igraci) VALUES ('{$naziv}', '{$website}', '{$email}', '{$igraci}')";
		$result = mysql_query($query, $connection);

 

How can i do this?

Thanks..

Link to comment
Share on other sites

$igraci = implode(',',$_POST['igraci']); //from array to comma separated list.
$query = "INSERT INTO klanovi (naziv, website, email, igraci) VALUES ('{$naziv}', '{$website}', '{$email}', '{$igraci}')";
$result = mysql_query($query, $connection);

Link to comment
Share on other sites

Thanks, and how can i read them from mysql, i mean to read 1 by 1?

With foreach or?

 

Yeah, you  will have to read them with a foreach.  It is not advisable to save them as a comma separated list in mysql.  I didn't go into all of that, but just answered your question.  I have no idea what 'igraci' means, but would think that you should use a separate table to hold those values.  Using a primary key linking back to the 'klanovi' table's row that those values are tied to.

 

This lets you pull a table join to group all of those values.  This would.

A. Make it easier to manipulate the data.

B. Make it easier to delete only one of said values.

C. Further your database design in the the Normal Form.

Link to comment
Share on other sites

Thanks, and how can i read them from mysql, i mean to read 1 by 1?

With foreach or?

 

Yeah, you  will have to read them with a foreach.  It is not advisable to save them as a comma separated list in mysql.  I didn't go into all of that, but just answered your question.  I have no idea what 'igraci' means, but would think that you should use a separate table to hold those values.  Using a primary key linking back to the 'klanovi' table's row that those values are tied to.

 

This lets you pull a table join to group all of those values.  This would.

A. Make it easier to manipulate the data.

B. Make it easier to delete only one of said values.

C. Further your database design in the the Normal Form.

I know, i go now in that way, thanks :)

Link to comment
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.