Jump to content
Sign in to follow this  
zioarnold

PHP Form with MySQL queries

Recommended Posts

<?php
include "connetti.php";
session_start();
$query = "SELECT id_studente, nome, cognome, anno_maturita, voto_maturita
 		  FROM utenti_studenti
 		  WHERE utenti_studenti.confermato = 0";
$risultato = @mysql_query($query);
if (mysql_num_rows($risultato) == 0) {
    echo("Nessun elemento trovato");
    header("refresh:3;url=AreaAmministratore.phtml");
    exit();
} ?>
<html>
<head>
    <title>Annuario Studenti</title>
    <link rel="stylesheet" href="cssUtils/aggiungi_properties.css"/>
    <script type="text/javascript" src="jsUtils/jsUtils_annuario/annuario_properties.js"></script>
</head>
<body>
<form method='post' action='confermaStudenti.php' name='aggiornaStatoStudente'>
    <div align='center'>
        <table class='tg'>
            <tr>
                <th class='tg' style='color: #000;'>ID</th>
                <th class='tg' style='color: black'>Nome</th>
                <th class='tg' style='color: black'>Cognome</th>
                <th class='tg' style='color: black'>Anno Maturita</th>
                <th class='tg' style='color: black'>Voto Maturita</th>
                <th class='tg' style='color: black'>Accetta</th>
            </tr><?php
            while ($row = @mysql_fetch_assoc($risultato)) {
                echo "
            <tr align='center'>
                <td class='tg' style='color: black'>" . $row['id_studente'] . "</td>
                <td class='tg' style='color: black'>" . $row['nome'] . "</td>
                <td class='tg' style='color: black'>" . $row['cognome'] . "</td>
                <td class='tg' style='color: black'>" . $row['anno_maturita'] . "</td>
                <td class='tg' style='color: black'>" . $row['voto_maturita'] . "</td>
                <td class='tg' style='color: black'>" . "<input type=\"hidden\" name=\"rifiuta\"/><input type=\"checkbox\" name=\"accetta\"/>" . "</td>
            </tr>";
            } ?>
        </table>
    </div>

    <div align='center'>
        <a href='AreaAmministratore.phtml'>Torna indietro.</a>
        <input type='submit' value='Prosegui'/>
    </div>
</form>
</html>

<?php
session_start();
include("connetti.php");
if (isset($_POST["accetta"])) {
    $accetta = 1;
} else {
    $accetta = 0;
}
if ($accetta) {
    $cognome = $_POST['cognome'];
    $nome = $_POST['nome'];
    $id_studente = $_POST['id_studente'];
    $query = "UPDATE utenti_studenti SET confermato='1' WHERE nome='$nome'
AND cognome='$cognome'
AND id_studente='$id_studente'";

    $risultato = @mysql_query($query) or die('<p align="center">Errore!</p>' . mysql_error());
    echo("<script>alert('La modifica eseguita')</script>");
    header("refresh:0;url='AreaAmministratore.phtml'");
} else {
    echo "<script>alert('Errore');</script>";
    header("refresh:0;url='AreaAmministratore.phtml'");
    exit();
}
?>

Greetings guys, could you help me with those codes above? Cuz I can't understand why it doesn't work...

 

So I have an table with the users that not confirmed. So to confirm them I've made checkbox, so on the other side I have a control if the checkbox is checked so I need to update some values on my database right ?

Well it doesn't work.... :(

Share this post


Link to post
Share on other sites

Are you seeing any errors anywhere? Try and remove @ from @mysql_query so that errors are not suppressed.

 

If the columns confermato and id_studente are of type int, you do need single quotes for the value.

 

Side note, if you're going to use MySQL, it's recommended to use MySQLi instead along with prepared statements.

Share this post


Link to post
Share on other sites

You're giving all of your checkboxes the same name and no values, you have no way to determine which one was checked. You're also checking for $_POST['cognome'], $_POST['nome'], and $_POST['id_studente'] which were not submitted with the form.

Share this post


Link to post
Share on other sites

first i suggest you use PDO instead of mysql_ .

  while ($row = @mysql_fetch_assoc($risultato)) {
                echo "
            <tr align='center'>
                <td class='tg' style='color: black'>" . $row['id_studente'] . "</td>
                <td class='tg' style='color: black'>" . $row['nome'] . "</td>
                <td class='tg' style='color: black'>" . $row['cognome'] . "</td>
                <td class='tg' style='color: black'>" . $row['anno_maturita'] . "</td>
                <td class='tg' style='color: black'>" . $row['voto_maturita'] . "</td>
                <td class='tg' style='color: black'>" . "<input type=\"hidden\" name=\"rifiuta\"/><input type=\"checkbox\" name=\"accetta\"/>" . "</td>
            </tr>";
            } 

you are using a static name for the form input while inside a while loop , so if you have 2 results in database they will be 2 inputs named rifuta ,

try to ad a $counter that you increment inside the loop and use that on the input (name="rifiuta1" for the first and rifuta2 for the second )

and yu have to verify those $_post on php side .

Edited by john_jack

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...