Wat ik heb geprobeerd:
<?php
}else{// Captcha verification is Correct. Final Code Execute here!
$msg="<span style='color:green'>The Validation code has been matched.</span>";
}
}
?>
<?php
$msg= $sql;
$msg= "INSERT INTO students (name, _email, city)
VALUES ('".$_POST["name"]."','".$_POST["email"]."','".$_POST["city"]."')";
?>
Dit ondere is dan wat ik daarboven bij $msg heb ingevuld.
Waarom zet je die in $msg?
$msg is niets meer dan een container waar het bericht wordt geplaatst of de captcha goed/fout is.
Je kan na die $msg regel prima je queries uitvoeren.
Let wel op SQL-injection, als je $_POST, $_GET en en/of $_COOKIE variabelen gaat gebruiken.
Je kan het beste mysqli_real_escape_string() gebruiken in combinatie met de MySQLi-functies
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// hier je controles formulier validatie e.d.
// hier je CAPTCHA dingetje als alles klopt
if($captcha === true)
{
// dit kan.
$msg= "<span style='color:green'>The Validation code has been matched.</span>";
// nu kunnen we alles veilig doen.. hier alleen nog wel wat tegen SQL injection doen, want ander is dit lek
$sql = "INSERT INTO students (name, _email, city)
VALUES ('".$_POST["name"]."','".$_POST["email"]."','".$_POST["city"]."')";
// query uitvoeren
}
}
?>
Als je $sql in $msg zet, dan kan je uren turen naar je scherm, maar dat doet ie niet.
if(isset($_POST['Submit'])){
// code for check server side validation
if(empty($_SESSION['captcha_code'] ) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0){
$msg="<span style='color:red'>The Validation code does not match!</span>";// Captcha verification is incorrect.
}else{// Captcha verification is Correct. Final Code Execute here!
HIER
}
}
?>
$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES ('".$_POST["student_name"]."','".$_POST["student_email"]."','".$_POST["student_city"]."')";
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
// MySQL statement.
$insert = "INSERT INTO table_fruit (orange, apple, grapefruit, banana, watermelon)
VALUES ({$values['orange']}, {$values['apple']}, {$values['grapefruit']}, {$values['banana']}, {$values['watermelon']})";
// MySQL statement to execute the INSERT statement above.
mysqli_query($conn, $insert) or die('<br/>Error reading database: '.mysqli_error($dbconnect));
mysqli_close($conn);
} // End of, if statement from the button check
;
}
}
?>
Je bedoelt student_name, student_email of student_city, ik zie geen index met de naam Students.
In dat geval bestaan die $_POST variabelen niet, omdat die formuliervelden niet bestaan.
Verder mis er beveiliging tegen SQL-injection en ben je nu behoorlijk goed hackbaar:
<?php
$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES(
'".$conn->real_escape_string($_POST["student_name"])."',
'".$conn->real_escape_string($_POST["student_email"])."',
'".$conn->real_escape_string($_POST["student_city"])."'
)";
?>