PHP Classes

How to Create an SQL Table from a TextArea? (Button Click)

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  How to Create an SQL Table from a...  
Subject:How to Create an SQL Table from a...
Summary:How to Create an SQL Table from a TextArea?
Messages:2
Author:Miklo
Date:2013-08-12 16:08:45
Update:2013-08-15 16:06:36
 

  1. How to Create an SQL Table from a...   Reply   Report abuse  
Picture of Miklo Miklo - 2013-08-12 20:43:51
Hello everyone, I been a member here for some time, and have seen some amazing Stuff... Today I really need you help with my PHP Script!

I am creating a php Script for users to log Visitor IP's and also go in and Track who was visiting their site and everything will be logged inside a mysql database!

When launching the script, the users will be asked to choose a Name inside a "textarea". So when a user picks a name and clicks on CREATE URL, then the script will create a sql database table called "test".

Here is an image of the TextArea script:

i39.tinypic.com/106aghx.png

I want to know how I can create a Table from the text being typed in the Textare. So when a user chooses the name "mike" in the textarea, and clicks SUBMIT then the script should create a table named "mike", and also add the TABLES inside the MIKE that I am pasting below!


The textarea is called "$name" in the sql code below! When I add "$name" infront of the this part and run the script, I get an error:

CREATE TABLE $name

The code below is my full index script, and it creats a table named "test"! But it does not create it when clicking submit! It creates the table as soon as the script is runned... So that needs to be fixed too, so it should only add the table after a click on the submit button:

My full code:
<form action="" method="post" >
<table width="400" border="0" cellspacing="0" cellpadding="5">
<tr>
<td colspan="3" align="center">Please write gallery name and description</td>
</tr>
<tr>
<td>Name </td>
<td>&nbsp;</td>
<td><input type="text" name="gname" id="text" value=""></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="submit" id="submit" value="Submit"></td>
</tr>
</table>
</form>
<?PHP
if (isset($_POST['submit'])) {
$name = $_POST['gname'];
$username = 'gname';
$description = $_POST['gdescription'];
mkdir("$name", 0777);
copy('test1.php', "$name/test1.php");
echo " ";
echo "Conguradulations. Your new URL is sucessfully created.!";
echo " ";
echo " ";
echo "Your new URL name is www.visitor.webege.com/$name";
}
$dbhost = 'host';
$dbuser = 'dbuser';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE test('.
'entry_id int(11) NOT NULL AUTO_INCREMENT, '.
'visitor_id int(11) default NULL, '.
'ip_address varchar(15) NOT NULL, '.
'page_name text, '.
'query_string text, '.
'timestamp timestamp NOT NULL default CURRENT_TIMESTAMP, '.
'PRIMARY KEY ( entry_id ), '.
'KEY visitor_id ( visitor_id , timestamp )) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;';

mysql_select_db('dbname');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
mysql_query($sql,$conn);
?>

I realy need help with this, I have worked for days and even asked help on other forums, but so far no answers!

So my Summary is, How to Create a Table from a Textarea after clicking the Submit button! Please notice that when the TABLE is created, then rest of the Tables should be added in side it..


******** A code I found online *********
I found the code below online with 3 Pages. I cannot make it work.. I added it in here just if someone could make it work....

Page 1:
//hands of the post values to a handler
<form name="news" method="post" action="textareatexthandler.php">
<textarea name="comment" cols="90" rows="25" wrap="hard"></textarea><br />
<input type="submit" value="Submit" />

Page 2:
//The handler page only contains the insert function instead of the insert and select function
<?php
$host="serverName";
$username="rainhider";
$password="yeah_right";

$conn = mysql_connect($host, $username, $password) or
die('Error connecting to mysql ' . mysql_error());


$selected = mysql_select_db("rainhid_test", $conn)or
die("Could not select db rainhid_test");


$mysql = array(); // create array of mysql escaped values
$mysql['comment'] = mysql_real_escape_string( $_POST['comment'] );
$sql = "INSERT INTO comments ( comment_text ) VALUES ( '{$mysql['comment']}' )"; //insert your comment to db
mysql_query( $sql );



Page 2:
//I needed to retrieve the last mysql id inserted to the database and this code //retrieves the id number of the last insert and then stores it in the session array so that I can retrieve it on the third page.

session_start();
$_SESSION['$sqlID'] = mysql_insert_id(); // store last id in session array
echo $_SESSION['$sqlID']; //just to make sure it got the number


echo "Success! Your newsletter has been saved and posted!"
?>


//The third page retrieves the last inserted comment(by the last id) and displays it

$host="serverName";
$username="rainhider";
$password="yeah_right";

$conn = mysql_connect($host, $username, $password) or
die('Error connecting to mysql ' . mysql_error());


$selected = mysql_select_db("rainhid_test", $conn)or
die("Could not select db rainhid_test");


session_start();
$sqlID = $_SESSION['$sqlID'];
$sql = "SELECT * FROM comments where comment_id='$sqlID'";
$result = mysql_query( $sql );
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
$html = array(); //create an array of html formated values.
$html['comment'] = nl2br( stripslashes( htmlentities( $row['comment_text'], ENT_QUOTES, 'UTF-8' ) ) );
echo "<br />{$html['comment']}<hr />";
}
?>


Thank you very much in advance, and GOD BLESS!

There is 1 reply in this thread, which is not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.