View Full Version : What the problem here? PHP CODE
this code to insert new week to Champion when the week id not found in the Champion.
the problem now i can't insert new week i don't know why.
i try many times to fix it but nothing changed.
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if($_POST['curr']!=""){
mysql_query("UPDATE champ_week SET Currant=0 WHERE ID_Champ=".$_GET['id']);
mysql_query("UPDATE champ_week SET Currant=1 WHERE ID_Champ=".$_GET['id']." AND ID_Week=".$_POST['curr']);
}else{
$res_count=mysql_query("SELECT count(*) AS count FROM champ_week WHERE ID_Week='".$_POST['week']."' AND ID_Champ=".$_GET['id']);
$count=mysql_result($res_count,0,"count");
if($count==0){
mysql_query("INSERT INTO champ_week(ID_Champ, ID_week) VALUES (".$_GET['id'].", ".$_POST['week'].")");
echo '<script>alert("TS")</script>';
}else{
echo '<script>alert("This Week already exists before on this Champion")</script>';
}
}
}
?>
HTML CODE
<table width="100%" border="0" cellpadding="0" cellspacing="1">
<tr>
<td align="right" width="96" ><span lang="ar-eg">
<?php if($wtype == "m") {echo "Add Group";} else {echo "Add Week";}?>
:</span></td>
<td align="right">
<?php
$res_week= mysql_query("SELECT * FROM weeks where w_type='$wtype'");
if( mysql_num_rows($res_week) > 0){
?>
<select name="week">
<?php while($wrows = mysql_fetch_array($res_week)){?>
<option value="<?php echo $wrows["ID"];?>" ><?php echo $wrows["Name"];?></option>
<?php }?>
</select>
<?php }?>
<input type="submit" value="Save" /></td>
</tr>
</table>
Neseema M M 06-06-2009, 06:40 AM There is no form tag in the html code given.
If POST method is used how can we get id in $_GET array?
If you want to get the value in select box named 'week' use $_POST['week'].
ie. $id = $_POST['week'];
It is not clear from which field in the form we get the value of 'curr' used in the line
if($_POST['curr']!=""){
If that's your whole form for for submitting, where is the following var defined?
if($_POST['curr']!="")
Also, is the field really "Currant" (a type of berry), or is it supposed to be "Current"?
(I was too slow submitting, and less thorough) :P
Shinary 06-08-2009, 02:49 PM mysql_query("UPDATE champ_week SET Currant=0 WHERE ID_Champ=".$_GET['id']);
BAD IDEA!
Never fully trust your user input like that. In this case you are blindly taking data from $_GET is using it to run a database query. There would be nothing stopping a user from passing in something like:
?id=(DROP TABLE champ_week;)
Don't let a Bobby Tables incident happen, make sure to always sanitize your data before it goes into your database. There are a number of built in functions in PHP to help with this.
ghostweb 06-09-2009, 01:29 PM To avoid a Bobby Drop Tables situation refer to:
http://us3.php.net/mysql_real_escape_string
fiona_mei 06-09-2009, 09:48 PM erm... hello everyone.... i am new here.... n yet, i still looking around in this forum to seek for help....
can anyone help me with this??
<?php
include ("connectdb.php");
include ("closedb.php");
$in_benefit = $_POST['in_benefit'];
$race = $_POST['race'];
$division = $_POST['division'];
?>
<?php
$SQLMale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Male' AND race = '$race' AND division = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_num_rows($rsMale);
?>
<?php
$SQLFemale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);
print "<table border = 1>";
print "<tr>";
print "<th>Division</th>";
print "<th>Race</th>";
print "<th>Male</th>";
print "<th>Female</th>";
print "<th>Total</th>";
print "</tr>";
?>
--> i'll get this error...
--> Warning: mysql_query(): 3 is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\my_project\member_inbenefit.php on line 58
anyone help me?? i am new to php as well..... =)
tim2718281 06-09-2009, 09:52 PM --> Warning: mysql_query(): 3 is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\my_project\member_inbenefit.php on line 58
anyone help me?? i am new to php as well..... =)
Which is line 58?
fiona_mei 06-09-2009, 10:03 PM ooo... i left tat out...
-->$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
fiona_mei 06-09-2009, 10:09 PM forgot to mention this as well...
in my code as posted earlier...
i want to count total of members in my organisation according o their gender and division... tq...
tim2718281 06-09-2009, 11:26 PM ooo... i left tat out...
-->$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
OK, so that's giving you the error message "3 is not a valid MySQL-Link resource".
Have you looked up the documentation for mysql_query ? It's here:
http://uk.php.net/mysql_query
Presumably $dbConn is not being set correctly. So you need to look at the code that's issuing mysql_connect.
fiona_mei 06-10-2009, 12:32 AM i've been using that connection for others function... and it works well.... it just that when it comes to count function, it cannot work well.... =(
larwilliams 06-10-2009, 12:42 AM erm... hello everyone.... i am new here.... n yet, i still looking around in this forum to seek for help....
can anyone help me with this??
<?php
include ("connectdb.php");
include ("closedb.php");
$in_benefit = $_POST['in_benefit'];
$race = $_POST['race'];
$division = $_POST['division'];
?>
<?php
$SQLMale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Male' AND race = '$race' AND division = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_num_rows($rsMale);
?>
<?php
$SQLFemale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);
print "<table border = 1>";
print "<tr>";
print "<th>Division</th>";
print "<th>Race</th>";
print "<th>Male</th>";
print "<th>Female</th>";
print "<th>Total</th>";
print "</tr>";
?>
--> i'll get this error...
--> Warning: mysql_query(): 3 is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\my_project\member_inbenefit.php on line 58
anyone help me?? i am new to php as well..... =)
I think this is your problem:
include ("connectdb.php");
include ("closedb.php");
I assume this has the affect of opening and closing the database connection immediately.
Take "include ("closedb.php");" and move that to the end of your code :)
EDIT: do not include the quotes above :)
rasin 06-10-2009, 12:48 AM there is a line in the code
include ("closedb.php");
what it contains ?
if it contains mysql_close() statement..
then you need to change this code 'include ("closedb.php");' to bottom of the program,because mysql didnt get the pointer after the mysql_close() statement
fiona_mei 06-10-2009, 01:23 AM rasin.... it contains mysql_close...
fiona_mei 06-10-2009, 01:28 AM guys.... i love u all.... =)
i clear the error already.. but i didn't the output.... =|
Neseema M M 06-10-2009, 02:02 AM If you want to get the count of members u can give count function in sql query and take result.
Or you can give select (*) in the query and take mysql_num_rows($result).
If we are giving count function in query and return the number of rows it will be always 1.
fiona_mei 06-10-2009, 04:03 AM erm... since i am new.... i didn't get what u said... huhu.... sorry... can u xplain more...? =|
Neseema M M 06-10-2009, 04:21 AM <?php
$SQLMale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Male' AND race = '$race' AND division = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_num_rows($rsMale);
?>
<?php
$SQLFemale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);
Try below code instead of above:
<?php
$SQLMale = "SELECT (*) FROM membership WHERE gender = 'Male' AND race = '$race' AND division = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_num_rows($rsMale);
?>
<?php
$SQLFemale = "SELECT (*) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);
Or
<?php
$SQLMale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Male' AND race = '$race' AND division = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_result($rsMale, 0);
?>
<?php
$SQLFemale = "SELECT COUNT(referer) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_result($rsFemale, 0);
fiona_mei 06-10-2009, 08:25 PM neseema.... i've tried the above code.... tq =)
but i can't use this as i need to count how many members based on their gender n division...
"$SQLFemale = "SELECT (*) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);"
n i've tried the 2nd option u gave...
tis error pop out...
-->Warning: Wrong parameter count for mysql_num_rows() in C:\Program Files\xampp\htdocs\my_project\member_inbenefit.php on line 58
any other option? =|
Neseema M M 06-11-2009, 12:33 AM Sorry... there is no need of '()' around '*'. So remove that braces.
Try like this:
$SQLFemale = "SELECT * FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_num_rows($rsFemale);
fiona_mei 06-11-2009, 02:19 AM as far as i concern...
select *from will retrieve all the data... not count the data....
is tat right?
Neseema M M 06-11-2009, 02:47 AM as far as i concern...
select *from will retrieve all the data... not count the data....
is tat right?
You will get the count of data using the function mysql_num_rows().
Or
$SQLFemale = "SELECT count(referer) FROM membership WHERE gender = 'Female' AND race = '$race' AND division = '$division'";
$rsFemale = mysql_query($SQLFemale, $dbConn) or die("<center>SQLFemale ERROR</center>");
$female = mysql_fetch_array($rsFemale);
$count = $female['count(referer)'];
echo "Count = ".$count;
Make sure referer field exists in the db.
fiona_mei 06-11-2009, 08:30 PM Neseema...
i am glad tat i learnt a lot from u... =)
but....
i still can't get the ans.... huhu.....=|
<body>
<form action="" method = "POST" name = "in_benefit">
<p class="style6">Get Member In Benefit :
<select name="in_benefit" >
<option value="" selected = "selected">-Choose-</option>
<option value="A">A</option>
<option value="N">N</option>
<option value="Y">Y</option>
</select>
<span class="style6">Race </span>:
<select name="race" class="style6" >
<option value="" selected = "selected">-Choose Race-</option>
<option value="Bumiputera Sarawak">Bumiputera Sarawak</option>
<option value="Chinese">Chinese</option>
<option value="Indian">Indian</option>
<option value="Malay">Malay</option>
<option value="Others">Others</option>
</select>
<span class="style6">Division</span>:
<select name="division" class="style6" >
<option value="" selected = "selected">-Choose Division-</option>
<option value="Betong">Betong</option>
<option value="Bintulu">Bintulu</option>
<option value="Kapit">Kapit</option>
<option value="Kuching">Kuching</option>
<option value="Limbang">Limbang</option>
<option value="Miri">Miri</option>
<option value="Mukah">Mukah</option>
<option value="Samarahan">Samarahan</option>
<option value="Sarikei">Sarikei</option>
<option value="Sibu">Sibu</option>
<option value="Sri Aman">Sri Aman</option>
</select>
<input name="submit" type="submit" value="Submit"/>
</p>
<?php
include ("connectdb.php");
$in_benefit = $_POST['in_benefit'];
$race = $_POST['race'];
$division = $_POST['division'];
$SQLMale = "SELECT COUNT(in_benefit) As NumberOfMembers FROM membership WHERE gender = 'Male' AND `in_benefit` = '$in_benefit' AND `race` = '$race' AND `division` = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_fetch_array($rsMale);
$count = $male ['count(referer)'];
echo "Count = ".$count;
-->here's my complete code... is there any error??
larwilliams 06-11-2009, 08:37 PM Is all that code in one file? If so, it won't work. Put the HTML stuff in one file and the PHP code in a second file, and update the <FORM> tag to call the second file:
example:
<form action="second_file.php" method = "POST" name = "in_benefit">
fiona_mei 06-11-2009, 10:07 PM hmmm.... yeap... 1 file....
i oso do other function under 1 file... but it works well....
foobic 06-11-2009, 11:05 PM $SQLMale = "SELECT COUNT(in_benefit) As NumberOfMembers FROM membership WHERE gender = 'Male' AND `in_benefit` = '$in_benefit' AND `race` = '$race' AND `division` = '$division'";
$rsMale = mysql_query($SQLMale, $dbConn) or die("<center>SQLMale ERROR</center>");
$male = mysql_fetch_array($rsMale);
$count = $male ['NumberOfMembers'];
Apart from that, the code is wide open to SQL injection (http://en.wikipedia.org/wiki/SQL_injection) - it would be dangerous to open it to the public until you fix this. As a general rule, always sanitize user inputs as soon as you can. eg. If your "in_benefit" input can only take the values "A", "N" or "Y", you could use:
$in_benefit_values = array("A", "N", "Y");
$in_benefit = $_POST['in_benefit'];
if (in_array($in_benefit, $in_benefit_values) {
// Valid input - do something
} else {
// Invalid input - tell user to go away / try again / whatever
}
(for extra brownie points, loop through the values in the array to create the select options) ;)
fiona_mei 06-12-2009, 02:37 AM haha... am so happy.... i finally can count the members... without everyone help, i could have failed.... =)
hehe..... erm.... then how can i display the data in the table?
my table--> |division | race | male | female | total |
can anyone help me? =)
|