Lee-Bartlett Posted September 19, 2008 Share Posted September 19, 2008 I been looking arround on google for a while now, trying some snipets of code etc and none seem to be right, all i want is when the radio button is checked i want it to come up in my mysql database as somthing. not just a 0, problem with it being a 0 is, unticked or ticked = 0, i would like to make it somthing like 1 for yes and 0 for no, can some one help me my basic form so far <html> <head> </heaD> <body> <form name="form1" method="post" action=""> <table width="418" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="157"> Name:</td> <td width="259"><label for="name"></label> <input type="text" name="name" id="name"></td> </tr> <tr> <td>Email:</td> <td><label for="email"></label> <input type="text" name="email" id="email"></td> </tr> <tr> <td>WiFi Business Name:</td> <td><label for="buissnes_name"></label> <input type="text" name="buissnes_name" id="buissnes_name_name"></td> </tr> <tr> <td>WiFi Location;</td> <td><label for="textfield"></label> <input type="text" name="location" id="location"></td> </tr> <tr> <td>Free or Paid:</td> <td><input type="radio" name="free" id="free" value="free"> <label for="radio"></label> <input type="radio" name="paid" id="paid" value="paid"> <label for="radio2"></label></td> </tr> <tr> <td> </td> <td><label for="button"></label> <input type="submit" name="button" id="button" value="Submit"> <label for="button2"></label> <input type="reset" name="button2" id="button2" value="Reset"> <label for="sub"></label></td> </tr> </table> </form> <?php $sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes) VALUES ('$_POST[name]','$_POST[email]','$_POST[buissnes_name]','$_POST[location]','$_POST[freeyes]','$_POST[paidyes]')"; if (!mysql_query($sql,$connect)) { die('Error: ' . mysql_error()); } echo "1 record added"; Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 i added a bit at the bottom and tried that, didnt work anyone ? <html> <head> </heaD> <body> <form name="form1" method="post" action=""> <table width="418" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="157"> Name:</td> <td width="259"><label for="name"></label> <input type="text" name="name" id="name"></td> </tr> <tr> <td>Email:</td> <td><label for="email"></label> <input type="text" name="email" id="email"></td> </tr> <tr> <td>WiFi Business Name:</td> <td><label for="buissnes_name"></label> <input type="text" name="buissnes_name" id="buissnes_name_name"></td> </tr> <tr> <td>WiFi Location;</td> <td><label for="textfield"></label> <input type="text" name="location" id="location"></td> </tr> <tr> <td>Free or Paid:</td> <td><input type="radio" name="free" id="free" value="free"> <label for="radio"></label> <input type="radio" name="paid" id="paid" value="paid"> <label for="radio2"></label></td> </tr> <tr> <td> </td> <td><label for="button"></label> <input type="submit" name="button" id="button" value="Submit"> <label for="button2"></label> <input type="reset" name="button2" id="button2" value="Reset"> <label for="sub"></label></td> </tr> </table> </form> <?php $sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes) VALUES ('$_POST[name]','$_POST[email]','$_POST[buissnes_name]','$_POST[location]','$_POST[freeyes]','$_POST[paidyes]')"; if (!mysql_query($sql,$connect)) { die('Error: ' . mysql_error()); } echo "1 record added"; $freeyes_status = 'unchecked'; $paidyes_status = 'unchecked'; if (isset($_POST['Submit1'])) { $selected_radio = $_POST['freeyes']; if ($selected_radio = 'freeyes') { $freeyes_status = 'checked'; } else if ($selected_radio = 'paidyes') { $paidyes_status = 'checked'; } } mysql_close($connect) ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 20, 2008 Share Posted September 20, 2008 Are you sure you want to be using radio buttons? Radio buttons only allow you to select one of a given group (one of all with the same name), and once you select it, you can't un-select it. Otherwise, you may want to use checkboxes. Then, the one(s) that are checked would show up in either $_POST['varname'] or $_GET variables after the form is submitted. Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 The first thing I see, is that your radio buttons are not a radio group. They are 2 independent radios that can both be checked. I fixed that part and fixed the labels so they actually do what they are supposed to do. Note that I spelled business_name correctly so you may need to change that in your php code. I changed the radio buttons to the name type, so if you look for $_POST['type'] in your code, you should see one or the other. The values are still free or paid. <html> <head></heaD> <body> <form name="form1" method="post" action=""> <table width="418" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="157"><label for="name">Name:</label></td> <td width="259"><input type="text" name="name" id="name"></td> </tr> <tr> <td><label for="email">Email:</label></td> <td><input type="text" name="email" id="email"></td> </tr> <tr> <td><label for="business_name">WiFi Business Name:</label></td> <td><input type="text" name="business_name" id="business_name"></td> </tr> <tr> <td><label for="location">WiFi Location;</label></td> <td><input type="text" name="location" id="location"></td> </tr> <tr> <td>Free or Paid </td> <td><label>Free <input type="radio" name="type" id="free" value="free"> </label> <label >Paid <input type="radio" name="type" id="paid" value="paid"> </label> </td> </tr> <tr> <td> </td> <td><input type="submit" name="button" id="button" value="Submit"> <input type="reset" name="button2" id="button2" value="Reset"> </td> </tr> </table> </form> Nate Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 what type of data type should it be, atm i got it on tinyint, got suspicion that could be wrong. Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 If you wish to store free or paid in the db then it should be varchar(4) a tinyint is a number and I believe that it is 1 digit long. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 made it a varchar and still nothing has anyone got a tutorial or somthing along them lines, to show me how to get somthing in my db Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 You should be getting an error with this. $_POST[name] should be $_POST['name'] You should not take post data and submit it directly to the database. It opens your server up for sql injection attacks. Do it as $name = addslashes($_POST['name']); Turn on error reporting and I think you will find the error I said. Add this to the top of the php page ini_set('error_reporting', E_ALL); Change the $_POST vars to $vars so your query looks like this. $sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes) VALUES ('$name','$email','$buissnes_name','$location','$freeyes','$paidyes')"; Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 all my things are unidentified varibles with the error code put in. But still i dont see how that helps the radio button input data into the database, didnt put it in when i checked it and left 1 blank Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 The ini_set does not help the database insert, it helps you see errors in your code. ini_set('error_reporting', E_ALL ^ E_NOTICE); // this will show all errors besides notices which are the undefined vars try this.... echo $sql; then take what that outputs and go to phpmyadmin and run that query there. I am sure you will find errors there. Your mysql_error() should return the same error that phpmyadmin give ya. mysql_query($sql,$connect) or die('Error: ' . mysql_error()); Change the if statement to the above and tell me if that gives you an error. Nate Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 i am getting this error from the ini Notice: Undefined variable: paidyes in C:\xampp\htdocs\Website\form.php on line 68 1 record added also i dont see no errors in the query in myadmin Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 I pretty much ignore notices about undefined variables. $thisVar = 1; This will produce a Notice:Undefined Variable error, the E_ALL ^ E_NOTICE in the ini_set should show all errors except notices. Show me the $connect part... star out the username pass & host if you wish, and show the mysql_select_db() part. I can't believe though that your not getting anything from mysql_error(), you should either get an error from that or an error from the ini_set that tells you what is wrong. Show the whole script with the $connect pieces in it as well please. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 <?php ini_set('error_reporting', E_ALL); $db_host = "localhost"; // Your database host server, eg. db.server.com $db_user = ""; // User who has access to the database $db_pass = ""; // User password to access database $db_name = "Roberts_work"; // Existing database name // -- Connecting to the database (not persistent connection) $connect = mysql_connect($db_host, $db_user, $db_pass); mysql_select_db($db_name,$connect) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 Ok, in the page that contains the form, try this at the bottom and post the results here: <?php $result = mysql_query("SHOW TABLES") or die('Error: ' . mysql_error()); echo 'There are '. mysql_num_rows($result) .' tables in the this database'; ?> Nate Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 the form isnt on a seperate page, will that be the problem ? Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 do i try this with all my other php running and before or after the current bit ? Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 with my current code Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Website\form2.php on line 40 with out, i get another radio button and the database doesnt make any entries if i take my old code out if i put ur code before just another radio button, which still doesnt put anything in Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 Ok, now we're getting somewhere. If the $connect part of the script and the form and the PHP is all on the same page and you placed the code i gave you last at the bottom, and your getting this error.... Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Website\form2.php on line 40 Then that means that the connection to the database is failing because the username and/or password your trying to connect with is wrong. In the $connect part, are you defining a username and password in the actual script? I saw that it was blank, but I thought you did that purposely so as not to post your db username and pass on the board. That is all I can tell you for now... Errors are a good thing... at least when something is not working... they tell you what is wrong and you can fix it from there... so define username and password and try it again. Nate Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 i took the username and password out my self, if i take out your code, i feel in the text area of the form i can get what ever i typed in, input into my database, but radio button still come back with a blank space. Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 ok got ur code working with it all i got back was There are 1 tables in the this database Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 Ok, that means that it connected and read the database and gave the number of tables in the db. Remove any extra code that I gave you now, minus the ini_set part. So, now to the next piece. The DB stuff is *correct*, so we need to look at what is being posted. at the top of the page put this... and post back the results <?php echo '<pre>'; print_r($_POST); echo '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 Array ( [name] => => [buissnes_name] => [location] => [type] => paid [button] => Submit ) is it me or are we missing, free and reset? Quote Link to comment Share on other sites More sharing options...
chronister Posted September 20, 2008 Share Posted September 20, 2008 Array ( [name] => => [buissnes_name] => [location] => [type] => paid [button] => Submit ) I am assuming that you did not fill out the form in it's entirety. The free/paid values are one or the other. That is what radio buttons are for. So $_POST['type'] = paid, if you check the other one, it will equal free. If you wish to allow both values to show up, then you will use checkboxes instead. But radio buttons are meant to allow 1 of a group. The reset button is a button and did not get pressed, so the value of it is not going to show up. In your database, you will basically have 1 field to take either free or paid. There is no need to have both. I am assuming that you are looking for one or the other, not both... right?? Quote Link to comment Share on other sites More sharing options...
Lee-Bartlett Posted September 20, 2008 Author Share Posted September 20, 2008 I got my answer it was my db feild which were wrong, it needed to be named typed, and then link typed (radio grp) to my db and then it go the desired effect i needed Chro i really appreciate your help, must of been tedious trying to figure this out. Thank you. 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.