PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Aug 10, 2020 1:58 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Mon Jun 09, 2003 9:36 pm 
Offline
Forum Newbie
User avatar

Joined: Mon Jun 09, 2003 9:36 pm
Posts: 21


Last edited by stc7outlaw on Tue Jun 10, 2003 10:42 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2003 3:33 am 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
Hi, start by reading:
http://forums.devnetwork.net/viewtopic.php?t=8815
http://forums.devnetwork.net/viewtopic.php?t=511
http://php.net/language.operators.logical.php (for info on how to have multiple statements in an if statement)
http://www.mysql.com/doc/en/CREATE_TABLE.html (how to create a table in MySQL)

There are a couple of important things to note about why your code may not have been working, firstly by having the mysql_select_db() call within this if statement block:
Syntax: [ Download ] [ Hide ]
if ( ! $link ) {

$dberror = "Couldn't connect to MySQL server";

return false;

mysql_select_db($db);

}

it would not run.

Secondly, you need to take care with variable names - you go from using $tbl_exists to using $tbl_exist which, well, doesn't exist.

Try this code, it's not entirely complete (check the comments for more info) but will hopefully help:
Syntax: [ Download ] [ Hide ]
<html>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<BR>Enter the Taco Bell Store Number

<BR><input type="text" name="storenumber">

<BR>Enter Your Name

<BR><input type="text" name="name">

<BR>Enter The Size

<BR><input type="text" name="size">

<BR>Enter Your Phone Number

<BR><input type="text" name="phone">

<input type="submit" value="Load Store">

<input type="reset">

</form>

<?php



if (!empty($_POST['storenumber']) && !empty($_POST['name']) && !empty($_POST['size']) && !empty($_POST['phone'])) {

        $user = 'oprods';

        $pass = 'breakin';

        $db = 'storedbtest';



        $storenumber = $_POST['storenumber'];

        $name = $_POST['name'];

        $size = $_POST['size'];

        $phone = $_POST['phone'];



        @$link = mysql_connect('localhost', $user, $pass) or die('<p>Couldn''t connect to MySQL server.</p>');

        @mysql_select_db($db) or die('<p>Could not select '.$db.' database');



        $sql = "SHOW TABLES LIKE '$storenumber'";

        @$tbl_exists = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');



        if (mysql_num_rows($tbl_exists) == 1) {

                echo '<p>Store database already exists.</p>';

        } else {

                // you're going to have to modify the size of each field to fit your requirements

                $sql = "CREATE TABLE $storenumber (storenumber INT(5), name VARCHAR(50), size VARCHAR(50), phone VARCHAR(20))";

                @mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

                echo '<p>Store database created.</p>';

        }



        // you need to do a lot more validation of data before putting it into the database

        // e.g. you should definitely look at using is_numeric() to check that the store number

        // is a number

        // depending on your PHP setup you may also have to use addslashes() on all your data

        $sql = "INSERT INTO $storenumber (storenumber, name, size, phone) VALUES('$storenumber', '$name', '$size', '$phone')";

        @mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

        echo '<p>Database info complete.</p>';

} else {

        echo '<p>Please enter some data.</p>';

}

?>

</body>

</html>

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2003 10:01 am 
Offline
Forum Newbie
User avatar

Joined: Mon Jun 09, 2003 9:36 pm
Posts: 21
thank you very much for the help

but when i run that revised code i get a parse error on line 53. Does it have somethign to do with the $sql variable?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2003 2:17 pm 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
That's strange - I ran the code to check it worked before posting it and didn't get any parse errors. I'll test again.

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2003 2:21 pm 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
I think I see why there might be a problem - the forum has eaten one of my closing PHP tags:
Syntax: [ Download ] [ Hide ]
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ">

should be:
Syntax: [ Download ] [ Hide ]
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ? >">

(with no space between the ? and the &gt;) and
Syntax: [ Download ] [ Hide ]
} else {

   echo '<p>Please enter some data.</p>';

}



</body>

</html>

should be
Syntax: [ Download ] [ Hide ]
} else {

   echo '<p>Please enter some data.</p>';

}



? > // with no space between the ? and >



</body>

</html>

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2003 4:23 pm 
Offline
Forum Newbie
User avatar

Joined: Mon Jun 09, 2003 9:36 pm
Posts: 21


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2003 3:04 am 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
I think that the problem is that you can't have a table name that starts with a digit (or one which is only digits). Perhaps it would be better to call the table 'store_66666' and adjust the SQL statements accordingly, so instead of:
Syntax: [ Download ] [ Hide ]
$sql = "SHOW TABLES LIKE '$storenumber'";

you would have:
Syntax: [ Download ] [ Hide ]
$sql = "SHOW TABLES LIKE 'store_$storenumber'";

and instead of
Syntax: [ Download ] [ Hide ]
$sql = "CREATE TABLE $storenumber (storenumber INT(5), name CHAR(50), size CHAR(50), phone CHAR(20))";

you would have
Syntax: [ Download ] [ Hide ]
$sql = "CREATE TABLE `store_$storenumber` (storenumber INT(5), name CHAR(50), size CHAR(50), phone CHAR(20))";

(note that those are backticks ` around the table name not single quotes ')

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2003 3:14 am 
Offline
Forum Regular

Joined: Thu Apr 24, 2003 6:51 am
Posts: 416
Location: The Netherlands


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2003 3:36 am 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2003 3:50 am 
Offline
Forum Regular

Joined: Thu Apr 24, 2003 6:51 am
Posts: 416
Location: The Netherlands
Thnx for looking it up.

I will advise, never to start with a digit.
But that will be your advise to i think :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2003 6:01 am 
Offline
Forum Newbie
User avatar

Joined: Mon Jun 09, 2003 9:36 pm
Posts: 21
bless your souls it works


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group