Jump to content

How to compare $_POST input and an integer saved to MySQL table?


hannap

Recommended Posts

Hello!

 

I have a problem to compare a value given by a subject as an input ($_POST["sava"]) and an integer value taken from a MySQL table

mysql_connect("","root");

mysql_select_db("chat");

$coran = mysql_query("select * from pretest_tasks");

$cansw = mysql_fetch_assoc($coran);

$cana=$cansw["answ1a"];

 

In which form is the $_POST input and how can I compare it to the integer value with if-else structure?

 

This far I have tried the string comparison and the direct == comparison, but it always goes into the else-part saving the given answer as 'incorrect'. The piece of PHP code can be seen below:

 

$compa=strcmp($_POST["sava"],$cana);

if($_POST["sava"]==$cana) {

/* OR if($compa==0) { */

$corr='correct';

mysql_query("insert workspace1 ($savecor, $savea, $saveb)

values ('" . $corr . "','" . $_POST["sava"] . "',

'" . $_POST["savb"] . "')");

} else {

$corr='incorrect';

mysql_query("insert workspace1 ($savecor, $savea, $saveb)

values ('" . $corr . "','" . $_POST["sava"] . "',

'" . $_POST["savb"] . "')");

 

How should I change the format of $_POST or integer to make them comparable?

 

Thank you!

Link to comment
Share on other sites

Please post all code in the forum's code tags.

 

Before any of the processing, add

print_r($_POST);

And see if the values are what you expect. You should also probably print_r($cansw); and make sure it has the values you expect. You are not checking to see if your query was actually successful, so that could be failing.

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.