janim Posted August 6, 2007 Share Posted August 6, 2007 hey guys hello i want to insert checkbox into mysql like when someone insert his e-mail i would give him option to keep his email private by check box how can i insert it into mysql and when i show his information print "you can't see the email" or something many thanks for all Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 you dont insert a check box in DB you insert the result of the chk box ??? like if the user click it put in the db yes else no Quote Link to comment Share on other sites More sharing options...
janim Posted August 6, 2007 Author Share Posted August 6, 2007 thank you for replay but i'm new in php how can i do it thank you again Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 can you give me your code and i will edit it for you Quote Link to comment Share on other sites More sharing options...
janim Posted August 6, 2007 Author Share Posted August 6, 2007 ok this is the check box <input name="Check_hphone" type="checkbox" value="Check_hphone" /> and here it;s my insert code $insert = mysql_query("insert into $table values ('NULL', '".$_POST['name']."', '".$_POST['check_mail']."')") thank you for your pleasure Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 ok i explain it add a field in your db for the hide or not <input name="Check_hphone" type="checkbox" value="Check_hphone" /> value for that should only Y or N to save space heres how to view while($x=mysql_fetch_assoc($query)): if ($x[hideORnot]==y) //show email endwhile; something like that Quote Link to comment Share on other sites More sharing options...
janim Posted August 6, 2007 Author Share Posted August 6, 2007 aha thank you i got the idea but how can i insert Y or NO into database when i tried '".$POST['check_mail']."' i foubd the field empty in mysql so how can i insert it ??? ??? ??? ??? :D ;D Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 do you have a field like that show us the html and your code Quote Link to comment Share on other sites More sharing options...
janim Posted August 6, 2007 Author Share Posted August 6, 2007 include("config.php"); $link=mysql_connect($server,$datauser,$datapass) or die("connection faild becouse".mysql_error()); mysql_select_db($database) or die("No database".mysql_error()); $check = "select id from $table where email = '".$_POST['email']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, this E-mail $email is registered before.";exit;} $insert = mysql_query("insert into $table values ('NULL', '".$_POST['name']."', '".$_POST['email']."', '".$_POST['con']."', '".$_POST['hphone']."', '".$_POST['mphone']."','".$_POST['key']."','".$_POST['sex']."','".$_POST['town']."','".$_POST['position']."','".$_POST['min_salary']."','".$_POST['min_hourly']."','".$_POST['user']."','".$_POST['check_mail']."')") or die("Could not insert data because ".mysql_error()); <input name="check_mail" type="checkbox" value="1"/> Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 have your coding this way include("config.php"); $link=mysql_connect($server,$datauser,$datapass)or die("connection faild becouse".mysql_error()); mysql_select_db($database)or die("No database".mysql_error()); $check = "select id from $table where email = '".$_POST['email']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, this E-mail $email is registered before.";exit;} $insert = mysql_query("insert into $table values ('NULL', '".$_POST['name']."', '".$_POST['email']."', '".$_POST['con']."', '".$_POST['hphone']."', '".$_POST['mphone']."', '".$_POST['key']."', '".$_POST['sex']."', '".$_POST['town']."', '".$_POST['position']."', '".$_POST['min_salary']."', '".$_POST['min_hourly']."', '".$_POST['user']."', '".$_POST['check_mail']."')") or die("Could not insert data because ".mysql_error()); } if you can insert others why not in this field its just the same to check echo the post for that check if it really has a value or echo your query to know if the DB or PHP code is the prob Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 6, 2007 Share Posted August 6, 2007 using post in any mysql statement is bad programming add each condition to a varable and use mysql_reel_escape_string() example <?php $redarrow=mysql_reel_escape_string($_POST['redarrow']); ?> then use the varable $redarrow in the insert Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 ya i agree its not suppose to be done that way Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 6, 2007 Share Posted August 6, 2007 example u need to add the database insert names values ok. <?php include("config.php"); $link=mysql_connect($server,$datauser,$datapass)or die("connection faild becouse".mysql_error()); mysql_select_db($database)or die("No database".mysql_error()); $check = "select id from $table where email = '".$_POST['email']."';"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "Sorry, this E-mail $email is registered before."; exit; } $name=mysql_real_escape_string($_POST['name']); $email=mysql_real_escape_string($_POST['email']); $con=mysql_real_escape_string($_POST['con']); $hphone=mysql_real_escape_string($_POST['hphone']); $key=mysql_real_escape_string($_POST['key']); $sex=mysql_real_escape_string($_POST['sex']); $town=mysql_real_escape_string($_POST['town']); $postion=mysql_real_escape_string($_POST['postion']); $min_salary=mysql_real_escape_string($_POST['min_salary']); $min_hourly=mysql_real_escape_string($_POST['min_hourly']); $user=mysql_real_escape_string($_POST['user']); $check_mail=mysql_real_escape_string($_POST['check_mail']); $insert = mysql_query("insert into $table values ('NULL', '$name', '$email', '$con', '$hphone', '$mphone', '$key', '$sex', '$town', '$position', '$min_salary', '$min_hourly', '$user', '$check_mail' ") or die("Could not insert data because ".mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
janim Posted August 6, 2007 Author Share Posted August 6, 2007 thank you all many thanks :-* Quote Link to comment Share on other sites More sharing options...
Barand Posted August 6, 2007 Share Posted August 6, 2007 aha thank you i got the idea but how can i insert Y or NO into database when i tried '".$POST['check_mail']."' i foubd the field empty in mysql so how can i insert it ??? ??? ??? ??? :D ;D One thing to be aware of with checkboxes - only checked values are posted. If you want Y/N values to write to db then $check_mail = isset($POST['check_mail']) ? 'Y' : 'N' ; and write $check_mail to the db table. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.