/    Sign up×
Community /Pin to ProfileBookmark

Radio buttons and editing MYSQL

Ok, so I made a registering script for my page. It enters data fine. No I want to add radio buttons, and depending on what radio button is selected, I want certain fields in the ‘users’ table in my mysql database to be changed.

I have got what I would think could work, but sadly, it doesn’t do the editing I want. The default values for each field is ‘1’, however if lets say radio button 1 is selected, I want field 3 to change to 5. (Out of lets say in this case, 7 fields, all named field1, field2, field3, etc) on top of that then I have the username being added to the database, and email, and password (in md5) I have the password, email, and username being added, and then all the fields are added fine as ‘1’ but they don’t change to the 5 or whatever I want. Any idea on what I could be doing wrong?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 05.2009 — You would need to post some code for someone to try to figure out. The explanation you gave didn't make much sense to be but I bet we could decipher a block of code a little easier.
Copy linkTweet thisAlerts:
@Nate101authorJul 05.2009 — Sorry, here's basically the two pages it's based on


[CODE]<?
// process.php

$mode = $_POST['mode']; // Make sure a mode is specified.
if ($mode == "") {
print("No mode specified. Please contact the webmaster to have this problem fixed.");
die();
}
require("vars.php"); // Get mysql info.
mysql_connect($host, $user, $pass) or die("MySQL connection error.");
mysql_select_db($db) or die("No such database."); // Connect to the right database
switch ($mode) {
case "register";

// First, check to make sure that all the data is filled in.
$username = $_POST['username']; // Min 4, Max 16
$password = $_POST['password']; // Min 4, Max 10
$cpassword = $_POST['cpassword']; // Min 4, Max 10
$email = $_POST['email']; // Min 4, Max 40

if ($username == "") {
header("Location: register.php?error=1&email=$email");
die();
}
if ($password == "") {
header("Location: register.php?error=2&email=$email&username=$username");
die();
}
if ($cpassword == "") {
header("Location: register.php?error=3&email=$email&username=$username");
die();
}
if ($email == "") {
header("Location: register.php?error=4&username=$username");
die();
}
// Check to see if the passwords match
if ($password != $cpassword) {
header("Location: register.php?error=5&username=$username&email=$email");
die();
}
// Now check the minimum lengths
// Check the maximum lengths
if (strlen($username) > 15) {
header("Location: register.php?error=9&username=$username&email=$email");
die();
}
if (strlen($password) > 50) {
header("Location: register.php?error=10&username=$username&email=$email");
die();
}
if (strlen($email) > 40) {
header("Location: register.php?error=11&username=$username&email=$email");
die();
}
// Now check to make sure that we don't have the same username.
$temp = strtolower($username);
$query = mysql_query("SELECT COUNT(LOWER(username)) FROM users WHERE username='$temp'");
$result = mysql_fetch_array($query, MYSQL_NUM);
if ($result[0] != 0) {
die();
}
// Check for duplicate email addresses.
$temp = strtolower($email);
$query = mysql_query("SELECT COUNT(LOWER(email)) FROM users where email='$email'");
$result = mysql_fetch_array($query, MYSQL_NUM);
if ($result[0] != 0) {
die();
}
// We have completed all the checks.
// MD5 the password.
$password = md5($password);
// Insert the data into the user page.
mysql_query("INSERT INTO users (username, password, email) VALUES ('$username','$password','$email)");
mysql_query("INSERT INTO other (current,possible) VALUES ($current,$energypossible)");
mysql_query("INSERT INTO item2 (current,bank23) VALUES ('$current2','$bank23')");
// Redirect to login page
}
if ( isSet($_POST['submit']) )
{

echo '<pre>';
print_r($_POST);
echo '</pre>';

{
switch( strToLower($_POST['class']) )
{

case "1":
$result = mysql_query("UPDATE users SET field1='10,field3='12' where id=$id");
break;
case "2":
$result = mysql_query("UPDATE users SET field1='3,field3='10'where id=$id");
break;
case "3":
$result = mysql_query("UPDATE users SET field1='6,field3='14' where id=$id");
break;
case "4":
$result = mysql_query("UPDATE users SET field1='2,field3='8' where id=$id");
break;
case "5":
$result = mysql_query("UPDATE users SET field1='9,field3='12' where id=$id");
break;
case "6":
$result = mysql_query("UPDATE users SET field1='5,field3='10' where id=$id");
}
}
switch ($mode) {
case "login":
// Get user data
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == "") {
header("Location: main.php?error=1");
die();
}
if ($password == "") {
header("Location: main.php?error=2");
die();
}
// MD5 the password. Lower the username
$password = md5($password);
$username1 = strtolower($username);
$query = mysql_query("SELECT COUNT(LOWER(username)), id FROM users where username='$username' GROUP BY username");
$result = mysql_fetch_array($query, MYSQL_NUM);
if ($result[0] != 1) {
header("Location: main.php?error=3");
die();
}
$query = mysql_query("select password from users where id=$result[1]");
$id1 = $result[1];
$result = mysql_fetch_array($query, MYSQL_NUM);
if ($result[0] == $password) {
session_start();
$_SESSION['id'] = $id1;
// Get the actual user name
$query = mysql_query("SELECT username FROM users where id=$id1");
$result = mysql_fetch_array($query, MYSQL_NUM);
$username = $result[0];
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
// Check to see if they want to be logged in for a week.
$week = $_POST['week'];
if ($week == "yes") {
// add the cookie data
// find out the time a week from now
$time = time();
$time1 = 7 * 24;
$time1 = $time1 * 60 *60;
$time = $time + $time1;
setcookie("username", $username, $time,"/");
setcookie("password", $password, $time,"/");
setcookie("id", $id1,$time,"/");
setcookie("signedin","true",$time,"/");
}
header("Location: home.php");
die();
} else {
header("Location: main.php?error=3");
}
}
[/CODE]


