Kleine Vragen Topic #1

Gestart doorLennard4.284 reacties15.585 weergaven

4.285 berichten · pagina 264 van 286

Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Lennard schreef:
En doe eens wat met de tips die in dit topic gegeven worden, met deze code ga je geheid tegen enorme bugs en andere ellende aanlopen.

- variabelen buiten quotes
- getallen buiten quotes
- fix je uitlijning want dit is echt om te huilen
- werk aan je foutafhandeling en kijk waarom een query fout is i.p.v. random je query te veranderen in de hoop dat het dan wel werkt
Ik had zostraks de variables buiten de quotes alleen dat werkte toen niet. Achteraf bleek dat ik twee keer $ip postte. Zal dus als ik weer thuis ben vanavondnde quotes weghalen.

toevoeging op 19 maart 2013, 10:25:22:
Rik schreef:
Ten eerste test je in je ifs of een string kleiner is dan een andere string. Dat werkt natuurlijk niet. Gebruik hiervoor hetvolgende:
if(strlen($var) < 100) { }


Waarbij de 100 uiteraard de minimale lengte is die je wil hebben.

Bovendien zou ik werken met een error array, en dan als laatste kijken of je errors hebt of niet. Als dit niet het geval is dan pas moet je de gegevens posten.

Nu doe je namelijk het eerste wat ik zeg al fout, daardoor voert ie telkens de else uit, en dat betekend dat hij dus de query probeert uit te voeren.

Dus ik zou voor alles gewoon f(strlen($var) < 100) { } moeten gebruiken? En dan geen miljoen elses zoals ik die nu heb? Dus zoiets: (ben op ipad dus kan het niet even netjes doen)

f(strlen($var) < 100) { }
f(strlen($var) < 100) { }
f(strlen($var) < 100) { }

//hier de query en een echo met dat het gelukt is.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Nee.

Je zult alsnog if/else moeten gebruiken. Maar als je het met een error array doet dan hoef je niet hondermiljoenmiljard else te gebruiken:

