954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can someone help me with an php/mysql array

What im doing is i have a form with 4 inputs like below. and im using the post method to store the fields values so i can send them to the database. what i neeed is each input to be put into a separate row in a databases table.

also sometimes maybe only 3 of the 4 fields are filled and therefore i would like only three new rows in my table.

Ive managed to get this going but it will always add 4 rows even if some of the input fields were blank.

so idk i think i need an array like idk $links = array();
foreach ( $_POST['links'] as $key => $value)

but im not sure how to achieve this. could someone help.

<table width="100%">
<tr>
        <td width="140" style="padding:4px;">link 1:</td>
        <td ><input type="text" class="edit bk" name="link_one" style="width:350px"></td>
    </tr>
   <tr>
     <td width="140" style="padding:4px;">link 2:</td>
        <td ><input type="text" class="edit bk" name="link_two" style="width:350px"></td>
    </tr>
    <tr>
        <td width="140" style="padding:4px;">link 3:</td>
        <td ><input type="text" class="edit bk" name="link_three" style="width:350px"></td>
    </tr>
   <tr>
        <td width="140" style="padding:4px;">link 4:</td>
        <td ><input type="text" class="edit bk" name="link_four" style="width:350px"></td>
    </tr>
    <tr>
       
    </tr>
</table>

<?php 

$link_one = trim( $db->safesql( $parse->process( $_POST['link_one'] ) ) );
$link_two = trim( $db->safesql( $parse->process( $_POST['link_two'] ) ) );
$link_three = trim( $db->safesql( $parse->process( $_POST['link_three'] ) ) );
$link_four = trim( $db->safesql( $parse->process( $_POST['link_four'] ) ) );


$db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_one}')" );
$db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_two}')" );
$db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_three}')" );
$db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_three}')" );

?>


I need to make this an array so it doesnt add rows when its not needed. also to make it less redundant.

any help please and thanks.

reco21
Light Poster
33 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
 
if (trim($link_one)!="")
    $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_one}')" );

if (trim($link_two)!="")
   $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_two}')" );

if (trim($link_three)!="")
   $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_three}')" );


if (trim($link_four)!="")
   $db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$row}', '{$link_four}')" );
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

That worked very well, thank you.

What if i wanted 60 input fields to create multiple rows. i dont mind the extra html but wondering if its possible to put it in an array. like

if( $_POST['link'] != "" ) {
		
		$links = array ();
		foreach ( $_POST['link'] as $value ) {
			
			$links[] = "('" . $row . "', '" . trim( $value ) . "')";
		}
		
		$db->query( "INSERT INTO " . PREFIX . "_post_links (matchid, link) VALUES('{$lastid}', '{$links}')" );
	
	}


Thanks again.

reco21
Light Poster
33 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
 

you must use html element array

<input type="text" class="edit bk" name="links[]"  id="links[]" style="width:350px">


Here is used [] IN NAMING html

now in php you will get array of links and u can use it as follows

$totaltextbox=count($_POST[links])
for ($i=0;$i<$totaltextbox;$i++)
{
  if(trim($_POST['links'][$i])=="")
       echo $_POST['links'][$i].""   ;
}
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

thanks :) Very informative.

reco21
Light Poster
33 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: