Jump to content

Recommended Posts

Me again i will get the hang of this one day :)

 

I have a form that has 3 fields battleno, asname, amon. I can send the arrays to the next page and echo them out no problem, but this is individually.

 

What i am unable to do is add the other 2 fields into the code below so the code keeps working. I have tried a few things and well i thought it was time to post. I have a few books but there is nothing in them about this. Also searched the internet but there is little that i understand to be honest and the things i did find dont work. battleno is not an array btw the other 2 are.

 

foreach($_POST['asname'] as $row => $assetname)

{

$asname = mysql_real_escape_string($assetname);

$query = "INSERT INTO `assetlist` (`asset`) VALUES ('$asname')";

if(!mysql_query($query))

{

echo"Error";

}

else

{

echo "$row record added<br />";

}

}

mysql_close($con)

 

Hope that makes sense and thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/254938-need-some-help-again/
Share on other sites

I think you want to be looking at multi dimensional arrays (siplified they are arrays within arrays). something like :

$master = array();

foreach ($_POST['asname'] as $key => $value){

$master['asname'][$key] = $value;

}

then use more foreach along with explode to build up a single insert string that you can run on the database.  you could user the $_POST array, but I preffer to leave that alone whenever I can.

cheers. maybe i am unsure to be honest. If i was doing it with out arrays i would have something like.

 

<?

$value1 = $_POST['value1'];

$value2 = $_POST['value1'];

$value3 = $_POST['value1'];

 

$app_sql = mysql_query("INSERT INTO table (value1, value 2, value3)

        VALUES('$value1', '$value2','$value3')")

        or die (mysql_error());

 

if(!$app_sql){

    echo 'There has been an error creating your account. Please contact the webmaster.';

exit();

 

}else{

echo"Done info has been added";

}

 

But with the information coming from arrays well i just get lost.

OK taking the advise above i have made some small headway. However i have a problem i have no idea how to fix.

 

Ok the problem is the array is carrying empty fields. So there is 26 fields and people can fill out anything from 1 of them to 26. If you say fill out 3 of the fields you get a result like.

 

Returned Value 1

Returned Value 2

Returned Value 3

Empty

Empty x 21.

And then it adds 21 empty fields to the database. My question is how do i make it so it only add fields to the database that has content rather than adding the full 26 rows.

 

Hope it makes sense.

 

Cheers for the replys. Muddy_Funster i tried what you said but it generates and error. That it canot unset.

 

Has for the array_filter() that looks just like what i need. I will have a read up and hopefully it will make some sense.

 

Thanks to you both.

 

 

I am getting close well what i class has close lol. However i have 1 problem well 2.

 

$battleno = $_POST['battleno'];

$asname = $_POST['asname'];

$amon = $_POST['amon'];

$amon = (array_filter($amon));

foreach ($asname as $asname)

  {

 

  foreach ($amon as $amon)

 

// {

$query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES ('$battleno' , '$asname' , '$amon')";

if(!mysql_query($query))

{

echo"error";

}

else

{

echo "$amon | $asname record added<br />";

}

 

//}

 

}

mysql_close($con)

 

Doing it like this adds everything fine exsept the amon field it adds only the last number from the array.

 

However

$battleno = $_POST['battleno'];

$asname = $_POST['asname'];

$amon = $_POST['amon'];

$amon = (array_filter($amon));

foreach ($asname as $asname)

  {

 

  foreach ($amon as $amon)

  {

 

$query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES ('$battleno' , '$asname' , '$amon')";

if(!mysql_query($query))

{

echo"error";

}

else

{

echo "$amon | $asname record added<br />";

}

 

}

 

}

mysql_close($con)

 

Doing it with the { } back in add everything fine expect the asname field where it add only the last select item from the array.

 

I realize that the coding is prob all wrong but it sort of works :)

OK, I'll attempt this, even though I am not 100% sure what the data holds.

 

<?php
$battleno = $_POST['battleno']; //integer?
$asname = $_POST['asname']; //array of names that matches the count of the amon array BEFORE taking out empty values?
$amon = $_POST['amon']; //amon array.
$combined = array_combine($asname,$amon); //combine the arrays, so that asname is keys, and amon is values.
foreach ($combined as $asn => $am)
  {
    if(!empty($am)) { $parts[] = "('$battleno' , '$asn' , '$am')"); } //if amon is not empty, add it to the query.
  }
$query = "INSERT INTO `assetlist` (`battleid` , `asset` , `amount`) VALUES " . implode(', ',$parts);
mysql_query($sql) or trigger_error($query . ' has encountered an error: <br />' . mysql_error());

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.