mkebkr04 Posted November 14, 2008 Share Posted November 14, 2008 I am trying to link my php site to a mySQL database. So far it seems as though everything connects right because when I enter information into the form it says successful. I then check the database and it did indeed create a new row but only of blanks, 1s, 0s, and NULLs. So I am kind of at a stand point right now. <? if (isset($_POST['submit'])) { $business = $_POST['Business_name']; $title = $_POST['Job_title']; $salary = $_POST['Salary']; $cname = $_POST['Business_contact_name']; $cemail = $_POST['Business_contact_email']; $cphone1 = $_POST['Business_contact_phone1']; $cphone2 = $_POST['Business_contact_phone2']; $business_url = $_POST['Business_url']; $intern_url = $_POST['Intern_url']; $fname = $_POST['Faculty_contact_name']; $femail = $_POST['Faculty_contact_email']; $fphone = $_POST['Faculty_contact_phone']; $username = $_POST['Username']; $sdate = $_POST['Internship_start_date']; $edate = $_POST['Internship_end_date']; $exdate = $_POST['Expiration_date']; $sdescription = $_POST['Short_description']; $description = $_POST['Description']; $con = mysql_connect("host", "username", "pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bakerjm", $con); $sql = "INSERT INTO Interns( Business_name, Job_title, Salary, Business_contact_name, Business_contact_email, Business_contact_phone1, Business_contact_phone2, Business_url, Internship_url, Faculty_contact_name, Faculty_contact_email, Faculty_contact_phone, Username, Internship_start_date, Internship_end_date, Expiration_date, Short_description, Description) VALUES('$business','$title','$salary','$cname','$cemail','$cphone1','$cphone2','$business_url','$intern_url','$fname', '$femail','$fphone','$username','$sdate','$edate','$exdate','$sdescription','$description')"; if (!mysql_query($sql, $con)) { die('Error: ' . mysql_error()); } echo "Submitted Successfully"; mysql_close($con); } ?> That is what I am using as of right now. The inputs are mostly text except for a few drop down menus that include dates. Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/ Share on other sites More sharing options...
revraz Posted November 14, 2008 Share Posted November 14, 2008 echo $sql and see what it contains. Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690220 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 This is what it displays after using echo $sql but this is my first php project and I am unsure if this is supposed to be like this or not. I am guessing that VALUES is supposed to actually have variables between the quotation marks? Submitted SuccessfullyINSERT INTO Interns( Business_name, Job_title, Salary, Business_contact_name, Business_contact_email, Business_contact_phone1, Business_contact_phone2, Business_url, Internship_url, Faculty_contact_name, Faculty_contact_email, Faculty_contact_phone, Username, Internship_start_date, Internship_end_date, Expiration_date, Short_description, Description) VALUES('','','','','','','','','','', '','','','','','','','') Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690221 Share on other sites More sharing options...
Jibberish Posted November 14, 2008 Share Posted November 14, 2008 in the <form> tag from the page that sends the data what method are you using? POST or GET? Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690231 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2008 Share Posted November 14, 2008 Either your form is invalid or the names of the form fields do not match what you are using in the php code. At least the submit field in your form is working. Have you checked that the names of your form fields match what you are using in the php code? When learning php, developing php code, or debugging php code, set error_reporting to E_ALL and set display_errors to ON. These settings should be done in your php.ini (stop and start your web server to get any changes made to php.ini to take effect) so that you will also get fatal parse errors to be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690234 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 The form method is post and the fields all match. There is one thing that might be causing it possibly. I have drop down menus for three dates so nine menus total. The format is January 1 2008. So for example the name of one of these dates is Expiration_date, what would I use in this case....DATETIME, just DATE or would I use something else on the mySQL side to take in the date from the PHP side. If this could be the problem would it cause the whole form to fail like it is? Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690247 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2008 Share Posted November 14, 2008 Except for your submit field, none of your form variables have any values. Either your form is invalid or the form field names don't match. You would need to post your form to get any specific help with why it is not submitting data to your form processing code. Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690252 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 The more I look at it the more I think the date part is messed up. <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> <br> <font size="+1">Add New Intern/Job Posting</font> <br><br> Please complete the following fields <br><br> <table cols="2"> <tr> <td align="left" valign="bottom"><p>Name of Business: </p></td> <td align="right" valign="bottom"><p><input type="text" name="business" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Job Title: </p></td> <td align="right" valign="bottom"><p><input type="text" name="title" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Business Contact Name: </p></td> <td align="right" valign="bottom"><p><input type="text" name="contact_name" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Business Contact Email: </p></td> <td align="right" valign="bottom"><p><input type="text" name="contact_email" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Business URL: </p></td> <td align="right" valign="bottom"><p><input type="text" name="business_url" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Internship URL: </p></td> <td align="right" valign="bottom"><p><input type="text" name="intern_url" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Faculty Contact Name: </p></td> <td align="right" valign="bottom"><p><input type="text" name="faculty_name" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Faculty Contact Email: </p></td> <td align="right" valign="bottom"><p><input type="text" name="faculty_email" size="50" maxlength="50" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Submitted by (Username): </p></td> <td align="right" valign="bottom"><p><input type="text" name="username" size="20" maxlength="20" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Business Contact Phone #1: </p></td> <td align="right" valign="bottom"><p><input type="text" name="contact_phone1" size="10" maxlength="10" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Business Contact Phone #2: </p></td> <td align="right" valign="bottom"><p><input type="text" name="contact_phone2" size="10" maxlength="10" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Faculty Contact Phone: </p></td> <td align="right" valign="bottom"><p><input type="text" name="faculty_phone" size="10" maxlength="10" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p>Salary: </p></td> <td align="right" valign="bottom"><p><input type="text" name="salary" size="10" maxlength="10" /></p></td> </tr> <tr> <td align="left" valign="bottom"><p> Internship Start Date: </p></td> <td align="right" valign="bottom"><p> <select name="start_month"> <? for($i=1;$i<=12;$i++) { echo "<option value='$i'>$months[$i]</option>\n"; } ?> </select> <select name="start_day"> <? for($i=1;$i<=31;$i++) { echo "<option value='$i'>$i</option>\n"; } ?> </select> <select name="start_year"> <? $current_year = date("Y"); for($i=$current_year-1;$i<=$current_year+2;$i++) { if( $i == $current_year ) { echo "<option SELECTED value='$i'>$i\n"; } else { echo "<option value='$i'>$i\n"; } } ?> </select> </p></td> </tr> <tr> <td align="left" valign="bottom"><p> Internship End Date: </p></td> <td align="right" valign="bottom"><p> <select name="end_month"> <? for($i=1;$i<=12;$i++) { echo "<option value='$i'>$months[$i]</option>\n"; } ?> </select> <select name="end_day"> <? for($i=1;$i<=31;$i++) { echo "<option value='$i'>$i</option>\n"; } ?> </select> <select name="end_year"> <? $current_year = date("Y"); for($i=$current_year-1;$i<=$current_year+2;$i++) { if( $i == $current_year ) { echo "<option SELECTED value='$i'>$i\n"; } else { echo "<option value='$i'>$i\n"; } } ?> </select> </p></td> </tr> <tr> <td align="left" valign="bottom"><p> Expiration Date: </p></td> <td align="right" valign="bottom"><p> <select name="exp_month"> <? for($i=1;$i<=12;$i++) { echo "<option value='$i'>$months[$i]</option>\n"; } ?> </select> <select name="exp_day"> <? for($i=1;$i<=31;$i++) { echo "<option value='$i'>$i</option>\n"; } ?> </select> <select name="exp_year"> <? $current_year = date("Y"); for($i=$current_year-1;$i<=$current_year+2;$i++) { if( $i == $current_year ) { echo "<option SELECTED value='$i'>$i\n"; } else { echo "<option value='$i'>$i\n"; } } ?> </select> </p></td> </tr> </table> <p> </p> <table> <tr valign="bottom"> <td align="center"><p>Short Description: <textarea name="short_description" rows="2" cols="80"></textarea> </p></td> </tr> <tr valign="bottom"> <td align="center"><p>Description: <textarea name="description" rows="10" cols="80"></textarea> </p></td> </tr> </table> <div align="center"><input type="submit" name="submit" value="Submit" /></div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690260 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2008 Share Posted November 14, 2008 NONE of your form's data fields match the $_POST['....'] variable names you are using. Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690263 Share on other sites More sharing options...
runnerjp Posted November 14, 2008 Share Posted November 14, 2008 and use code tags!! <php echo ' the code tags will make it easyer to read?!?!?! ?> Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690267 Share on other sites More sharing options...
Jibberish Posted November 14, 2008 Share Posted November 14, 2008 the name of the input box has to be the same as the $_POST[]. you have things like <input type="text" name="business" size="50" maxlength="50" /> and the post as <?php $business = $_POST['Business_name']; ?> and it should be <?php $business = $_POST['business']; ?> they have to match in order to get them Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690276 Share on other sites More sharing options...
nitation Posted November 14, 2008 Share Posted November 14, 2008 follow Jibberish last post and i believe your problem will be history!. My 2 cent Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690315 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 Thanks alot it works perfectly now, except for one thing. Say I have start_month, start_day, start_year and I want all three to be put into one column in the database. I tried $exdate = $_POST['exp_month','exp_day','exp_year']; and its complains about the commas?? Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690325 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 Say I have start_month, start_day, start_year and I want all three to be put into one column in the database. I tried $exdate = $_POST['exp_month','exp_day','exp_year']; and its complains about the commas?? Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690367 Share on other sites More sharing options...
wildteen88 Posted November 14, 2008 Share Posted November 14, 2008 Look into using the concatenation operator (.), example $var1 = 'Hello'; $var2 = 'World'; echo $var1 . ', ' . $var2; So with your example you'd use $exdate = $_POST['exp_month'] . $_POST['exp_day'] . $_POST['exp_year']; Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690374 Share on other sites More sharing options...
mkebkr04 Posted November 14, 2008 Author Share Posted November 14, 2008 That definitly makes sense but it doesn't seem to want to work. Do I need some kind of format function or something for the date to go into my SQL. After I hit submit it shows dates as follows: 0000-00-00, so does php accomadate this order or would I need to put it in that order somehow? Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690379 Share on other sites More sharing options...
wildteen88 Posted November 14, 2008 Share Posted November 14, 2008 No this is due to how you have setup your table. You have set your date columns datatype as date which expects the following date format yyyy-mm-dd. However your date format is mmddyyyy. You should change this $exdate = $_POST['exp_month'] . $_POST['exp_day'] . $_POST['exp_year']; to $exdate = sprintf('%d-%02d-%02d', $_POST['exp_year'], $_POST['exp_month'], $_POST['exp_day']); Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-690471 Share on other sites More sharing options...
mkebkr04 Posted November 19, 2008 Author Share Posted November 19, 2008 Thanks, you have all been a lot of help now I am moving from the create.php to the edit.php. Les just say hopefully I learn a lot from this. Probably more questions to come. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/132725-php-to-mysql-connection/#findComment-693090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.