ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Data not being written to MySQL DB

<< < (4/4)

kyrathaba:
At this point, I can connect to my server, AND correctly select the desired database. My code even creates the needed table if it's not existent. But there's some little problem that prevents my code from inserting a record. To see the results, go to the following URL and enter a fake first name, last name, and email address and click the submit button:

http://williambryanmiller.com/proofreading/index.html

Below is my PHP Script. The only thing I've altered is to replace the actual database name and password with an asterisk, but we already know my code connects and selects the database.


--- Code: PHP ---<center><a href="index.html">Return to proofreading page</a></center> <?php                        $tablename = 'proofreading_sought';        $echo_on = true;        $user="kyrathab";        $password="*";        $database="*";        $mysql_host = "localhost";                if($_POST['firstname']=='')        {                               ?>                You should <a href="index.html">return</a> to my proofreading page, because                <?php                        die(" you failed to enter data for your first name!");        }        if($_POST['lastname']=='')        {                ?>                You should <a href="index.html">return</a> to my proofreading page, because                <?php                        die(" you failed to enter data for your last name!");        }                if($_POST['email']=='')        {                ?>                You should <a href="index.html">return</a> to my proofreading page, because                <?php                        die(" you failed to enter an email address!");        }                echo 'Hello, ' . $_POST['firstname'] . ' ' . $_POST['lastname'] . '!<p>';        echo 'You specified the following email address: ' . $_POST['email'] . '.';        echo '<p>';        echo "Your information was submitted. Thank you! You should hear back from the proofreader within the next 48 hours. ";        echo "The proofreader will attempt to contact you at the following email address: " . $theMail . '<p>';                        mail("[email protected]","Interested in obtaining proofreading", $msg, 'From:' . $_POST['email']);                $link = mysql_connect($mysql_host, $user, $password);        if (!$link) {                die('Could not connect to host: ' . mysql_error());        }        echo 'Connected to host...<p>';                $db_selected = mysql_select_db($database, $link) or die('Connected to host but could not select database: '     . mysql_error());        echo 'Connected successfully to the database...<p>';                if (!blnTableNameFoundInDatabase($database,$tablename)){                //Let's create the table...        if($echo_on){ echo "Creating table $tablename"; }        $query = "CREATE TABLE $tablename        ( ID int unsigned not null auto_increment primary key,        FirstName char(45) not null, LastName char(45) not null,        Email char(50) not null)";        @mysql_query($query) or Die('Failed to create table $tablename, script dies...' . mysql_error());               $blnJustCreatedTable = True;        }                $query = "INSERT INTO $tablename (FirstName, LastName, Email)                 VALUES ($first, $last, $theMail)";        mysql_query($query) or die('Insertion failed!' . mysql_error() . '<p>');                 $file = 'proofread_seekers.txt';        // The new person to add to the file        $person = $name . '|' . $theMail . '|' . date("Y-m-d H:i:s") . "\n";        // Write the contents to the file,         // using the FILE_APPEND flag to append the content to the end of the file        // and the LOCK_EX flag to prevent anyone else writing to the file at the same time        file_put_contents($file, $person, FILE_APPEND | LOCK_EX);                function blnTableNameFoundInDatabase($the_database, $the_tablename){                $res = mysql_query("SHOW TABLES FROM $the_database");                   $bln = False;                while($row = mysql_fetch_array($res, MYSQL_NUM)){                        if($row[0] == $the_tablename){ $bln = True; }                }        return $bln;}                 ?>
If anyone can test the web form and take a gander at my PHP and figure out why it's producing the error, I'd be much obliged.

Ath:
I'm unable to find the spot in your code where you assign values to $first, $last and $theMail variables. That could IMHO explain the failure to store, as the columns have the 'not null' attribute...

kyrathaba:
Thanks, Ath. That may be what the problem was. I found an online example and worked backward from it, and got it working:

http://williambryanmiller.com/proofreading/needProofreading.php

Navigation

[0] Message Index

[*] Previous page

Go to full version