/    Sign up×
Community /Pin to ProfileBookmark

PHP Form Data Type

I am trying create a form that submits data into a MySQL table. A gutted version of the code is shown below. It works great when I replace the red line with:
[COLOR=”Red”]$query = “INSERT INTO top VALUES ($score, ‘Bob’)”;[/COLOR]
so I think there is some problem recognizing the variable $name which is sent from the form as a string compatible with the MySQL table type tinytext

<html>
<head>
<?php
//data String
$name = $_POST[“name”];
$score = (int)$_
POST[“score”];
?>
</head>
<body>

<?php
—Code Removed—
[COLOR=”Red”]$query = “INSERT INTO top VALUES ($score, $name)”;[/COLOR]
mysql_query($query);
?>

</body>
</html>

Thanks in Advance,
Alex

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ryanlundApr 27.2011 — Have you tried:

[code=php]
mysql_query($query) or die(mysql_error());
[/code]


If so, what are the results?

P.s this may work?
[code=php]
$query="INSERT INTO top VALUES('".$score."','".$name."')";
[/code]
Copy linkTweet thisAlerts:
@NogDogApr 27.2011 — [code=php]
$query = sprintf(
"INSERT INTO top VALUES (%d, '%s')"; // quote string literal in SQL
(int) $score, // force to be integer
mysql_real_escape_string($name) // escape problematic SQL characters
);
[/code]
Copy linkTweet thisAlerts:
@ryanlundApr 27.2011 — [code=php]
$query = sprintf(
"INSERT INTO top VALUES (%d, '%s')"; // quote string literal in SQL
(int) $score, // force to be integer
mysql_real_escape_string($name) // escape problematic SQL characters
);
[/code]
[/QUOTE]


Good plan, i assumed the rest of his code (he said it was a stripped down version) had incorporated for SQL injection like...
Copy linkTweet thisAlerts:
@NogDogApr 27.2011 — Good plan, i assumed the rest of his code (he said it was a stripped down version) had incorporated for SQL injection like...[/QUOTE]

I never like to assume that, as many people do not. ?

PS: To the OP, you might want to consider moving up the the MySQL[b]i[/b] extension and use prepared statements, which can help handle this sort of thing if you use bound input parameters.
Copy linkTweet thisAlerts:
@ablatyauthorApr 28.2011 — Worked great.

Thanks!
×

Success!

Help @ablaty 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...