Jump to content

Archived

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

EriRyoutan

(Almost) Working...

Recommended Posts

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...
[code]
<?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>
[/code]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

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.