Jump to content

PHP to mySQL Connection


mkebkr04

Recommended Posts

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.

Link to comment
Share on other sites

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('','','','','','','','','','', '','','','','','','','')

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.