0

I am using php to insert into Microsoft SQL table and the below is my code:

$server = "**/**,1433";
$connectionInfo = array( "Database"=>"***", "UID"=>"**", "PWD"=>"******" );
$conn = sqlsrv_connect( $server, $connectionInfo );
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {

$sql = "SELECT Item.HQID, Item.ItemLookupCode, Item.Description, Item.ExtendedDescription, Item.SalePrice, Item.Price, Item.CategoryID FROM Item WHERE Item.HQID = '$check'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

 $html_table = '<table border="1" cellspacing="0" cellpadding="2">';

 $html_table .= '<form method="post" action="upload.php"><tr><td width="350" align ="center">' .$row['Description']. '</td><td width="350" align ="center">' .$row['ExtendedDescription']. '</td><td width="130" align="center">' 

.$row['ItemLookupCode']. '<td width="100" align="center">' .$row['SalePrice']. '</td><td width="100" align="center">' .$row['Price']. '</td><td width="200" align="center"><input 

type="text" size="24" name="stitle"></td><td width="100" align="center"><input type="text" size="8" name="wprice"></td><td width="120" align="center"><input type="text" size="10" name="scategory"></td><td width="220" align="center"><input 

type="text" size="28" name="sbody"></td></tr>';
$html_table .= '</table>'; 
echo $html_table;  
}
}
}
}
?>
<input type="submit" value="Upload this page" ></form>

I have an <input> with name="stitle" and I want the PHP code to take values from each <input> but currently it picks up the value just from the first <input>. How do I fix this?

2
  • I would strongly suggest you reformat your HTML - vertical scroll is horrible (hence why web developers and designers spend an age to avoid it). Commented Jan 23, 2013 at 0:07
  • I would also suggest you remove all code from the example that is not required for the question - this extra code begs more questions. Commented Jan 23, 2013 at 0:27

4 Answers 4

5

Your post is horrible formatted, but I think you're looking for the array notation.

You can make something like that:

<input type="text" size="24" name="stitle[]">

The $_POST array field stitle is an array with all values after submitting the form.

Sign up to request clarification or add additional context in comments.

6 Comments

Also worth noting, you can add keys to those arrays too, and the arrays can be multi-dimensional.
Max, how did this not work? $_POST['stitle'] should be an array - try print_r($_POST['stitle'])
Yes SEoF is right..by the way why are you outputting the $html_table beyond the html and body tag?
Don't mind the other part of code, it is just part of code which was deleted while i am posting this question SeoF, can you show me the code which is supposed in "upload.php" thanks....
See my answer for a basic example.
|
2

OK, a simple example of the form, ignoring all extra syntactical mark-up

<pre>
<?php print_r($_POST); ?>
</pre>

<form method="POST" action="">
    <input type="text" size="24" name="stitle[]" />
    <input type="text" size="24" name="stitle[]" />
    <input type="text" size="24" name="stitle[]" />
</form>

Now, anything you enter into those three text boxes will all be returned, and be accessible in the "$_POST" variable.

Comments

0

If you do not want to use the array style inputs [] and you have control over the qty of fields then you can also do the following:

for($f=0; $f < 5; $f++){ 

 echo '<input type="text" name="foo_'.$f.'">';

}

/*-----------server-side------------*/

for($f=0; $f < 5; $f++){ 

 if(isset($_POST['foo_'.$f])){
  //do something
 }

}

Comments

0

You can use this simple code for multiple simple input

$stitle=$_POST['stitle'];
for($j=0;$j<count($stitle);$j++)
{
    echo   $stitle[$j]."<br>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.