"SIMPLE" SCIRPT creating drop etc.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

"SIMPLE" SCIRPT creating drop etc.

Post by Citizen99 »

In this script u can take action by clicking button.

I have problem with create (utworz) because this part is a little more complicated.. by if else if etc... when script gets number of fields and table name (ilosc pol), he "return" to without asking about field name field type sie... he print only sql error because he didnt get size, field name and etc. Scirpt is wirtten quite dynamicly so u can run it on your own server u must make only few changes on the beginig. host, pass etc

Thx for all help.

Code: Select all

<?

$host='localhost'; 
$user='moo'; 
$pass='moo'; 
$baza='moo'; 

$moja = mysql_connect($host,$user,$pass)
or die("Nie mozna polaczyc sie z serverem");
mysql_select_db("$baza")
or die("Nie mozna sie polaczyc z baza");

$tab="show tables from $baza";
$wynik=mysql_query($tab);
$licz=mysql_num_rows($wynik);

print "<HTML><BODY><FORM>\n";
print "<INPUT TYPE='text' name='baza' value='$baza'><BR><BR>\n";
print "<SELECT name='tb_name'><option>\n";
	
	for($i=0;$i<$licz;$i++)
	{
	$row=mysql_fetch_array($wynik);
	print ("<option>$row[0]\n");
	 }

print "</SELECT><BR><BR>\n";
print "<input type='submit' value='utworz' name='utworz'>\n";      /
name='describe'><BR><BR>\n";


// <------------------- UTWORZ

if(!$_POST['fields'] and $utworz!="")
        { 
            print "<form method='post'>\n"; 
            print "Ile pol w tabeli? :\n";
            print "<br><input type='text' name='fields'>\n";
            print "<input type='submit' value='Submit'>\n";
            print "<input type='hidden' name='dalej' value='dalej'>\n";
            print "</form>";
        }

        if ($_POST['table'] and $dalej!="" )
        {
            print "Nazwa Tabeli: <input type'text' name='table'><br>\n";
            print "<form method='post'>\n";
  
          for ($i = 0 ; $i <$_POST['fields']; $i++)
            {
                print "<br>Nazwa Kolumny: <input type='text' name='name$i'>\n ";
                print "Type: <select name='type$i'>\n";
                print "<option value=''></option>\n";
                print "<option value='char'>char</option>\n"; 
                print "<option value='int'>int</option>"; 
                print "</select> "; 
                print "Size:<input type='text' name='size$i'>\n"; 
            } 
             print "<input type='hidden' name='fields' value='$_POST[fields]'>"; 
             print "<input type='submit' value='dawaj'>";
             print "</form>";
        } 

        else
        {   
	    $host='localhost'; 
	    $user='moo'; 
   	    $pass='moo'; 
	    $baza='moo'; 
	
	    $moja = mysql_connect($host,$user,$pass) 
	    or die("Nie mozna polaczyc sie z serverem"); 
	    mysql_select_db("$baza") 
	    or die("Nie mozna sie polaczyc z baza");

            $sql = "CREATE TABLE $_POST[table] ("; 
            for ($i = 0; $i < $_POST['fields']; $i++) 
            { 
               	$name="name".$i;
		$type="type".$i;
		$size="size".$i;

                $sql .= "$_POST[$name] $_POST[$type]"; 
                ($_POST[$name] != "") or die("Chociaz nazwe pola wpisz");
                ($_POST[$type] != "") or die("Wybierz typ pola");
                ($_POST[$size] != "") or die("Wpisz wielkosc pola");
                $sql.="($_POST[$size]),";
            } 
            $sql .= ")";
 
            print "<B>Zapytanie:</B> $sql <hr>"; 
            $wynik = mysql_query($sql,$moja) or die("Pewnie wpisales size literami. SQL ERROR ;P"); 

            if ($wynik) 
            { 
                print "Tabela '$_POST[table]' utworzona"; 
            } 
        } 

print "<textarea cols=50 name='info'>$sql</textarea><BR><BR>\n";

mysql_close($moja); 

?>
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

What is the exact sql error message you get? And what line?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You should use:

Code: Select all

$wynik = mysql_query($sql,$moja) or die(mysql_error());
To get exact details instead of a custom error!.
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

You can't create a sql query with a for-loop in it. SQL isn't able to perform php code in a query. You can only use a php-variable in the query, but not a complete piece of code.
Post Reply