Anyone know how I could go about making a PHP script that took a form variable, and used it in a mySQL query?
One that looked at a variable already in the table, incremented it by 1, and then put that into the query, would be even better.
Ideas?

Moderator: Tech Haven Network - Moderator team
Lachlan wrote:What part are you needing help with? Doing the query or using the variable? Keep in mind that it's a very bad idea to use any kind of user input directly in a sql query without sanitizing it. That's how sql injections hapen.
Morpheous wrote:Pretty much everything
Morpheous wrote:And it's only using user input for one part of the query, not the whole thing
$dbserver = "localhost";
$dbname = "mydb";
$dbuname = "dbuser";
$dbpass = "letmein";
$database = mysql_connect($dbserver, $dbuname, $dbpass);
mysql_select_db($dbname);
$query = "select * from dummy_table where dummy_field = \'".$user_input."\'";
$result = mysql_query($query);
$xnum = mysql_num_fields($result);
$rowNum = mysql_num_rows($result);
// Read all the data in the table
for ($j = 0; $j<$rowNum; ++$j)
{
$row = mysql_fetch_array($result);
$currTable[$j]=$row;
}
Morpheous wrote:Edit: Think I got that, I got a form on the page- how do I get it to load it? Or might using 2 pages and a HTTP POST form be best with a get_url_var tag?
Lachlan wrote:Morpheous wrote:Edit: Think I got that, I got a form on the page- how do I get it to load it? Or might using 2 pages and a HTTP POST form be best with a get_url_var tag?
There's two ways you can do that. You can either use a form that has your PHP script as it's "action" or you can have some kind of flag that if not set will make your php script actually put the form and call itself as the "action" ($PHP_SELF).
Either way, it's as simple as setting a named input in your form and then referencing that name with a dollar sign in front of it.
In html:
<input name="test">
In php:
echo $test;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Database insert test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p> </p>
<form action="testdb.php" method="get" onSubmit="<?php
$username = username
$dbserver = "localhost";
$dbname = "icarus_codevault";
$dbuname = "icarus_codevault";
$dbpass = "codevault";
// Don't lecture me about security, that's the test pwd :)
$database = mysql_connect($dbserver, $dbuname, $dbpass);
mysql_select_db($dbname);
$query = "INSERT INTO `codevault_users` (`userid`, `username`, `password`, `fullname`, `email`, `theme`, `admin`) VALUES ('', '"$username"', '4cb9c8a8048fd02294477fcb1a41191a', '', '', 'facade', '0');";
$result = mysql_query($query);
$xnum = mysql_num_fields($result);
$rowNum = mysql_num_rows($result);
// Read all the data in the table [Removed because i'm writing... :x]
//for ($j = 0; $j<$rowNum; ++$j)
//{
// $row = mysql_fetch_array($result);
//$currTable[$j]=$row;
//}
?>">
<input name="username" type="text" id="username">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Database insert test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p> </p>
<?php
if ($username == "")
{
echo "<form action=\"testdb.php\" method=\"POST\">\n";
echo "<input name=\"username\" type=\"text\" id=\"username\">\n";
echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">"\n;
echo "</form>\n";
}
else
{
$dbserver = "localhost";
$dbname = "icarus_codevault";
$dbuname = "icarus_codevault";
$dbpass = "codevault";
// Don't lecture me about security, that's the test pwd :)
$database = mysql_connect($dbserver, $dbuname, $dbpass);
mysql_select_db($dbname);
$query = "INSERT INTO `codevault_users` (`userid`, `username`, `password`, `fullname`, `email`, `theme`, `admin`) VALUES ('', '"$username"', '4cb9c8a8048fd02294477fcb1a41191a', '', '', 'facade', '0');";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
echo "Insert successful";
}
else
{
echo "Insert failed";
}
?>
</body>
</html>
Morpheous wrote:Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/icarus/public_html/testdb.php on line 17
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/icarus/public_html/testdb.php on line 17
echo "<input type="submit" name="Submit" value="Submit">"\n;
echo "<input type="submit" name="Submit" value="Submit">\n";
echo "<form action="testdb.php" method="POST">\n";
echo "<form action="$PHP_SELF" method="POST">\n";
Users browsing this forum: No registered users and 3 guests