0

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

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by reco21
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}')" );
Votes + Comments
Well done
0

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.

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]."<br>"   ;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.