I have a small problem. I wrote this script to update the status of my list using a checkbox for each line I want updated. But I (beeing new to this whole thing) can't seem to get my button to do what I want it to do.

<?php

//ansluter till databasen
$opendb=mysql_connect("XXXXX", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("XXXXX") or die(mysql_error());

//skriver information till den angivna tabellen
$today = date("Y-m-d");
$result = mysql_query("SELECT * FROM bills")

or die(mysql_error());

//Tabellstart
echo "<h2>Månadens registrerade räkningar</h2>";
echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
echo "<tr bgcolor='#c3c3c3'><td>Primär mottagare</td><td>Sekundär mottagare</td><td>Förfallodatum</td><td>Belopp</td></tr>";

//Visar räkning som registrerats
while($row = mysql_fetch_array( $result ))
 {
 if ($row['Betald'] <> "on") {
 echo "<tr><td>";
 echo $row['Betald'];
 echo "<tr><td>";
 echo $row['Primar'];
 echo "</td><td>";
 echo $row['Sekundar'];
 echo "</td><td>";
 echo $row['Forfallodatum'];
 echo "</td><td>";
 echo $row['Belopp'];
 echo "</td></tr>";
 $summa +=$row['Belopp'];
}
 }
//Tabellslut
echo "<tr><td colspan=2></td><td>Totalsumma:</td><td>".$summa."</td></tr>";
echo "</table>";

//stänger databasen
mysql_close($opendb);
?>

Anyone got a "quickfix" for me?

Recommended Answers

All 15 Replies

where are the checkboxes?

Crap! Wrong version of the file. Here's how it looks:

<?php

//ansluter till databasen
$opendb=mysql_connect("XXXX", "XXXX", "XXXX") or die(mysql_error());
mysql_select_db("XXXX") or die(mysql_error());

//skriver information till den angivna tabellen
$today = date("Y-m-d");
$result = mysql_query("SELECT * FROM bills")

or die(mysql_error());

//Tabellstart
echo "<h2>Månadens registrerade räkningar</h2>";
echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
echo "<tr bgcolor='#c3c3c3'><td>Betald</td><td>Primär mottagare</td><td>Sekundär 

mottagare</td><td>Förfallodatum</td><td>Belopp</td></tr>";

//Visar räkning som registrerats
while($row = mysql_fetch_array( $result ))
 {
 if ($row['Betald'] <> "Ja") {
 echo "<tr><td>";
 echo "<input type='checkbox' name='betald'>";
 echo "</td><td>";
 echo $row['Primar'];
 echo "</td><td>";
 echo $row['Sekundar'];
 echo "</td><td>";
 echo $row['Forfallodatum'];
 echo "</td><td>";
 echo $row['Belopp'];
 echo "</td></tr>";
 $summa +=$row['Belopp'];
}
 }
//Tabellslut
echo "<tr><td><input type='button' name='update' value='Updatera' onClick='mysql_query

("UPDATE bills")';</td><td colspan=2></td><td>Totalsumma:</td><td>".

$summa."</td></tr>";
echo "</table>";

//stänger databasen
mysql_close($opendb);
?>

your checkbox does not have a value attribute?
Also is your onClick function an Ajax function that is performing that query?
usually you will just submit a form to a php page that will handle that query...

The value is in the tablehead i.e. Betald. I have no Idea what an Ajax function is, sorry. So how would YOU write the button-script and the php-page to update the lines where the checkbox is active?

So you have assigned a function to the button onClick event. You cannot assign php functions to an onClick event.

I think it would be best if you created a form around your table and set it to submit to itself. you would also need some php at the beginning ouf your file to check if the page has been submitted.
e.g.

<?php
    //ansluter till databasen
    $opendb=mysql_connect("XXXX", "XXXX", "XXXX") or die(mysql_error());
    mysql_select_db("XXXX") or die(mysql_error());

    //skriver information till den angivna tabellen
    $today = date("Y-m-d");
    $result = mysql_query("SELECT * FROM bills")
    or die(mysql_error());

    // Check if the page has been submitted
    if ($_POST) {
        // check if the checkbox has been ticked.
        if ($_POST['betald']) {
            // code you want if it has been checked.
        }

        // code to execute if the page has been submitted.
        // this is where you may call mysql_query("UPDATE bills")
        // if you have defined this as a PHP function. you will need to ensure 
        // you have included the file wtih the function or put the code for that here.
    }    

    //Form open tag here
    echo "<form name=\"my_form\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" >";

    //Tabellstart
    echo "<h2>Månadens registrerade räkningar</h2>";
    echo "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
    echo "<tr bgcolor='#c3c3c3'><td>Betald</td><td>Primär mottagare</td><td>Sekundär
    mottagare</td><td>Förfallodatum</td><td>Belopp</td></tr>";
    //Visar räkning som registrerats
    while($row = mysql_fetch_array( $result ))
    {
        if ($row['Betald'] <> "Ja") {
            echo "<tr><td>";
            echo "<input type='checkbox' name='betald'>";
            echo "</td><td>";
            echo $row['Primar'];
            echo "</td><td>";
            echo $row['Sekundar'];
            echo "</td><td>";
            echo $row['Forfallodatum'];
            echo "</td><td>";
            echo $row['Belopp'];
            echo "</td></tr>";
            $summa +=$row['Belopp'];
        }
    }

    // Change the button to a submit button
    //Tabellslut
    echo "<tr><td><input type=\"submit\" value=\"Exec Query\" /></td><td colspan=2></td><td>Totalsumma:</td><td>".
    $summa."</td></tr>";
    echo "</table>";

    // Form end tag here
    echo "</form>";

    //stänger databasen
    mysql_close($opendb);
?>

Hi!
I tried your version. Unfortunately it still doesn't put any value in the field "betald" in my SQL-table. What the value is (boolean, text, "yes", "no", "on", 1, 0) is of little importance, but the rows I check with the checkbox has to be given a value in the column "betald" when I press the button, in order for me to not show them next time.

Where is the code you are using to update the database?

Also so I can test the code I give you can you please give me an output of your database.
In the while loop if you put `echo "<pre>".print_r($row)."</pre>";

if you check it here: http://www.dbinitiative.se/NEW/php/paid.php

Ive made some changes (not regarding to the putting of info in 'Betald' but still. I'm thankful for every bit of help.

If you want to try the change try the post named "Kronofogden" 'cause that bill IS paid (wich is what 'Betald' means)!

:P

I can see the output but I need to see what is happening in the while loop. Can you please provide info as per my previous post.

I solved it in a different way. I put a button for each line and when it's clicked it redirects to another php page wich sets the status as paid and then returns you to the previous page, in wich case the line does no longer show up in the list.

Thanx anyway!

However I have now run in to another speedbump. I'm trying to get a form to retrieve the information in a post in order to change or update said info. Most fields works fine, but the two chekboxes will not show me wether they're checked or not. The checkboxes are there, but it doesn't matter if the value is 1 (checked) or 0 (uncheked), they're uncheked either way.

Here's my code:

<?php

//ansluter till databasen
$opendb=mysql_connect("dbinitiative.se.mysql", "dbinitiative_se", "CDTrack7DVD1") or die(mysql_error());
mysql_select_db("dbinitiative_se") or die(mysql_error());

 //This code runs if the form has been submitted

 if (isset($_POST['submit'])) { 
    $Giro=$_POST['PgBg'];
    $Prim=$_POST['Primar'];
    $Sek=$_POST['Sekundar'];
    $Avbet=$_POST['Avbetalning'];
    $Tot=$_POST['Total'];
    $Forf=$_POST['Forfallodatum'];
    $Ref=$_POST['Ocr'];
    $Bel=$_Post['Belopp'];
    $Adr=$_POST['Adressat'];
    $Bet=$_POST['Betald'];
// echo "<br>Values set for update";
    $updatera = mysql_query("UPDATE bills SET PgBg='$Giro', Primar='$Prim', Sekundar='$Sek', Avbetalning='$Avbet', Total='$Tot', Forfallodatum='$Forf', Ocr='$Ref', Belopp='$Bel', Adressat='$Adr', Betald='$Bet' WHERE Post=$id");


}
 //skriver information till den angivna tabellen

$id =$_GET['id'];
//echo "<br>Id retrieved: ".$id;
$result = mysql_query("SELECT * FROM bills WHERE Post = $id") or die (mysql_error());
echo "<br>Query sent";
while($row = mysql_fetch_array( $result ))
{
    if ($row['Post'] <> $id)
    {
//echo "<br>Id do not match retrieved post";
    }
    else
    {
//echo "<br>Id match retrieved post";

    $Giro=$row['PgBg'];
//echo $Giro."<BR>";
    $Prim=$row['Primar'];
//echo $Prim."<BR>";
    $Sek=$row['Sekundar'];
//echo $Sek."<BR>";
    $Avbet=$row['Avbetalning'];
//echo $Avbet."<BR>";
    $Tot=$row['Total'];
//echo $Tot."<BR>";
    $Forf=$row['Forfallodatum'];
//echo $Forf."<BR>";
    $Ref=$row['Ocr'];
//echo $Ref."<BR>";
    $Bel=$row['Belopp'];
//echo $Bel."<BR>";
    $Adr=$row['Adressat'];
//echo $Adr."<BR>";
    $Bet=$row['Betald'];
//echo $Bet."<BR>";
    }
}

//echo "<br>Values set for form input";
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<h1>Räknings id: #".$id."</h1>";
echo "<TABLE BORDER=1 bordercolor='#3fc9fc' CELLPADDING=1 BGCOLOR='#3FC9FC'>";
echo "<TR><TD ALIGN='LEFT'>Pg/Bg nr:</TD><TD ALIGN='LEFT'><input name='PgBg' type='text' value='".$Giro."'></TD></TR>";
echo "<TR><TD>Primär betalningsmottagare:</TD><TD>Sekundär betalningsmottagare (vid inkasso etc.):</TD></TR>";
echo "<TR><TD><input name='Primar' type='text' value='".$Prim."'></TD><TD><input name='Sekundar' type='text' value='".$Sek."'></TD></TR>";

echo "<TR><TD COLSPAN=2>Avbetalning:<input type='checkbox' name='Avbetalning' Value='".$Avbet."'></TD></TR>";

echo "<TR><TD>Total skuld före inbetalning:</TD><TD><input name='Total' type='text' value='".$Tot."'></TD></TR>";
echo "<TR><TD>Förfallodatum:</TD><TD><input name='Forfallodatum' type='text' value='".$Forf."'></TD></TR>";
echo "<TR><TD>OCR-nummer:</TD><TD>Belopp:</TD></TR>";
echo "<TR><TD><input name='Ocr' type='text' value='".$Ref."'></TD><TD><input name='Belopp' type='text' value='".$Bel."'> Kr</TD></TR>";
echo "<TR><TD>Adressat:</TD><TD><input name='Adressat' type='text' value='".$Adr."'></TD></TR>";

echo "<TR><TD COLSPAN=2>Betald: <input type='checkbox' name='Betald' Value='".$Bet."'></TD></TR>";

echo "</TABLE>";
echo "<P><input type='submit' value='Uppdatera'></P>";
echo "</form>";

//stänger databasen
mysql_close($opendb);
?>

The checkboxes are in my form at the end of the code.

Please advice!

Case closed! I solved it myself. Happy happy!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.