[COLOR= #0000BB]<?php
$error[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var2[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var2 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var3[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var3 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var4[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var4 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
{[/COLOR]
    [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700])
    {[/COLOR] 
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700];
    }
}[/COLOR]
[URL="http://www.php.net/else"]else[/URL]
[COLOR= #007700]{[/COLOR]
    [COLOR= #FF8000]// Query en succesmessage[/COLOR]
[COLOR= #007700]}[/COLOR]
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Nee.

Je zult alsnog if/else moeten gebruiken. Maar als je het met een error array doet dan hoef je niet hondermiljoenmiljard else te gebruiken:
[COLOR= #0000BB]<?php
$error[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var2[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var2 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var3[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var3 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var4[/COLOR] [COLOR= #007700]==[/COLOR] false[COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Var4 is niet goed.[/COLOR][COLOR= #007700];
}[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
{[/COLOR]
    [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700])
    {[/COLOR] 
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700];
    }
}[/COLOR]
[URL="http://www.php.net/else"]else[/URL]
[COLOR= #007700]{[/COLOR]
    [COLOR= #FF8000]// Query en succesmessage[/COLOR]
[COLOR= #007700]}[/COLOR]


Hartstikke bedankt, ziet er al stukken beter uit. Zal vanavond ook eens wat meer naar de uitlijningen kijken. Het is inderdaad een grote mess. emo
toevoeging op 19 maart 2013, 11:39:36:
Enkel moet ik dan == false vervangen door < 10 bijvoorbeeld
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Uiteraard moet je die if-condities vervangen door eigen if-condities. In jouw geval dus werken met [func]strlen()[/func]
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php
$error[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
                    
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/strlen"]strlen[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$mcname[/COLOR][COLOR= #007700]) <[/COLOR] [COLOR= #DD0000]"5"[/COLOR][COLOR= #007700]) {[/COLOR] 
    [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Je hebt een ongeldige Minecraft naam ingevuld![/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
{[/COLOR]
    [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700])
{[/COLOR] 
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700];
}
}[/COLOR]
[URL="http://www.php.net/else"]else[/URL]
[COLOR= #007700]{[/COLOR]
    mysqli_query[COLOR= #007700]([/COLOR][COLOR= #0000BB]$con[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]"UPDATE servers set votes = votes +1 WHERE id = $serverid"[/COLOR][COLOR= #007700]);[/COLOR]
    mysqli_query[COLOR= #007700]([/COLOR][COLOR= #0000BB]$con[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]"INSERT INTO votes (name, ip, date) VALUES ($mcname, $ip, $date)"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Je hebt succesvol gestemd op <strong>"[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]title[/COLOR][COLOR= #007700]] .[/COLOR] [COLOR= #DD0000]"</strong>"[/COLOR][COLOR= #007700];
}[/COLOR]
?>


Heb ik momenteel. Enkel insert hij ook bij lager dan 5 de gegevens gewoon in de database. Hmm
0
Berichten
3.112
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
if(strlen($mcname) < 5) {
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Perryz schreef:
if(strlen($mcname) < 5) {
Ohja, tuurlijk. Had ik kunnen weten clownAlleen doet hij nu nog precies hetzelfde, hij voert de query gewoon uit terwijl de naam niet eens 5 tekens is (of zelfs leeg).
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Janm schreef:
Eerdere citaten tonen
Perryz schreef:
if(strlen($mcname) < 5) {
Ohja, tuurlijk. Had ik kunnen weten clownAlleen doet hij nu nog precies hetzelfde, hij voert de query gewoon uit terwijl de naam niet eens 5 tekens is (of zelfs leeg).
De S doet het hem

$error vs $errors
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Nielsie schreef:
Eerdere citaten tonen
Janm schreef:
Eerdere citaten tonen
Perryz schreef:
if(strlen($mcname) < 5) {
Ohja, tuurlijk. Had ik kunnen weten clownAlleen doet hij nu nog precies hetzelfde, hij voert de query gewoon uit terwijl de naam niet eens 5 tekens is (of zelfs leeg).
De S doet het hem

$error vs $errors
Aah, inderdaad. Enkel geeft hij nu wel gewoon wit weer in plaats van Je hebt een ongeldige Minecraft naam ingevuld! yummie
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Janm schreef:
Eerdere citaten tonen
Nielsie schreef:
Eerdere citaten tonen
Janm schreef:
Eerdere citaten tonen
Perryz schreef:
if(strlen($mcname) < 5) {
Ohja, tuurlijk. Had ik kunnen weten clownAlleen doet hij nu nog precies hetzelfde, hij voert de query gewoon uit terwijl de naam niet eens 5 tekens is (of zelfs leeg).
De S doet het hem

$error vs $errors
Aah, inderdaad. Enkel geeft hij nu wel gewoon wit weer in plaats van Je hebt een ongeldige Minecraft naam ingevuld! yummie
Welke code heb je nu?
En zeker dat je overal $errors hebt?
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Ik heb deze code:

[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]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]submit[/COLOR][COLOR= #007700]])) {[/COLOR]
                    [COLOR= #0000BB]$mcname[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]mcname[/COLOR][COLOR= #007700]];[/COLOR]
                    [COLOR= #0000BB]$ip[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/getenv"]getenv[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"REMOTE_ADDR"[/COLOR][COLOR= #007700]);[/COLOR] 
                    [COLOR= #0000BB]$date[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/date"]date[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"Ymd"[/COLOR][COLOR= #007700]);[/COLOR] 
                    
                    [COLOR= #0000BB]$error[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
                                        
                    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/strlen"]strlen[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$mcname[/COLOR][COLOR= #007700]) <[/COLOR] [COLOR= #0000BB]5[/COLOR][COLOR= #007700]) {[/COLOR]
                        [COLOR= #0000BB]$error[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #DD0000]Je hebt een ongeldige Minecraft naam ingevuld![/COLOR][COLOR= #007700];
                    }[/COLOR]
                    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$error[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
                    {[/COLOR]
                        [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$errors[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$error[/COLOR][COLOR= #007700])
                        {[/COLOR] 
                            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<h1>Oops...</h1>"[/COLOR][COLOR= #007700];[/COLOR]
                            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$error[/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]"<h1>Je hebt gestemd op deze server!</h1>"[/COLOR][COLOR= #007700];[/COLOR]
                        mysqli_query[COLOR= #007700]([/COLOR][COLOR= #0000BB]$con[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]"UPDATE servers set votes = votes +1 WHERE id = $serverid"[/COLOR][COLOR= #007700]);[/COLOR]
                        mysqli_query[COLOR= #007700]([/COLOR][COLOR= #0000BB]$con[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]"INSERT INTO votes (name, ip, date) VALUES ($mcname, $ip, $date)"[/COLOR][COLOR= #007700]);[/COLOR]
                        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Je hebt succesvol gestemd op <strong>"[/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]title[/COLOR][COLOR= #007700]] .[/COLOR] [COLOR= #DD0000]"</strong>"[/COLOR][COLOR= #007700];
                    }[/COLOR]
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
foreach($errors as $error)

Je errors staan in $error en jij zegt: Pak $error uit $errors
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
foreach documentatie bekijken crycry..
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Raaoul schreef:
foreach documentatie bekijken crycry..
Had gekund, maar ik dacht die raaoul die weet een heleboel emoemo
Maar ik heb het nu werkend Nielsie & Rik, bedankt thumbsup
0
Berichten
11.263
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ik ga me weer verdiepen in MySql vanaf het begin, omdat ik nu echt serieuze systemen wil gaan maken!

Maar ik heb een klein vraagje, is deze tutorial goed

http://phptuts.nl/view/41/

Want die dateerd vanuit 2008!

Het liefst heb ik een Nederlandstalige, en deze staat me echt wel aan. Alleen dit klein vraagje dus nog!

Of die goed is...?
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