Page 1 of 1

Inserting data in database

Posted: Sun Dec 27, 2009 12:04 am
by dwsddas
Hi

I wrote a code that inserts data in database and it does not work on host server. However, it does work on my localhost. Please help to find what I should change in my script.

Code: Select all

 
<?php 
     
    error_reporting(E_ALL ^ E_NOTICE); 
    session_start(); 
    mysql_connect("localhost","root") or die(mysql_error()); 
    mysql_select_db("database2") or die(mysql_error()); 
     
    if($_SESSION['username']) { 
    $curnum = 0; 
    } else { 
        $curnum++; 
    } 
?> 
    <form method="POST" action="submitp.php"> 
    <table border="0" style="font-size: 15px; font-family: Tahoma; border: 1px solid black;"> 
        <tr> 
                <td> 
                    Text name: 
                </td> 
                <td> 
                    <input type="text" name="textname" value="<?php echo $_POST['textname'];?>" /> 
                </td> 
        </tr> 
        <tr> 
                <td> 
                    Text body: 
                </td> 
                <td> 
                    <TEXTAREA NAME="textbody" ROWS=6 COLS=40 value="<?php echo $_POST['textbody'];?>" /></TEXTAREA> 
                </td> 
        </tr> 
        <tr> 
                <td colspan="2" align="center"> 
                    <input type="submit" name="submit" value="Submit" /> 
                </td> 
        </tr> 
    </table> 
    </form> 
    <?php 
                                         
                         
                         
                    if($_POST['submit']) {                             
                            $textname = $_POST['textname'];     
                            $textbody = $_POST['textbody']; 
                            $username = $_SESSION['username'];  
                             
                            if($curnum == 0) { 
                                mysql_query("INSERT INTO textt VALUES('". $username ."', '" . $textname."' ,'". $textbody."')") or die(mysql_error()); 
                                echo "<font color='green'>'". $username ."', '" . $textname."' ,'". $textbody."'</font>\n"; 
                             }  
                                             
                                             
                             
                             
                    } 
                 
    ?> 
 
 
I inserted following code in my index.php file also. It was not in index.php on my localhost. I think it relates to the problem.

Code: Select all

mysql_query("CREATE TABLE users  
                    (username varchar(15), 
                                PRIMARY KEY (username), 
                    lastname text, 
                    firstname text, 
                    password text) 
                    ENGINE=INNODB; 
                    "); 
  
                    mysql_query("CREATE TABLE textt  
                    (username varchar(15),  
                    textname text, 
                    textbody text, 
                    INDEX IX_username (username), 
                    FOREIGN KEY (username) REFERENCES users (username) 
            ON UPDATE CASCADE) 
                            ENGINE=INNODB; 
                    ");  
 

Re: Inserting data in database

Posted: Sun Dec 27, 2009 1:23 am
by MichaelR
dwsddas wrote:

Code: Select all

...
mysql_connect("localhost","root") or die(mysql_error());
...
You are changing that when running the code on your host's server, right? Because they'll have a different hostname, and no doubt a different username and a password to go with it.

Re: Inserting data in database

Posted: Sun Dec 27, 2009 2:58 am
by dwsddas
Instead of

Code: Select all

1....
2.mysql_connect("localhost","root") or die(mysql_error());
3....

I put information that was provided to me by my host server.

Re: Inserting data in database

Posted: Sun Dec 27, 2009 3:07 am
by daedalus__
please provide the error messages. also it helps to encapsulate names and use caps where appropriate

Code: Select all

 
   mysql_query("CREATE TABLE `text`  
                     (`username` VARCHAR(15),  
                     `textname` TEXT,
                     `textbody` TEXT,
                     INDEX `IX_username` (`username`),
                     FOREIGN KEY (`username`) 
                     REFERENCES `users` (`username`) 
                     ON UPDATE CASCADE)
                     ENGINE=INNODB;
                     ");
 

Re: Inserting data in database

Posted: Sun Dec 27, 2009 4:08 am
by dwsddas
When I made everything like you said, it didn't create tables. So I removed all encapsulations. I can't tell what is error message because my host server doesn't show it.

Re: Inserting data in database

Posted: Sun Dec 27, 2009 2:55 pm
by mellowman
hey what hosting company are you using...because some like godaddy does not let you use local host...you need a specific link to the database