Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:39:51+00:00 2026-05-25T17:39:51+00:00

I do not think that this has been posted before – as this is

  • 0

I do not think that this has been posted before – as this is a very specific problem.

I have a script that generates a “create table” script with a custom number of columns with custom types and names.

Here is a sample that should give you enough to work from –

$cols = array();
$count = 1;
$numcols = $_POST['cols'];
while ($numcols > 0) {

    $cols[] = mysql_real_escape_string($_POST[$count."_name"])." ".mysql_real_escape_string($_POST[$count."_type"]);
    $count ++;
    $numcols --;
}
$allcols = null;
$newcounter = $_POST['cols'];
foreach ($cols as $col) { 
    if ($newcounter > 1)
        $allcols = $allcols.$col.",\n";
    else
        $allcols = $allcols.$col."\n";
    $newcounter --;
};
$fullname = $_SESSION['user_id']."_".mysql_real_escape_string($_POST['name']);
$dbname = mysql_real_escape_string($_POST['name']);
$query = "CREATE TABLE ".$fullname." (\n".$allcols." )";
mysql_query($query);
echo create_table($query, $fullname, $dbname, $actualcols);

But for some reason, when I run this query, it returns a syntax error in MySQL. This is probably to do with line breaks, but I can’t figure it out. HELP!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:39:51+00:00Added an answer on May 25, 2026 at 5:39 pm

    You have multiple SQL-injection holes
    mysql_real_escape_string() only works for values, not for anything else.
    Also you are using it wrong, you need to quote your values aka parameters in single quotes.

    $normal_query = "SELECT col1 FROM table1 WHERE col2 = '$escaped_var' ";
    

    If you don’t mysql_real_escape_string() will not work and you will get syntax errors as a bonus.
    In a CREATE statement there are no parameters, so escaping makes no sense and serves no purpose.

    You need to whitelist your column names because this code does absolutely nothing to protect you.

    Coding horror

    $dbname = mysql_real_escape_string($_POST['name']); //unsafe
    

    see this question for answers:
    How to prevent SQL injection with dynamic tablenames?

    Never use \n in a query
    Use separate the elements using spaces. MySQL is perfectly happy to accept your query as one long string.
    If you want to pretty-print your query, use two spaces in place of \n and replace a double space by a linebreak in the code that displays the query on the screen.

    More SQL-injection
    $SESSION['user_id'] is not secure, you suggest you convert that into an integer and then feed it into the query. Because you cannot check it against a whitelist and escaping tablenames is pointless.

    $safesession_id = intval($SESSION['user_id']);  
    

    Surround all table and column names in backticks `
    This is not needed for handwritten code, but for autogenerated code it is essential.

    Example:

    CREATE TABLE `table_18993` (`id` INTEGER .....
    

    Learn from the master
    You can generate the create statement of a table in MySQL using the following MySQL query:

    SHOW CREATE TABLE tblname;
    

    Your code needs to replicate the output of this statement exactly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sorry if this has been posted before. I have searched many websites
I don't think this question has been posed before, at least not in the
Currently in our enterprise we have a situation that i think it's not very
I have an AutoMapper issue that has been driving me crazy for way too
First, I apologize if this has been asked before - indeed I'm sure it
So the teacher has posed this assignment: You have been hired by the United
I know that this question has been done to death at StackOverflow and that
Sorry if this has been asked before - there's a question with a similar
First of all: A quite similar problem has been posted and somehow solved already,
OK so that title sucks a little but I could not think of anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.