Jump to content

Forms By MySQL


angeloghiotto

Recommended Posts

try something like this

$data = mysql_query('query'); // send query to database asking for data
while ($row = mysql_fetch_assoc($data)) // loop through results
{
   $dataArray[] = $row['fieldName']; // put value of field name you want to put in the form item into an array if there are multiple values
}
print('<form><select name="fieldName">');
foreach($dataArray as $value) // loop through data to be put into select dropdown
   print('<option value="'.$value.'"/>'); output values into dropdown options.
print('</select></form>');

Link to comment
https://forums.phpfreaks.com/topic/196358-forms-by-mysql/#findComment-1031208
Share on other sites

thx a lot.. bur. i try to do this ...

 

<?php
include_once 'config.php';
mysql_connect($host,$user,$pass);
mysql_select_db($db);
$sql = "SELECT * FROM `cadastro` LIMIT 0, 30 ";
$data = mysql_query($sql); // send query to database asking for data
echo $data;
while ($row = mysql_fetch_assoc($data)) // loop through results
{
   $dataArray[] = $row['UTE']; // put value of field name you want to put in the form item into an array if there are multiple values
}
print('<form><select name="UTE">');
foreach($dataArray as $value) // loop through data to be put into select dropdown
   print('<option value="'.$value.'"/>'); //output values into dropdown options.
print('</select></form>');
?>

 

and .. when i run... appears this result...

 

imagemyab.png

 

whats happen?

Link to comment
https://forums.phpfreaks.com/topic/196358-forms-by-mysql/#findComment-1031224
Share on other sites

$dataArray is empty. i think. try print_r($dataArray); to see if there is data in $dataArray.

 

you need to verify your data is being taken from the database, and being passed into the array as expected.

you could try using mysql_num_rows($data) to check if the select statement is getting dat from your table. http://au.php.net/manual/en/function.mysql-num-rows.php

is the field name in this line: $dataArray[] = $row['UTE']; correct? letter case is important.

Link to comment
https://forums.phpfreaks.com/topic/196358-forms-by-mysql/#findComment-1031547
Share on other sites

mysql_num_rows() i guess is returning the '2' in the output, which means the query is working, the result data contains two rows from the database.

 

If you used print_r($dataArray) and it is only showing 'Array' in the output, then $dataArray is not filling up correctly. But looking at what you've posted it should be. Did you use print_r($dataArray) or print($dataArray) ? If you used print($dataArray) it may be the foreach loop that is not working. You may need to add braces for the foreach block like:

 

foreach($dataArray as $value)

{

  print('<option value="'.$value.'"/>'); //output values into dropdown options.

}

Link to comment
https://forums.phpfreaks.com/topic/196358-forms-by-mysql/#findComment-1032632
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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