Jump to content

Putting POST variable into database


eyegraphix

Recommended Posts

I have problem that I'm sure is easy to solve. The problem is getting a certain post variable from a form into a database. I'm submitting all the variables into a mysql database, but one of the variables does not show up in the database ($vote). Why oh why do the contents of the $vote variable not get into the database?

 

<? session_start();

if (isset($_SESSION['valid_user']))
{
include ("dbconnect.php");

$vote=$_POST['radiobutton'];
$netid=$_SERVER['REMOTE_USER'];


echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else
click <a href=\"70scluster.php?action=submitvote\"> here </a><br>";

$action=$_GET['action'];

if ($action =="submitvote"){

mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$vote') ") or die(mysql_error());                  

echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>";
}


}
?>

Link to comment
Share on other sites

Um, that didn't work either. Here's the updated code.

 

<? session_start();

if (isset($_SESSION['valid_user']))
{
include ("dbconnect.php");

$vote=$_POST['radiobutton'];
$selected_radio = $_POST[$vote];   

$netid=$_SERVER['REMOTE_USER'];



echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else
click <a href=\"70scluster.php?action=submitvote\"> here </a><br>";

$action=$_GET['action'];

if ($action =="submitvote"){

mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$selected_radio') ") or die(mysql_error());

echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>";
}


}
?>

Link to comment
Share on other sites

is radiobutton the name of the radio field?

 

for instance in the form...

 

<input type='radio' name='radiobutton' value='value_1'>

<input type='radio' name='radiobutton' value='value_2'>

<input type='radio' name='radiobutton' value='value_3'>

 

if the user selects say, value_2...

 

the $_POST['radiobutton'] variable will be value_2.

You should be able to echo that value from that variable.

 

Link to comment
Share on other sites

Yes you are correct, that's what my form looks like. And yes, I know perfectly well I can echo it's contents out. When I put something like echo $vote I get what's in it, but when I try to insert it into the database it's not there.

Link to comment
Share on other sites

Why are you doing this?

<?php
$vote=$_POST['radiobutton'];
$selected_radio = $_POST[$vote];   
?>

 

If you do an

<?php
echo $selected_radio;
?>

 

What do you get?

 

You probably want to change your MySQL query to:

<?php
$query = "INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$vote') ";
$rs = mysql($query) or die("Problem with the query <pre>$query</pre><br>" . mysql_error());
?>

 

Ken

 

Link to comment
Share on other sites

At a quick glance this is what it looks like you have happening:

 

Person clicks a radio button then submits it to the page you displayed here.

 

The page you displayed here lets the person verify that was the one they wanted to vote for and re-submits it to the same page.

 

The page then puts it in the database.

 

If this is what happens, then the problem is you aren't re-passing the variable to the next instance of the page after they verify they picked the right choice.  You can either do that with a hidden submit or a session variable:

 

$_SESSION[selected_radio] = $_POST[$vote]; 

 

mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$_SESSION[selected_radio]') ") or die(mysql_error());

 

Michael

 

Link to comment
Share on other sites

Yes pentan. That's exactly what I'm trying to do. Your suggestion makes sense, but for some reason I'm getting the same result and the vote doesn't get into the database. I can echo'd the contents of $_SESSION[selected_radio] out at various points of the script, but once I try to echo it out past the line if ($action =="submitvote"){ , it doesn't work. Here's my updated code.

 

<? session_start();

if (isset($_SESSION['valid_user']))
{
include ("dbconnect.php");

$vote=$_POST['radiobutton'];

$_SESSION[selected_radio] = $_POST['radiobutton'];

$netid=$_SERVER['REMOTE_USER'];

echo "You have selected to vote for $vote. If this is incorrect,<a href=\"index.php\"> click here </a> to go back else
click <a href=\"70scluster.php?action=submitvote\"> here </a><br>";

$action=$_GET['action'];

if ($action =="submitvote"){

mysql_query("INSERT INTO voting (username, cluster,vote) VALUES('$netid', '70s','$_SESSION[selected_radio]') ") or die(mysql_error($

echo "<input type='button' value='Click here to close this window and logout' onclick='self.close()'>";
}

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.