Jump to content

[SOLVED] Using $_POST[''] Variable in a SQL Query


jjacquay712

Recommended Posts

I am having trouble getting a user entered variable into a query.

 

here is my code:

 

mysql_query("CREATE TABLE " . $_POST['name'] .  " (" . $_POST['email'] .  " varchar)") or die("Could not Create Table");

 

it always prints the error message "Could not Create Table"

 

any suggestions in the syntax for entering user variables?

 

Thanks for the help, John

Well, you want to match a very strict set of values... soo using a regex like

 

if (  preg_match( '/[^\w-]/', $_POST['name'] ) || preg_match( '/[^\[email protected]]/', $_POST['email'] )  )
    # Found something that wasnt a letter, number, underscore or dash! ( @ and . allowed in email )
    exit( 'Invalid characters used' );

 

Second, varchar must have a length, i believe

 


# Assuming it passes the regex above
$q = <<<QDOC
CREATE TABLE `{$_POST['name']}` (
    `{$_POST['email']}` VARCHAR( 255 )
)
QDOC;

mysql_query($q);

 

 

Change the die clause to:

 

die(mysql_error()) so you can see what's going on.

 

Better yet, create your query as a string so you can echo it to the page when there is an error:

 

<?php

$query = "CREATE TABLE " . $_POST['name'] .  " (" . $_POST['email'] .  " varchar)";

mysql_query($query) or die("Could not Create Table<br>Query: $query<br>Error: ".mysql_error());


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.