I have problem write back to table with UPDATE from mysql_fetch_assoc. I got it to work with INSERT but then it add a new row.

Is there anybody who can help me with the correct syntax?

I heve this query:

    $sql  = "SELECT * FROM oppgave  WHERE modulid=5 AND resultat is NULL ORDER BY RAND() LIMIT 1";

$data = null;
$dataid = null;
$result = mysql_query($sql, $tilkobling); 
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "Svar: " . $nextrow['besvarelse'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
     $data = $nextrow['modulid'];
     $dataid = $nextrow['id'];
}

echo '<form name="input" action="tilretting.php" method="post">';
    echo'Retter<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
    echo '<input type="hidden" name="resultat" value="0">';
    echo 'Godkjent<input type="checkbox" name="resultat" value="1">';
    echo 'modul<input type="text" name="modulid" value="'.$data.'">';
     echo 'id<input type="text" name="id" value="'.$dataid.'">';
    echo '<input class="levermodulknapp" type="submit" name="lever1" value="Send inn retting">';
    echo  "</form>";
    echo "<hr>";

?>

I And this is that i tryed put in the php who is submitted:

     <?php
    include "header.inc.php";
    //in this file the connetion to server
    include "funksjoner.inc.php";

    $correctedby= $_POST['correctedby'];
    $resultat= $_POST['resultat'];
    $data = $nextrow['modulid'];
    $dataid = $nextrow['id'];


    //Step 2: connetion to db
    $tilkobling = kobleTil(); //trenger ikke oppgi databasenavn

    //Steg 3: Kjør en SQL-query
    $sql  = "UPDATE oppgave SET correctedby='".$correctedby."', resultat='".$resultat.", WHERE id='".$dataid."'";   
            mysql_query($sql, $tilkobling);

?>

What am i doing wrong? So grateful for any tip on this one!

有帮助吗?

解决方案

You are doing unnneccessary concatenations and there is an extra comma before the WHERE clause..

The right way..

$sql  = "UPDATE oppgave SET correctedby='$correctedby', resultat='$resultat' WHERE id='$dataid'"; 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top