Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having a little bit of trouble searching through my resulting array from mysql. I have the following code:
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
while($row=$result->fetch_assoc()) {
if(!preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error=true;
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
}
}
}
}
The resulting array has 59 rows and I am trying to match the $STATE pattern to at least one of the rows, otherwise echo an error. The problem is that it matches 1 of the 59 rows but still echos 58 errors because it fails to match the other 58 rows. Is there a way or modifier i can use so that it searchs through all of the rows and if it finds one match then no errors are sent?
Try it
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
$error = true;
while($row=$result->fetch_assoc()) {
if(preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error = false;
}
}
if($error) {
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
}
}
}
Let me know, if there is any issue.
Even if your code is not "bleeding edge", here is what to do to fix it:
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
while($row=$result->fetch_assoc()) {
if(!preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error=true;
}
}
}
}
if ($error) {
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
} else {
echo "OK";
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to create a small quiz where I choose a random question out of a sql and someone has to put in the right answer and then the next question will load.
My problem is that I have button1 for giving out the questions and buton2 to compare it to the answer of the user. But everytime button2 is clicked, another question will load which has another answer of course. Which looks kinda like :
click button 1: What is xy? , click button2: load another question :Who is ..? and compares your input to this queston.
Can I somehow save the question and not give out another random question while clicking on button2 ?
Right now this is my code to compare the answer from the user to the database:
$theme1 = $mysqli->query('SELECT * FROM Qustions WHERE theme = 1 ORDER BY RAND() LIMIT 1');
$line1 = $theme1->fetch_assoc();
//show the question
if (isset($_POST['button1'])) {
echo $line1['question'];
}
if (isset($_POST['button2'])) {
if ($line1['answer']== $_POST['input']) {
echo '<br>You are right !';
}
else if ($_POST['input'] == '') {
echo 'please insert';
}
else {
echo 'you\'re wrong';
}
}
Every time you press the button a new request is created and your data is lost.
You can store the question and the answer in session variables. How sessions work exactly you can read in the PHP manual.
Here is an example of how you can solve this with sessions.
// start session
session_start();
// get a new question and store into session
if (isset($_POST['button1'])) {
// get new data from database
$theme1 = $mysqli->query('SELECT * FROM Qustions WHERE theme =1 ORDER BY RAND() LIMIT 1');
$line1 = $theme1->fetch_assoc();
$_SESSION['question'] = $line1['question'];
$_SESSION['answer'] = $line1['answer'];
echo $_SESSION['question'];
}
// check the answer
if (isset($_POST['button2'])) {
if ($_SESSION['answer'] == $_POST['input']) {
echo '<br>You are right !';
} else if ($_POST['input'] == '') {
echo 'please insert';
} else {
echo 'you\'re wrong';
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i am not really good at this, this is my code. it seems like strlen() is not working, can someone help me figure what is wrong? thanks. it keeps on printing that password is too short even if i put 5 or more letters
<?php
if($username)
{
if(isset($_POST['submit']))
{
$newpass =md5($_POST['newpass']);
$confirmpass = md5($_POST['confirmpass']);
if(strlen($newpass) < 5)
{
if($newpass == $confirmpass)
{
$querychange = mysql_query("UPDATE users SET password = '$newpass' WHERE username = '$username'");
session_destroy();
?> <script> alert ( "Your password has been changed!" );
window.location='login.php'; </script> <?php
}
else
{
?> <script> alert ("New passwords don't match!" ); </script> <?php
}
}
else
{
echo "<font color='red'> * new password too short";
}
}
}
?>
First of all - doing md5 to a string will return a 32-character string.
That means that any password input by user will be hashed to a string of length 32.
This means that if(strlen($newpass) < 5) will never be true and you will always see a warning.
In this string you probably want if(strlen($newpass) > 5) - see, greater.
Then you password will be processed.
So, final code can be:
if(isset($_POST['submit'])) {
if(strlen($_POST['newpass']) >= 5)
{
$newpass = md5($_POST['newpass']);
$confirmpass = md5($_POST['confirmpass']);
// do other stuff
} else {
// warn about short password
}
}
Also take into consideration that strlen on UTF-8 encoded strings may give you unexpected results.
And for passwords you must use better methods then md5. Look at password_hash, password_verify.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My if elseif statements are being triggered every time I hit submit and it is only specific ones. I have checked Stack for similar problems but they all have to do with statements not triggering. I have also tried to validate my form multiple times and no matter what, right or wrong, these specific ones are triggered:
Username: strlen statement
Password: strlen statement
E-Mail: FILTER_VALIDATE_EMAIL
Empty Check is triggered even with all fields filled
Any help with correcting this would really be appreciated.
//Spam Check
if($spam > 2){
$error .= '<div class="problem">Too many accounts have been created from this IP Address. Please contact us if you would like to create more.</div>';
}
//Username Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$user)){
$error .= '<div class="problem">You may only use letters and numbers in your username.</div>';
}
elseif(strlen($user) < 3 OR strlen($user) > 20){
$error .= '<div class="problem">Please use an username between 3 and 20 characters.</div>';
}
elseif($taken > 0){
$error .= '<div class="problem">This username has already been taken. Please choose another.</div>';
}
//Password Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$pass1)){
$error .= '<div class="problem">Do not use special characters in your password.</div>';
}
elseif(strlen($pass1) < 5 OR strlen($pass1) > 20){
$error .= '<div class="problem">Your password must be at least 5 characters and no more than 20.</div>';
}
elseif($pass1 != $pass2){
$error .= '<div class="problem">You did not confirm your passwords correctly. Please make sure they are the same.</div>';
}
//E-Mail Check
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error .= '<div class="problem">The E-Mail Address entered was invalid. Please use another.</div>';
}
elseif(strlen($email) > 50){
$error .= '<div class="problem">This E-Mail Address is too large for us to store. Please use a different one.</div>';
}
//Empty Check
if(empty($user) OR empty($pass1) OR empty($pass2) OR empty($email)){
$error .= '<div class="problem">You need to fill out all fields to create an account.</div>';
}
//Variables
$user = mysqli_real_escape_string($_POST['username']);
$pass1 = mysqli_real_escape_string($_POST['pass1']);
$pass2 = mysqli_real_escape_string($_POST['pass2']);
$email = mysqli_real_escape_string($_POST['email']);
You are using a mysqli function on POST data -- your variables aren't coming from SQL, are they? Use trim() and strip_tags() or something similar to sanitize your data (but kudos for sanitizing to begin with, and using mysqli rather than mysql functions.)
Try something like this for all of your variables and see if your IF statements start evaluating correctly. If they are all NULL, they are probably all returning false right now.
$user = strip_tags(trim($_POST['username']));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
<?php
$text = $_POST['username', 'bio', 'service', 'age'];
if (empty($text )) {
echo "You forgot to fill in a field.";
}
else
echo "Passed.";
?>
I know this seems like a really simple question but I cannot figure this out for the life of me. Thank you a bunch if you guys can figure this out for me.
Parse error: syntax error, unexpected ',', expecting ']' in /home/burtonmo/public_html/v2/add.php on line 2
You can only access one element at a time in an array. Change your code to something like
$username = $_POST['username'];
$bio = $_POST['bio'];
$service = $_POST['service'];
$age = $_POST['age'];
if (empty($username) || empty($bio) || empty($service) || empty($age)) {
echo "You forgot to fill in a field.";
}
else
echo "Passed.";
There are quite a few ways to check variables are set, the way your doing is syntactically incorrect hence the error:
Here is a dynamic way that you can loop through an array of expected keys and check the corresponding $_POST[*]. Good for some situations and not for others:
$form_keys = array('username','bio','service','age');
$errors = array();
//check is POST
if($_SERVER['REQUEST_METHOD']=='POST'){
//loop $form_keys array
foreach ($form_keys as $form_key){
//check the corresponding $_POST[*] - if empty fill error
if(empty($_POST[$form_key])){
$errors[$form_key] = '*required field';
}
}
//error should be empty if all went well
if(empty($errors)){
//good - do something
}else{
//bad - show errors
//$errors contained which fields were empty
}
}
Or you can do it the standard way which is the best way IMO as you can assign different errors can validate based on other types like is_numeric(). Though its obviously a longer piece of code.
$errors = array();
//check is POST
if($_SERVER['REQUEST_METHOD']=='POST'){
//check user
if(empty($_POST['username'])){
$errors['username'] = '*required field';
}
//bio
if(empty($_POST['bio'])){
$errors['bio'] = '*required field';
}
//service
if(empty($_POST['service'])){
$errors['service'] = '*required field';
}
//age
if(empty($_POST['age'])){
$errors['age'] = '*required field';
}
//error should be empty if all went well
if(empty($errors)){
//good - do something
}else{
//bad - show errors
//$errors contained which fields were empty
}
}
Good Luck Hope it helps...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I don't understand why this doesn't work? It's a register form checking if fields are filled in,password is equal to retype password, and if it doesn't already exist in database.
I get this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/a4550840/public_html/newreg.php on line 32
But I already put a ';' at line 32 ... I don't understand why this error occurs.
Any help is appreciated :).
EDIT: Fixed that error ^ and added the mysql_real_escape_string , but it doesn't register the information to the database for some reason?
EDIT: It works now :), took away the quotations from the query
<?php
include ('connect.php');
if ($_POST['submit']) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$repassword = mysql_real_escape_string($_POST['repassword']);
$email = mysql_real_escape_string($_POST['email']);
if ($username && $password && $repassword && $email){
if ($password == $repassword) {
$check = mysql_query("SELECT * FROM members WHERE username='$username' ");
$countrows = mysql_num_rows($check);
if ($countrows == 0){
mysql_query("INSERT INTO members ('username','password','email') VALUES ('$username','$password','$email') ");
} else {
echo 'Username already exists';
}
} else {
echo 'Passwords don'\t match';
}
} else {
echo 'Fill in the fields';
}
} else {
echo 'Register please';
}
?>
You have a problem here:
echo 'Passwords don't match';
You need scape single quote as:
echo 'Passwords don\'t match';
or
echo "Passwords don't match";
NOTE: Your code is vulnerable to sql injection, you should use mysql_real_scape_string() before to pass yours parameters as sql query.
I suggest:
$username = mysql_real_scape_string($_POST['username']);
$password = mysql_real_scape_string($_POST['password']);
$repassword = mysql_real_scape_string($_POST['repassword']);
$email = mysql_real_scape_string($_POST['email']);
TIP: When your are testing (in dev environment) mysql querys, you should combine die(mysql_error()) at the end of line to check if you has a problem like as:
mysql_query('your sql') or die(mysql_error()).
If you have an error, this cause your app die an show the mysql error.
See this reference.
This error shows the earliest time it encounters a problem. The problem is on that line, or on a previous line. In this case you didn't escape a quote, so the parser found the rest of your string while it expected a , or ;. If you look at the colouring of your code, you'll see that more easily. The correct line would be
echo 'Passwords don\'t match';