Jump to content


Photo

(Almost) Working...


  • Please log in to reply
1 reply to this topic

#1 EriRyoutan

EriRyoutan
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 24 April 2006 - 01:54 AM

Okay, I have a working source code (sorry for the lack of comments) That's SUPPOSED to make just about any MySQL database editable with only a bit of information.

Problem being: It dosen't like to add anything to an empty table. (aka only headers, no rows...)

It works pretty well otherwise... ... But... Won't do anything unless there's at least one row.

Any ideas? And I apologize AGAIN for the lack of comments.... If I can help explain any of it, let me know...
<?php
//MySQL Settings...
$host   = " ";
$user   = " ";
$pass   = " ";
$dbname = " ";
$table  = $_GET['table'];
$pathto = " "; //Absolute path to this file... Allows saving of page and updating later. (Backup ability)
?>

<?php
mysql_connect ($host, $user, $pass);
mysql_select_db ($dbname);
if(isset($_POST["newc0"]))
{ 
    $numrows = 0;
    $numcols = 0;
    while(isset($_POST["r" . ++$numrows . "c" . $numcols])){;}
    $numrowsb = $numrows - 1;
    while(isset($_POST["r" . $numrowsb . "c" . ++$numcols])){;}
    $looprow = -1;
    while(++$looprow != $numrows)
    {
        $loopcol = 0;
        $ortest = false;
        while($loopcol != $numcols)
        {
            if($_POST["r" . $looprow . "c" . $loopcol] != $_POST["oldr" . $looprow . "c" . $loopcol++])
            {
                $ortest = true;
            }
        }
        if($ortest)
        {
            $query = "UPDATE $table SET ";
            $loopcol = -1;
            while(++$loopcol != $numcols)
            {
                $query = $query . "`" . $_POST["c" . $loopcol] . "` = '"
                . html_entity_decode($_POST["r" . $looprow . "c" . $loopcol]) . "'";
                if($loopcol != $numcols - 1)
                {
                    $query = $query . ", ";
                }
                else
                {
                    $query = $query . " WHERE ";
                }
            }
            $loopcol = -1;
            while(++$loopcol != $numcols)
            {
                $query = $query . "`" . $_POST["c" . $loopcol] . "` = '"
                . html_entity_decode($_POST["oldr" . $looprow . "c" . $loopcol]) . "'";
                if($loopcol != $numcols - 1)
                {
                    $query = $query . " AND ";
                }
                else
                {
                    $query = $query . " LIMIT 1;";
                }
            }
            mysql_query($query);
        }
    if(isset($_POST["delr$looprow"]))
    {
      $query = "DELETE FROM `$table` WHERE ";
      $loopcol = 0;
            while(++$loopcol != $numcols)
            {
                $query = $query . "`" . $_POST["c" . $loopcol] . "` = '"
                . html_entity_decode($_POST["r" . $looprow . "c" . $loopcol]) . "'";
                if($loopcol != $numcols - 1)
                {
                    $query = $query . " AND ";
                }
                else
                {
                    $query = $query . " LIMIT 1;";
                }
            }
            mysql_query($query);
    }
    }
    $loopcol = 0;
    $ortest = false;
    while($loopcol != $numcols)
    {
        if($_POST["newc" . $loopcol++] != "")
        {
            $ortest = true;
        }
    }
    if($ortest)
    {
        $query = "INSERT INTO $table VALUES ('";
        $loopcol = -1;
        while(++$loopcol != $numcols)
        {
            $query = $query . html_entity_decode($_POST["newc" . $loopcol]);
            if($loopcol != $numcols - 1)
            {
                $query = $query . "','";
            }
            else
            {
                $query = $query . "');";
            }
        }
        mysql_query($query);
    }
    echo "Database Updated...<br>\n";
} 
?>Input Data:<br>
<form action="<?php echo $pathto . "?table=" . $table ?>" method="post"><table border=1>
<tr><?php
$query = "SELECT * FROM $table";
$data = mysql_query($query);
mysql_close();
$numcols = mysql_num_fields($data);
$numrows = mysql_num_rows($data);
$loopcol = -1;
while(++$loopcol != $numcols)
{
    echo "<td>\""
    . mysql_field_name($data, $loopcol)
    . "\" "
    . mysql_field_type($data, $loopcol)
    . ' ('
    . mysql_field_len($data, $loopcol)
    . ')<br>'
    . mysql_field_flags($data, $loopcol)
    . "</td>\n<input type=\"hidden\" name=\"c"
    . $loopcol . "\" value=\""
    . mysql_field_name($data, $loopcol)
    . "\">\n";
}
echo "<td>Del</td></tr>\n\n";
$looprow = -1;
while(++$looprow != $numrows)
{
    echo "<tr>\n";
    $loopcol = -1;
    while(++$loopcol != $numcols)
    {
        echo "<td><input name=\"r{$looprow}c{$loopcol}\" maxlength="
        . mysql_field_len($data, $loopcol)
        . " value=\""
        . htmlentities(mysql_result($data, $looprow, mysql_field_name($data, $loopcol)))
        . "\"></td>\n"
        . "<input type=\"hidden\" name=\"oldr{$looprow}c{$loopcol}\" value=\""
        . htmlentities(mysql_result($data, $looprow, mysql_field_name($data, $loopcol)))
        . "\">\n";
    }
  echo "<td><input type=\"checkbox\" name=\"delr$looprow\"></td>";
    echo "</tr>\n\n";
}

echo"<tr><td colspan={$numcols}><hr>New Data:</td></tr>\n<tr>\n";
$loopcol = -1;
while(++$loopcol != $numcols)
{
    echo "<td><input name=\"newc{$loopcol}\" maxlength="
    . mysql_field_len($data, $loopcol)
    . "></td>\n";
}
?></tr>
</table><input type="submit" value="Update Database"></form>


#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 24 April 2006 - 02:16 AM

I found this script that appears to do what you want. You might peruse it to see how they accomplish that.

[a href=\"http://www.powerweb99.at/index.php?modul=8\" target=\"_blank\"]http://www.powerweb99.at/index.php?modul=8[/a]

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users