PHP Script not executing SQL to create tables

Discussion in 'PHP' started by Scoty, Dec 30, 2008.

  1. #1
    I will show both the full page and where I believe where the problem is coming from, I have tried injecting the SQL through phpMyAdmin and it has all executed successfully so I'm positive that's not the problem.

    Here is where I believe to be the problem:

    mysql_query( $sql, $con ) or die("Error: Creating table <b>users</b>:". mysql_error());
    mysql_query( $sql2, $con ) or die("Error: Creating table <b>ep</b> (episode):". mysql_error());
    mysql_query( $sql3, $con ) or die("Error: Creating table <b>category</b>:". mysql_error());
    PHP:
    I previously had it like:
    But read somewhere to have it like that, either way, the same problem exists

    And here is the full page:

    <?php
    
    $idbhost = mysql_real_escape_string($_POST['dbhost']);
    $idbname = mysql_real_escape_string($_POST['dbname']);
    $idbuser = mysql_real_escape_string($_POST['dbuser']);
    $idbpass = mysql_real_escape_string($_POST['dbpass']);
    $npw = $_POST['np1'];
    $npw2 = $_POST['np2'];
    $user = $_POST['ScoUser'];
    $dn = $_POST['displayname'];
    echo "Database Name:";
    echo $idbname;
    ?>
    <br />
    <?php
    mysql_connect($idbhost, $idbuser, $idbpass) or die
                            ("Error connecting to mysql: ". mysql_error());
    
    mysql_select_db($idbname);
    
    include 'createconfig.php';
    
    // this checks to see the passwords the user entered are the same
    if (isset($npw)) {
        if ($npw == $npw2) {
    function npwform($npw){
      return md5('[salt removed for security]'. $npw);
    }
    $p = npwform($npw);
    
    // this creates the users table to store login info
    $sql = " CREATE TABLE `users` (
    `id` INT NOT NULL ,
    `user` VARCHAR( 32 ) NOT NULL ,
    `pass` VARCHAR( 32 ) NOT NULL ,
    `name` VARCHAR( 128 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = InnoDB ";
    
    // this creates the ep (episode) table to store episode data
    $sql2 = " CREATE TABLE `ep` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `category` VARCHAR( 6 ) NOT NULL ,
    `episodenum` INT NOT NULL ,
    `production` VARCHAR( 10 ) NOT NULL ,
    `release` INT NOT NULL ,
    `season` INT NOT NULL ,
    `episode` INT NOT NULL ,
    `title` VARCHAR( 100 ) NOT NULL ,
    `releasedate` VARCHAR( 10 ) NOT NULL ,
    `description` VARCHAR( 1024 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = InnoDB ";
    
    // this creates the category table to store category data
    $sql3 = " CREATE TABLE `category` (
    `id` INT NOT NULL ,
    `show` VARCHAR( 30 ) NOT NULL ,
    `code` VARCHAR( 6 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ";
    mysql_query( $sql, $con ) or die("Error: Creating table <b>users</b>:". mysql_error());
    mysql_query( $sql2, $con ) or die("Error: Creating table <b>ep</b> (episode):". mysql_error());
    mysql_query( $sql3, $con ) or die("Error: Creating table <b>category</b>:". mysql_error());
    mysql_query( "INSERT INTO `users` (
    `id` ,
    `user` ,
    `pass` ,
    `name`
    )
    VALUES (
    '', '$user', '$p', '$dn'
    )", $con ) or die("Error inserting into database:". mysql_error());
    unset($npw);
    unset($npw2);
        } else {
    	echo "Error: Passwords do not match";
    	}
    }
    
    echo "\nProcessing completed. If there are no errors, it was successful. If there were, please go back ";
    ?>
    <a href="index.php">here</a>
    <?php
    echo "and input your information again.";
    ?>
    PHP:
    If it helps, here is the code for the page to input the data:

    <html>
    <head>
    <title>ScoCMS</title>
    <link href="../admin/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    
    <form action="process.php" method="post">
    <table>
    <tr>
    <td>
    Host:</td><td>
    <input type="text" name="dbhost" size="20" value="localhost" /></td></tr>
    <tr>
    <td>
    Database Name:</td><td>
    <input type="text" name="dbname" size="20" /></td></tr>
    <tr>
    <td>
    MySQL Username:</td><td>
    <input type="text" name="dbuser" size="20" /></td></tr>
    <tr><td>MySQL Password:</td><td>
    <input type="password" name="dbpass" size="20" /></td></tr>
    <tr><td>ScoCMS Username:</td><td><input type="text" name="ScoUser" value="admin" size="20" /></td></tr>
    <tr><td>ScoCMS Password:</td>
    <td><input type="password" name="pw1" size="20" maxlength="32" /></td></tr>
    <tr><td></td><td><input type="password" name="pw2" size="20" maxlength="32" /></td></tr>
    <tr><td>ScoCMS Displayname:</td><td><input type="text" name="displayname" size="20" /></td></tr>
    <tr><td></td><td><input type="submit" value="Submit"></td></tr>
    </table>
    </form>
    
    </body>
    </html>
    
    HTML:
    I just can't get it to create the tables in the database from the script, there must be something I'm missing or overlooking, I don't know a lot about PHP/MySQL, on top of that I havn't coded for a couple of months so I don't understand my own project so much lol

    Any help much appreciated,
    Thanks,
    Scot
     
    Scoty, Dec 30, 2008 IP
  2. champ

    champ Member

    Messages:
    30
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Does the mysql user your script use have the CREATE privilege? It may only have SELECT, INSERT, UPDATE, DELETE privileges. Your phpMyAdmin is probably using a different mysql user (ie, root) for its connection that has all privileges.
     
    champ, Dec 30, 2008 IP
  3. Scoty

    Scoty Active Member

    Messages:
    620
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks for your reply, the user I'm using is set to have all privileges, I've just tried using the root and it's the same problem so there's either something wrong with my server (using WAMP) or I'm just not using mysql_query or something else in the right way.

    I'll try uploading to my webhost and see if I can get it to work on there..

    Also someone pointed out this:
    I havn't defined $con, so have removed that to what it was before but it's still not working, I've also tried defining $con

    EDIT: Ok the web serv is throwing up 500 error when executing process.php lol
     
    Scoty, Dec 31, 2008 IP
  4. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #4
    Make sure the variables $sql, $sql1 and $sql2 don't contain multiple queries...
     
    nabil_kadimi, Dec 31, 2008 IP