Inserting data in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwsddas
Forum Newbie
Posts: 8
Joined: Tue Dec 22, 2009 4:44 am

Inserting data in database

Post 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; 
                    ");  
 
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Inserting data in database

Post 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.
dwsddas
Forum Newbie
Posts: 8
Joined: Tue Dec 22, 2009 4:44 am

Re: Inserting data in database

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Inserting data in database

Post 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;
                     ");
 
dwsddas
Forum Newbie
Posts: 8
Joined: Tue Dec 22, 2009 4:44 am

Re: Inserting data in database

Post 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.
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: Inserting data in database

Post 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
Post Reply