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

Link to comment
Share on other sites

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

 

if (  preg_match( '/[^\w-]/', $_POST['name'] ) || preg_match( '/[^\w@.-]/', $_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);

 

 

Link to comment
Share on other sites

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());


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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