/    Sign up×
Community /Pin to ProfileBookmark

New to PHP – Weird Error

Hi everyone,

I’m relatively new to PHP and database scripting, but I’m running into a strange problem that I can’t seem to resolve. I am using a simple HTML forum to get data which will then be sent to a MySQL database table.

<?php
if(isset($_POST[‘Submit’])){
$participant = $_
POST[‘participant’];
$time = $_POST[‘time’];
$err = ”;

if(trim($participant)==”){
$err .= ‘-Please enter a name<br>’;
}
if(empty($time)){
$err .= ‘-Please enter a time’;
}
if($err!=”){
echo $err;
}
else{
$db=mysql_connect (“localhost”, “user”, “pass”) or die (‘I cannot connect to the database because: ‘ . mysql_error());
mysql_select_db (“mydb”);

$query = “INSERT INTO testtable (name, milliseconds) VALUES ($participant, $time)”;

mysql_query($query) or die(‘Error: ‘ . mysql_error());

echo “new record added”;

mysql_close($db);

}
}
?>

<form name=”dataform” method=”post” action=””>
Participant Name: <br>
<input name=”participant” type=”text” id=”participant” value=”Bob”><br><br>
Time (milliseconds):<br><input name=”time” type=”text” id=”time” value=”99.99″><br><br>
<input type=”submit” name=”Submit” value=”Submit”>
</form>

When I hit submit, however, I get an error: “Unknown column ‘Bob’ in ‘field list'” Now, the problem here is that, ‘Bob’ is what I entered in my text box for $participant. That is being sent as a value for the ‘name’ column.

Any idea why it would switch those around on me?

Thanks!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscOct 16.2008 — [code=php]
$query = "INSERT INTO testtable (name, milliseconds) VALUES ('".$participant."', '".$time."')";
[/code]


encase the values in single quotes
Copy linkTweet thisAlerts:
@AdmanStrongauthorOct 16.2008 — Great! Thank you very much!
Copy linkTweet thisAlerts:
@MrCoderOct 17.2008 — And use mysql_real_escape_string, or even better mysql[B]i[/B]
×

Success!

Help @AdmanStrong 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.25,
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,
)...