Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ create a table with $name

Posted by: xxkasperxx Apr 11 2012, 10:06 PM

I am trying to create a new table using a form like this

CODE

<form action='' method="POST">
            Name: <textarea rows="1" name="name" class="style"> </textarea><br>
            Percent: <textarea name="name" class="style">0%</textarea>
            <br>
            <input type="submit" name="add" value="ADD New User">
            </form>


and my php:

CODE

$name = $_POST['name'];
    if ($_POST)
    {
        mysql_query("CREATE TABLE '$name'(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
age INT)")
or die(mysql_error());  
    }


but i get this error
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0%'( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), ' at line 1"

How do i fix that?

Posted by: Frederiek Apr 12 2012, 01:28 AM

You use name="name" twice and the second one is taken in account by your script:

Name: <textarea rows="1" name="name" class="style"> </textarea><br>
Percent: <textarea name="name" class="style">0%</textarea>

BTW, why don't you use single line text fields?

Posted by: Brian Chandler Apr 12 2012, 03:25 AM

Anyway, you are trying to create a new table for each user. This means you have misunderstood the whole point of a relational database, and like the other questioner in this forum you need to go back to the beginning and read a tutorial or real book.

In any event trying to create tables with random names will never work because of characters that need escaping, or tables that already exist for genuine purposes.

Posted by: xxkasperxx Apr 12 2012, 06:59 PM

They are only created by me. I see no point if I already know what is in the database or not.

Posted by: xxkasperxx Apr 12 2012, 07:00 PM

Frederick what do you mean? I have never herd of them in the html or php books I have read.

Posted by: Darin McGrew Apr 12 2012, 07:56 PM

From our HTML Reference:
http://htmlhelp.com/reference/html40/forms/input.html
"The type of form control defined by INPUT is given by the TYPE attribute. The default TYPE is text, which provides a single-line text input field."

A single-line text field is just:
<input type="text" ...>

Posted by: xxkasperxx Apr 12 2012, 08:11 PM

Thank you for the quick reply!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)