and here is the other one

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Register&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?
require("function.php");
$error = mysqlconnect();
if ($error === FALSE) {
die("Error connecting to MySQL.");
}

if (isset($_GET['error'])) {
print("&lt;b&gt;&lt;font color=Red&gt;");
switch ($_GET['error']) {
case 1:
print("You forgot to enter your username.");
break;
case 2:
print("You forgot to enter your password.");
break;
case 3:
print("You forgot to confirm your password.");
break;
case 4:
print("You forgot to enter your e-mail.");
break;
case 5:
print("Your passwords do not match.");
break;
case 6:
print("Your password must be greater than 4 characters long.");
break;
case 7:
print("Your username must be greater than 4 characters long.");
break;
case 8:
print("Your e-mail must be greater than 4 characters long.");
break;
case 9:
print("Your username may not be more than 15 characters long.");
break;
case 10:
print("Your password may not be more than 50 characters long.");
break;
case 11:
print("Your e-mail cannot be longer than 40 characters.");
break;
case 12:
print("The username, $username, is already in use.");
break;
case 13:
print("The email, $email, is already in use.");
break;
default:
print("Error information not understood. Please contact the webmaster.");
break;
}
print("&lt;/font&gt;&lt;br&gt;&lt;hr&gt;");
}
?&gt;
&lt;form action='process.php' method='post'&gt;
&lt;input type='hidden' name='mode' value='register'&gt;
Username: &lt;input type='text' name='username' size=16 maxlength=15 value='&lt;? print $_GET['username']; ?&gt;'&gt;&lt;br&gt;
Password: &lt;input type='password' name='password' size=10 maxlength=50&gt;&lt;br&gt;
Confirm Password: &lt;input type='password' name='cpassword' size=10 maxlength=50&gt;&lt;br&gt;
E-mail: &lt;input type='text' name='email' size=40 maxlength=40 value='&lt;? print $_GET['email']; ?&gt;'&gt;&lt;br&gt;
&lt;input type="radio" name="class" value="A" selected="selected" /&gt; A
&lt;br /&gt;
&lt;input type="radio" name="class" value="B" /&gt; B
&lt;br /&gt;

&lt;input tupe="submit" name="submit" /&gt;&lt;!-- Blahhhh--&gt;

&lt;/form&gt;


