MySQL not submitting?

Tekno Venus

Senior Administrator, Developer
Staff member
Joined
Jul 21, 2012
Posts
7,274
Location
UK
Hello,

I am currently writing a questionnaire for my Physics class, and am trying to test my programming skills as well :)

I have created an online page, linked to a SQL database. I have done this before, and made it work. But for some reason, it just won't work this time. It just won't submit to the DB.

I have tried using a hardcoded value for the submission (such as "hello"), and it works. Only when the variables are introduced does it fail.

PHP is not my strongest language. Can anyone see at a glance where I am going wrong?

HTML:
<!DOCTYPE HTML>
<html>
<head>
<title>Radioactivity Questionnaire</title>
<style>
body {font: 13px Arial,Tahoma,Calibri,Verdana,Geneva,sans-serif;}
.textfield{ background: white; border: 1px double #DDD; border-radius: 5px; box-shadow: 0 0 2px #333; color: #666; padding: 5px 10px; width: 600px; outline: no font: 13px Arial,Tahoma,Calibri,Verdana,Geneva,sans-serif}
.textfieldsmall{ background: white; border: 1px double #DDD; border-radius: 5px; box-shadow: 0 0 2px #333; color: #666; padding: 5px 10px; width: 100px; outline: no font: 13px Arial,Tahoma,Calibri,Verdana,Geneva,sans-serif}
</style>
</head>
<? if (!isset($_POST['submit'])) { ?>
<body>
<form name="myform" action="index.php" method="post">
<h2> Radioactivity Questionnaire </h2>
<!--AGE-->
<p><strong>Are you male or female?</strong></p>
<input type="radio" name="sex" value="Male" checked> Male <br />
<input type="radio" name="sex" value="Female"> Female <br />
<!---->
<hr />
<!--TYPES OF RADIATION-->
<p><strong>What types of radioactive sources have you heard of? Please list them below, each on a new line</strong></p>
<textarea title="sourceOfRadiation" class="textfield" id="radiationSources" name="radiationSources" rows="4"></textarea><br /><br />
<!---->
<hr />
<!--WHY ARE THEY DANGEROUS?-->
<p><strong>From your knowledge, why are radioactive sources dangerous to living creatures?</strong></p>
<textarea title="dangersOfRadiation" class="textfield" id="dangers" name="dangers" rows="3"></textarea><br /><br />
<!---->
<hr />
<!--WAYS TO PREVENT-->
<p><strong>Give some methods that you could use to reduce or prevent harm from radioactive sources</strong></p>
<textarea title="preventionOfRadiation" class="textfield" id="prevention" name="prevention" rows="3"></textarea><br /><br />
<!---->
<hr />
<!--WHO WOULD YOU GO TO FOR ADVICE?-->
<p><strong>Who, or where, would you go to for advice about radiation? List some examples below, each on a new line</strong></p>
<textarea title="sourceForAdvice" class="textfield" id="advice" name="advice" rows="4"></textarea><br /><br />
<!---->
<hr />
<!--DO YOU UNDERSTAND BACKGROUND RADIATION?-->
<p><strong>Do you understand what is meant by the term "Background Radiation"?</strong></p>
<input type="radio" name="backgroundRad" id="backgroundRad_yes" value="Yes"> Yes <br />
<input type="radio" name="backgroundRad" id="backgroundRad_no" value="No" checked> No <br />
<!---->
<hr />
<!--DO YOU UNDERSTAND BACKGROUND RADIATION?-->
<p><strong>If yes, give a simple definition below</strong></p>
<textarea title="backgroundRadDefinition" class="textfield" id="definition" name="definition" rows="3"></textarea><br /><br />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

<?
}
else{ 
$db = mysql_connect("localhost","USERNAME",'PASSWORD');
if(!$db) die("Error connecting to MySQL database.");

function PrepSQL($value) {
    if(get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    $value = "'" . mysql_real_escape_string($value) . "'";
    return($value);
}

$sex = PrepSQL($_POST['sex']);
$radSource = PrepSQL($_POST['radiationSources']);
$dangers = PrepSQL($_POST['dangers']);
$prevention = PrepSQL($_POST['prevention']);
$advice = PrepSQL($_POST['advice']);
$backgroundRadRadio = PrepSQL($_POST['backgroundRad']);
$backgroundRadDefinition = PrepSQL($_POST['definition']);
$ip=$_SERVER['REMOTE_ADDR'];

$sql = "INSERT INTO bsodforu_physics_questionnaire.results (Sex, Sources, WhyDangerous, Prevent, Advice, BackgroundDoYouKnow, BackgroundDefinition, IP) VALUES ($sex, '$radSource', '$dangers', '$prevention', '$advice', '$backgroundRadRadio', '$backgroundRadDefinition', '$ip')";
mysql_query($sql);
}

 /****************************/
 /******** EMAIL CODE ********/
 /****************************//*$to = "TO EMAIL"; 
$subject = "Questionnaire Submitted"; 
$message = "Questionnaire Submitted";
//$message .= '<h1 style = "font: 22px Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif; font-weight:bold; color:#000080;"> New questionnaire submission</h1>';
//$message .= "A user with the following IP has submitted your questionnaire - <strong><a href=geoip.flagfox.net/?ip=" . $ip . ">" . $ip . '</a></strong><br /><br />';
//$message .= "The results have been logged in the database";
//$message .= '</body></html>';
$from = FROM EMAIL;
//$from_name = "Physics Questionnaire"; 
//$headers = "From: " . $from_name . "<" . $from . ">" . "\r\n";
//$headers .= "MIME-Version: 1.0\r\n";
//$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message)) {
    echo "Form Sent Successfully - thank you for your feedback";
}}*/
?>

You can see it here: Radioactivity Questionnaire

Thanks,
Stephen
 
Last edited:
Code:
function PrepSQL($value) {
    if(get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    $value = "'" . mysql_real_escape_string($value) . "'";
    return($value);
}

You don't need to add the ' ' marks in your PrepSQL function. The query you're ending up with is INSERT INTO blah (blah, blah) VALUES '' blah '' blah '' blah - etc.

Code:
$sql = "INSERT INTO bsodforu_physics_questionnaire.results (Sex, Sources, WhyDangerous, Prevent, Advice, BackgroundDoYouKnow, BackgroundDefinition, IP) VALUES ($sex, '$radSource', '$dangers', '$prevention', '$advice', '$backgroundRadRadio', '$backgroundRadDefinition', '$ip')";

The $sex variable is also missing the '' marks, but as a result was probably the only variable actually being submitted.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top