php - My MySQL database is not updating? -
i'm trying update datebase php archive can't: before ask: names db written correctly , if works too...
<?php if(isset($_get["ans"]) && isset($_get["name"])) { include("con.php"); $con = con(); $answer = $_get["ans"]; $nam = $_get["name"]; if($answer="firefly" ||$answer="firefly" ||$answer="firefly") { $le=2; $sc=50; $update = "update user set level='$le', score='$sc' name = '$nam'"; mysql_query($update,$con); // header("location: lv1b.php?n=$nam"); } else { echo "try again..." ; } } ?>*
this con.php...
<?php function con() { $server="localhost"; $user="root"; $pass=""; $con= mysql_connect($server, $user,$pass); mysql_select_db("games"); return $con; }
your if
statement assigning values, not testing equality, can skip 3 tests using 1 of case conversion functions... instead if
line should be:
if(strtolower($answer)=="firefly") {
next - pointed out in comments - you're using out-of-date mysql library, should rather using mysqli or pdo
also you're wide open sql injection, should escaping value you're using in mysqli see mysqli_real_escape_string. in example means escaping value of $nam
a potential alternative script-fragment (using mysqli , above if
statement changes) might like:
<?php $mysqli = new mysqli("localhost", /*username*/"root", /*pass*/"", /*dbname*/); $name=$mysqli->real_escape_string($_get["name"]); if (strtolower($_get["ans"])=="firefly") { if (!$mysqli->query("update user set level='2',score='50' ". "where name='$name'")) { echo "query failed"; } else { echo "updated" } $mysqli->close(); } else { echo "try again.."; }
Comments
Post a Comment