Thank you ahead of time if you can help (by the way, I know I only have 2 radio buttons on the form, but 6 cases for them on process.php, I just put in 2 radio buttons for testing, will add more later).
Copy linkTweet thisAlerts:
@criterion9Jul 05.2009 — Have you tried print_r on _POST? I was under the impression that radio buttons passed the value attribute so in your example you would use case 'A': case 'B': and so on. If you want the radio buttons to pass numeric values then you would need to use numeric values in the value attribute for each radio button named "class". I'm also not sure if this might be a problem but "class" may be a reserved word in some cases so you may want to use another name for your radio group.
Copy linkTweet thisAlerts:
@Nate101authorJul 05.2009 — I tried switching the A and B to numerical values (on both register.php and process.php) and then the switching class to something else, neither worked ?
Copy linkTweet thisAlerts:
@criterion9Jul 05.2009 — Did you try print_r on_POST? Did you get the results you were expecting? Also have you identified where in the code it isn't working as expected?
Copy linkTweet thisAlerts:
@Nate101authorJul 05.2009 — I've never used print_r before can you please explain how it works/what it does (or supposed to do)?
Copy linkTweet thisAlerts:
@SyCoJul 06.2009 — At the top of process.php add
[code=php]<?
echo '<pre>';
print_r($_POST);
echo '</pre>';
[/code]


print_r() is a debugging function used to print out an array of key and values, var_dump() is also useful. You can use either with any array. If you wrap it in <pre> tags its more readable.

If you add it to the form's landing page you'll see what information was posted and if it's as you'd expect.

BTW you're renaming variables for no reason. This means it's easier to miss the user inputted origins of the data. When you do this you're more likely to miss validating the user data, leaving you open to attack. Your script is currently wide open to a type of attack called SQL injection. You need to read up on this and fix your script or your site can be hacked by anyone with virtually no skill.
Copy linkTweet thisAlerts:
@Nate101authorJul 06.2009 — oh alright, thank you, i'll look into that in a bit, this is the information I got

Array

(

[mode] => register

[username] => Nate101

[password] => testing123

[cpassword] => testing123

[email] => [email protected]

[skill] => class

)

I think instead of [skill] => class it's supposed to say [skill] => 1, instead? but, would it show the editing of the fields that I am using such as like editing the certain fields to 10 instead of 1?
Copy linkTweet thisAlerts:
@SyCoJul 06.2009 — I copied your example code just for your form (just the HTML) and this is the print r (with some data inputted)

[code=php]Array
(
[mode] => register
[username] => testun
[password] => testpw
[cpassword] => testpwc
[email] => testemail
[class] => A
[submit] => Submit Query
)[/code]


So your print_r() is not the same as the code you posted which makes finding out your problem a little difficult. It sounds like you've got an error in your HTML for the form.
Copy linkTweet thisAlerts:
@Nate101authorJul 06.2009 — Array

(

[mode] => register

[username] => testing123

[password] => testingagain

[cpassword] => testingagain

[email] => [email protected]

[skill] => 1

)

I think that is what I wanted. I changed the A's and B's to numerical values before, so that is pretty much exactly what I wanted, except it just doesn't do what I want it to do in the database?
Copy linkTweet thisAlerts:
@SyCoJul 06.2009 — You're making changes and not saying what you're doing. You are saying you have a new problem but not showing code. We don't know what it's doing wrong because we don't know what you want it to do in the database. What in this case does 'wrong' mean?

If you want help you'll need to help us help you. Details and code examples please. As a rule of thumb you should spend at least as long writing you questions as you expect someone else to in responding.
Copy linkTweet thisAlerts:
@Nate101authorJul 06.2009 — alright i'll start from the begining...

Ok, so I want to create a user sign up/regsitration, on it I want to contain 6 options to choose from, for this case, i'll call them A,B,C,D,E,F. User can only select one, and depending on which one they select, different attributes will go down into their account. So if they choose A, It'll mark down in their database as 1, for one of the fields, and then it will change a couple of the other fields instead of the default of 1, it'll change some of them to maybe 10. Now if they selected B, it would do the same thing except mark down Id number 2 (for cool.gif, and then in some of the other fields mark down 10 (in different fields than A did).

So basically if that was really confusing,

I have 6 Classes

A-F right?

Well, now this is what the database would kinda look like

Username Class Field1 Field2 Field3 Field 4

Testing1 1 1 10 1 1

Testing2 2 1 1 10 1

Testing3 3 10 1 1 1

Testing4 4 1 1 1 10

Testing5 5 10 10 1 1

Testing6 6 1 1 1 1

and as to the fix I just made, I didn't make one, well I did, but it was a error on my part but it was nothing new to the code I already posted ( i accidentally changed something from the above code, but changed it back)

and as to numerical to alphabetical, I changed all the A's to 1s, all the Bs to 2s, and so on.
×

Success!

Help @Nate101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...