Difficulty querying two tables in mysql with php -
i have database in mysql , contains 2 tables:
table: political_party +----------+--------------------+-------+ | party_id | party_abbreviation | party | +----------+--------------------+-------+ table: polling_party_result +----+------+-----------------+ | id | p_id | number_of_votes | +----+------+-----------------+
i writing php program outputs form, updates political_party_result table id runs 1 x.. problem i'm facing on form, id relates party_abbreviation column in above political_party table.
that 1(in political_party_result table) should bring out ap(from political_party table) 2 = adc
3 = pdp etc..
here html code:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>html inec</title> </head> <body> <form action="inechp.php" method="post" name="form1"> id: <input name="id" type="text" /> <br /> <br /> polling unit: <input name="pid" type="text" /> <br /> <br /> number of votes: <input name="votes" type="text" /> <br /> <br /> <input type="submit" /> <br /> </form> <form action="inechp.php" method="post" name="form2"> polling unit: <input name="unit" type="text" /> <br /> number of votes: <input name="nov" type="text" /> <br /> <input type="submit" /> </form> </body> </html>
here php code:
<?php $con = mysqli_connect("localhost", "root", "", "inec_results"); mysqli_query($con, "update inec_results.polling_party_result set p_id ='$_post[pid]' , number_of_votes = '$_post[votes]' id = '$_post[id]'"); ?>
forgive me if explanations weren't satisfactory i'm bit new php.
mysqli_query($con, "update inec_results.polling_party_result set p_id ='$_post[pid]' , number_of_votes = '$_post[votes]' id = '$_post[id]'");
this bad number of reasons.
firstly not validating form data have received valid. e.g enter email address id field , pass directly database. leads onto second big issue.
you should never place post data directly sql, andrew mentions insecure , malicious user quite drop entire database sql injection.
having said not sure passing post variables (its been little while since touched php) because entire statement in double quotes. means '$_post[var]' parts being read in part of string literal. (meaning passing value $_post[var] instead of value form. need brace or fullstop vars '{$_post[var]}' or '.$_post[var].'(but again bad!).
what should mentioned used prepared statement after have validated data. can see example here how can prevent sql injection in php?
Comments
Post a Comment