Jump to content

Getting mysql data into an array


mdmartiny

Recommended Posts

I am in the process of writing code for a dynamic form. I have the part of the form written for information to be entered. I am now trying to write code for the modification of the  information in the table.

 

I know that I want to get the table data my doing a MySql Select and putting that information into an array. I am not very familiar with arrays and have spent some time looking for a solution online with no solution. When I print_r the results I get what I want but when I do anything else I get Array. What I am actually looking to do is make it so that the table data is in another variable

 

this is the section of code that I am working with.

$mod_form = "<form name='insert_table' id='insert_table' action='ad_add.php' method='post' enctype='multipart/form-data'>
<fieldset>
<legend>table information</legend>
<input name='table' TYPE='hidden' VALUE='" . $table . "' />";

$mod_sql = "SELECT * FROM $table WHERE id = '$id'";
$mod_sql_result = mysql_query($mod_sql);

$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$input_array = array();

while ($row = mysql_fetch_array($mod_sql_result, MYSQL_NUM)) {
    $input_array[] = $row;
}

foreach ($input_array as $input => $data) {
    print_r($data);
}

while ($i < mysql_num_fields($mod_sql_result)) {


    $header = str_replace("_", " ", (mysql_field_name($mod_sql_result, $i)));
    $name = mysql_field_name($mod_sql_result, $i);

    (mysql_field_name($mod_sql_result, $i) == 'image' ? $mod_form .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='file' /></p>" : $mod_form .= "<p>" . $header . ": <input name='" . $name . "' id='" . $name . "' type='text' value='" . $input . "'/></p>");

    $i++;
}
$mod_form .= "
<p>
<input type='submit' name='submit_mod' id='submit_mod' />
</p>
</fieldset>
</form>
";

echo $mod_form;

Link to comment
https://forums.phpfreaks.com/topic/261290-getting-mysql-data-into-an-array/
Share on other sites

well do this

<?php
$Get = mysql_query("SELECT.....");
$Info = mysql_fetch_array($Get);
//that sets all the info as an array
//so echoing and choosing the key is simple
echo $Info['keyhere/tablename']; /*that will echo the variable for that key*/

This is just the test database that I am working with. Once this is done I will not know what the column names are going to be. The person that this is for will make all of the fields and names themselves. I am just writing the php code

Here you go

<?php
//get sql
$Sql = "SELECT * FROM {$table} WHERE id = '{$id}'";
//run query
$Query = mysql_query($Sql);
//allowed ext
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
//input array)
$input_array = array();
//$data 
$info = mysql_fetch_array($Query);
foreach ($info as $val) {
$input_array[] = $val;
}
//now start the form
$Form = "<form name=\"insert_table\" id=\"insert_table\" action=\"ad_add.php\" method=\"post\" enctype=\"multipart/form-data\">
<fieldset>
<legend> Table Information </legend>
<input name=\"table\" type=\"hidden\" value=\"{$table}\">";
//run foreach lop now
foreach ($input_array as $key => $val) {
$header = str_replace("_"," ",$key);
$Form .= ($key == 'image' ? "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"file\" /></p>" : "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"text\" value=\"{$val}\" /></p>" );
}
$Form .= "<p>
<input type=\"submit\" name=\"submit_mod\" id=\"submit_mod\" />
</p>
</fieldset>
</form>";
echo $Form;
?>

try

<?php
//get sql
$Sql = "SELECT * FROM {$table} WHERE id = '{$id}'";
//run query
$Query = mysql_query($Sql);
//allowed ext
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
//input array)
$input_array = array();
//$data 
$info = mysql_fetch_array($Query);
//now start the form
$Form = "<form name=\"insert_table\" id=\"insert_table\" action=\"ad_add.php\" method=\"post\" enctype=\"multipart/form-data\">
<fieldset>
<legend> Table Information </legend>
<input name=\"table\" type=\"hidden\" value=\"{$table}\">";
//run foreach lop now
foreach ($info as $key => $val) {
$header = str_replace("_"," ",$key);
$Form .= ($key == 'image' ? "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"file\" /></p>" : "<p>{$header}: <input name=\"{$key}\" id=\"{$key}\" type=\"text\" value=\"{$val}\" /></p>" );
}
$Form .= "<p>
<input type=\"submit\" name=\"submit_mod\" id=\"submit_mod\" />
</p>
</fieldset>
</form>";
echo $Form;
?>

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.