mysql hulp nodig.

Gestart doorFreestyle15 reacties14 weergaven

16 berichten · pagina 1 van 2

Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
ik ben vrij nieuw met php, mysql.
begin het nu pas te leren.

maar ik loop nu tegen een probleem aan.

ik wil dingen in me database veranderen. via een code.
maar het lukt niet echt,

code wat ik heb:

$sql = mysql_query("SELECT uName FROM leden WHERE uId = " . $_SESSION[uId] . "") or die(mysql_error());
    $fetch = mysql_fetch_assoc($sql);
     
if(isset($_POST[submit]))
{
$auteur =  . $fetch[uName] . ;
$datum = date("d-m-Y H:i");
mysql_query("INSERT INTO nieuws (Ntitel, Nbericht, Ndatum, Nauteur) VALUES 
(" . $_POST[Ntitel] . "," . $_POST[Nbericht] . "," . $datum . "," . $auteur . ")") or die(mysql_error());
echo Het is succesvol toegevoegd.<br />;
}

?>
    
        <form action="admnieuws.php" method="post">
        titel: <input type="text" name="Ntitel" /><br />
        bericht:<br />
        <textarea rows="10" cols="50" name="Nbericht" ></textarea><br />
        <input type="submit" name="submit" />
        </form>
        
        <hr /><br />


zo kan ik dingen toevoegen in me database.

en nu wil ik ze aanpassen.
maar hoe doe ik dit?

ik moet dit met UPDATE doen. maar ik heb geen flauw idee hoe ik het moet gebruiken.

je hoeft de code niet helemaal voor te brouwen (mag wel met uitleg danwink)
als je delen geeft vind ik het ook goed.

ik kan ook een lijst maken om de gegevens uit me db te halen.
dat moet denk ik wel om het te doen?

ik heb gebruikt:
w3schools
phphulp
sitemasters
websitemaken
pfz

dus wie weet het??
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Sorry dubbelpost domme pc -.-
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"UPDATE tabel SET kolom_1 = test, kolom_2 = test, kolom_3 = test WHERE kolom = test"[/COLOR][COLOR= #007700]);[/COLOR]
?>
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
zo ver ben ik ook ja.
maar hoe kies ik uit me db 1 ding??

dat ik een lijst maak van de dingen die er in staan.
en dat ik dan op verwijder druk dat ie dan word verwijdert.
hoe doe ik dat?
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Je kiest een kolom uit je DB via WHERE.

Zo verwijder je.

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM tabel WHERE kolom = test"[/COLOR][COLOR= #007700]);[/COLOR]
?>


en zo haal je ze op

[COLOR= #0000BB]<?php
$select[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM tabel"[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$select[/COLOR][COLOR= #007700])){[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]kolom[/COLOR][COLOR= #007700]];
}[/COLOR]
?>
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
dus:

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM nieuws WHERE (wat moet hier:S)  = Nid"[/COLOR][COLOR= #007700]);[/COLOR]
?>


en hoe doe ik dat in een tabel??

dan doe ik:

[COLOR= #0000BB]<?php
$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM nieuws"[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]))[/COLOR]
[URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM nieuws WHERE ?? = Nid"[/COLOR][COLOR= #007700]);
{[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]""[/COLOR][COLOR= #007700].[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]Ntitel[/COLOR][COLOR= #007700]] .[/COLOR] [COLOR= #DD0000]""[/COLOR][COLOR= #007700];
}[/COLOR]
?>


wat moet ik dan precies doen??

maar hoe voeg ik dat samen?
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Zo zou ik het doen;

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]])){[/COLOR]
    [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM nieuws WHERE id = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700]);    
}[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
    [COLOR= #0000BB]$select[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM nieuws"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<table>
            <tr>
                <td>Titel</td>
                <td>Verwijderen</td>
            </tr>"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$select[/COLOR][COLOR= #007700])){[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<tr>
                <td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]Ntitel[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
                <td><a href=?id="[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]">[x]</a></td>
            </tr>"[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"</table>"[/COLOR][COLOR= #007700];
}[/COLOR]
?>
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
aaah, bedankt!

maar hoe maak ik nu naast echo $row[Ntitel];
een knop. waar op staat: verwijder.
en als ik daar op druk dat ie word verwijdert

ik heb:

if(isset($_POST[submit]))
{
    mysql_query("DELETE FROM nieuws WHERE Ntitel = ".$_POST[Ntitel]."");    
}

    $select = mysql_query("SELECT * FROM nieuws");
    while($row = mysql_fetch_assoc($select))
{
    echo "". $row[Ntitel] ."<button type="button">Verwijder</button>";
    echo "<br /><hr />";
}
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Doe het anders zo:

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]verwijderen[/COLOR][COLOR= #007700]])){[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/is_numeric"]is_numeric[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]])){[/COLOR]
        [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM nieuws WHERE id = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700]);[/COLOR]  
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Gelukt"[/COLOR][COLOR= #007700];
    }[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Geen geldige ID"[/COLOR][COLOR= #007700];
    } 
}[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
    [COLOR= #0000BB]$select[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM nieuws"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<table>
            <tr>
                <td>Titel</td>
                <td>Verwijderen</td>
            </tr>"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$select[/COLOR][COLOR= #007700])){[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<tr>
                <td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]Ntitel[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
                <td><a href=?verwijderen&id="[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]">[x]</a></td>
            </tr>"[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"</table>"[/COLOR][COLOR= #007700];
}[/COLOR]
?>


id is die kolom naam met auto increed. als hij anders heet verander dan even in de goede naam wink of als je er geen een hebt maak ereen aan wink
0
Freestyle

Lid · Topicstarter

Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
BEDANKT!!!!!!!!!!!!!!!!!!!!!!!!!!

1 vraagje
wat betekend: is_numeric??
of eerder gezecht. wat doet: is_numeric
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Freestyle schreef:
BEDANKT!!!!!!!!!!!!!!!!!!!!!!!!!!

1 vraagje
wat betekend: is_numeric??
of eerder gezecht. wat doet: is_numeric
is_numeric is een functie dat kijkt of dat het een getal is, als je bijv bla bla bij id invoert dan zal hij die niet pakken wink je kunt het ook zo doen voor extra beveiliging.

[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #007700]function[/COLOR] in_db[COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700]){[/COLOR]
    [COLOR= #0000BB]$string[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$string[/COLOR][COLOR= #007700];
}[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]verwijderen[/COLOR][COLOR= #007700]])){[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/is_numeric"]is_numeric[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]])){[/COLOR] 
        [COLOR= #0000BB]$aQuery[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM nieuws WHERE id = "[/COLOR][COLOR= #007700].[/COLOR]in_db[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]]).[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700]);[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$aQuery[/COLOR][COLOR= #007700]) ==[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]){[/COLOR]
            [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"DELETE FROM nieuws WHERE id = "[/COLOR][COLOR= #007700].[/COLOR]in_db[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]]).[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700]);[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Nieuws bericht is verwijderd"[/COLOR][COLOR= #007700];
        }[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Helaas is dit geen geldig gebruikersnaam."[/COLOR][COLOR= #007700];
        }
    }[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Geen geldige ID ingevoerd"[/COLOR][COLOR= #007700];
    } 
}[/COLOR][URL="http://www.php.net/else"]else[/URL][COLOR= #007700]{[/COLOR]
    [COLOR= #0000BB]$select[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT * FROM nieuws"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<table>
            <tr>
                <td>Gebruikersnaam</td>
                <td>Verwijderen</td>
            </tr>"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$select[/COLOR][COLOR= #007700])){[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<tr>
                <td>"[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]gebruikersnaam[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]"</td>
                <td><a href=?verwijderen&id="[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]id[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]">[x]</a></td>
            </tr>"[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"</table>"[/COLOR][COLOR= #007700];
}[/COLOR]
?>


Volledige code hoe ik t zou doen.
0
Berichten
18.482
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Doe eens is_numeric veranderen in ctype_digit
0
Berichten
994
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Luukvdo schreef:
Doe eens is_numeric veranderen in ctype_digit
Das beter inderdaad die checkt of dat het een int is.
0
Tgrm

Lid

Berichten
2.133
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dasaque schreef:
Eerdere citaten tonen
Luukvdo schreef:
Doe eens is_numeric veranderen in ctype_digit
Das beter inderdaad die checkt of dat het een int is.
Veel verschil is het, volgens mij alleen met kommagetallen confused
$n = 1234.5;
var_dump(is_numeric($n));  // returns bool(true)
var_dump(ctype_digit($n)); // returns bool(false)
0
Berichten
18.482
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ja, en heb je komma getallen in een id? Meestal niet emo
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren