使用PHP将HTML表单插入MySQL

时间:2019-03-11 04:03:09

标签: php html mysql

我目前正在学习PHP和MySQL。我能够弄清楚如何将表单的值插入数据库。但是,单选按钮的值没有通过!知道为什么吗?

我注意到name = contact_by,但是它没有数组括号。这是问题吗?还是我想念其他东西?这是我唯一的单选按钮。

对不起,我对此很陌生。非常感谢您的任何建议。

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Freelance - Contact Form</title>

</head>
<body bgcolor="#FFFFFF">
    
    
  <p>
  </p><form action="dbconnect.php" method="post" >

  <p align="center">
  <font face="Verdana, Arial, Helvetica" size="2" color="white"> </font> 
  <table border="0" width="600" cellpadding="10" align="center">
    <tbody><tr> 
      <td colspan="4" height="111"> 
        <div align="center">Logo Here</div>
      </td>
    </tr>
    <tr bgcolor="#CC0000"> 
      <td colspan="4"> 
        <div align="center"><font face="Verdana, Arial, Helvetica" size="2" color="white"> 
          <font size="1">TELL US ABOUT YOURSELF</font></font></div>
      </td>
    </tr>
    <tr> 
      <td align="right" valign="middle" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1"> NAME:</font></font></td>
      <td align="center" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="name" id="name" size="20" maxlength="40" required>
        </font></td>
      <td align="right" valign="middle" width="89" bgcolor="#999999"><font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">COMPANY:</font></font></td>
      <td align="center" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="company" id="company" size="20" maxlength="40" required>
        </font></td>
    </tr>
    <tr> 
      <td align="right" valign="middle" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1"> EMAIL:</font></font></td>
      <td align="center" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="email" id="email" size="20" maxlength="40" required>
        </font></td>
      <td align="right" valign="middle" width="89" bgcolor="#999999"><font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">PHONE:</font></font></td>
      <td align="center" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="phone" id="phone" size="20" maxlength="40" required>
        </font></td>
    </tr>
    <tr> 
      <td align="right" valign="middle" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1"> YOUR URL:</font></font></td>
      <td align="center" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="URL" size="20" maxlength="40" required>
        </font></td>
      <td align="right" valign="middle" width="89" bgcolor="#999999"><font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">CITY:</font></font></td>
      <td align="center" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="city" id="city" size="20" maxlength="40" required>
        </font></td>
    </tr>
    <tr align="left" valign="middle"> 
      <td align="right" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1"> STATE/<br>
        COUNTRY:</font></font></td>
      <td width="156" align="center" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="state" type="text" id="state" size="20" maxlength="200" required>
        </font></td>
      <td align="right" width="89" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1"> BEST WAY TO CONTACT YOU:</font></font></td>
      <td width="204" align="center" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="contact_by" type="radio" id="contact_by" value="Contact Phone" required>
        <font size="1"> PHONE</font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="radio" name="contact_by" value="Contact Email" required>
        <font size="1">EMAIL</font><font face="Verdana, Arial, Helvetica" size="2"></font></font></font></td>
    </tr>
    <tr bgcolor="#CC0000"> 
      <td colspan="4"> 
        <div align="center"><font face="Verdana, Arial, Helvetica" size="2" color="white"> 
          <font size="1">WHAT SORT OF SERVICES DO YOU NEED?</font></font></div>
      </td>
    </tr>
    <tr> 
      <td align="right" valign="top" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">WEBSITE:</font></font></td>
      <td align="left" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="web[]" value="Site Design">
        <font size="1">SITE DESIGN</font><font face="Verdana, Arial, Helvetica" size="2" ><br>
        <input type="checkbox" name="web[]" value="Redesign" >
        <font size="1">SITE REDESIGN</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="web[]" value="Store" >
        <font size="1">ONLINE STORE<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="web[]" value="Marketing" >
        <font size="1">MARKETING<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="web[]" value="Maintenance" >
        <font size="1">MAINTENANCE</font></font></font></font></font></font></td>
      <td align="right" valign="top" width="89" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">PROGRAMMING:</font></font></td>
      <td align="left" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="programming[]" value="HTML/DHTML" >
        <font size="1">HTML/DHTML</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="programming[]" value="PERL/CGI" >
        <font size="1">CGI/PERL/C/C++</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="programming[]" value="Java" >
        <font size="1">JAVA<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="programming[]" value="ASP" >
        <font size="1">ASP<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="programming[]" value="XML" >
        <font size="1">XML</font></font></font></font></font><font size="1"> </font></font></td>
    </tr>
    <tr> 
      <td align="right" valign="top" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">DATABASE:</font></font></td>
      <td align="left" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="database[]" value="Access" >
        <font size="1">ACCESS</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="database[]" value="SQL" >
        <font size="1">SQL</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="database[]" value="Sybase" >
        <font size="1">SYBASE<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="database[]" value="Oracle" >
        <font size="1">ORACLE</font></font></font></font><font size="1"> </font></font></td>
      <td align="right" valign="top" width="89" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">MULTIMEDIA:</font></font></td>
      <td align="left" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="multimedia[]" value="Flash" >
        <font size="1">FLASH</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="multimedia[]" value="Quicktime" >
        <font size="1">QUICKTIME</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="multimedia[]" value="Media Player" >
        <font size="1">MEDIA PLAYER<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="multimedia[]" value="Real" >
        <font size="1">REAL</font></font></font></font><font size="1"> </font></font></td>
    </tr>
    <tr> 
      <td align="right" valign="top" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">CORPORATE DESIGN:</font></font></td>
      <td align="left" valign="middle" width="156" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="corporate[]" value="Marketing Collateral" >
        <font size="1">MARKETING </font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="corporate[]" value="Logos" >
        <font size="1">LOGOS</font><font face="Verdana, Arial, Helvetica" size="2"><br>
        <input type="checkbox" name="corporate[]" value="Packaging" >
        <font size="1">PACKAGING<br>
        </font><font face="Verdana, Arial, Helvetica" size="2"> 
        <input type="checkbox" name="corporate[]" value="Annual Reports" >
        <font size="1">ANNUAL REPORTS</font></font></font></font><font size="1"> 
        </font></font></td>
      <td align="right" valign="middle" width="89" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#000000"> 
        <font size="1">OTHER:</font></font></td>
      <td align="left" valign="middle" width="204" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="other" id="other" size="20" maxlength="40" >
        </font></td>
    </tr>
    <tr bgcolor="#CC0000"> 
      <td colspan="4"> 
        <div align="center"><font face="Verdana, Arial, Helvetica" size="2" color="white"> 
          <font size="1">ADDITIONAL COMMENTS</font></font></div>
      </td>
    </tr>
    <tr> 
      <td align="right" valign="middle" width="72" bgcolor="#999999"> <font face="Verdana, Arial, Helvetica" size="2" color="#FFFFFF"> 
        <font size="1"> <font color="#000000">COMMENTS:</font></font></font></td>
      <td align="left" valign="middle" colspan="3" bgcolor="#CCCCCC"><font face="Verdana, Arial, Helvetica" size="2"> 
        <input name="comments" id="comments" size="60" maxlength="100">
        </font></td>
    </tr>
    <tr> 
      <td valign="middle" colspan="3" align="right" bgcolor="#CC0000"> 
        <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"></font> 
          <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#FFFFFF">SUBMIT 
          FORM &gt;&gt;&gt;</font> </div>
      </td>
      <td valign="top" align="right" bgcolor="#CC0000"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"> 
        <input name="send" type="submit" id="send" value="SEND">
        <input type="reset" value="RESET" onclick="resetform()" name="reset">
        </font></td>
    </tr>
  </tbody></table>
</p></form>



</body></html>

<?php

//Create the variables
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$url = $_POST['URL'];
$city = $_POST['city'];
$state = $_POST['state'];
$contact = $_POST['contact_by'];
$web = implode(",",$_POST['web']);
$programming = implode(",",$_POST['programming']);
$dbservices = implode(",",$_POST['database']);
$multimedia = implode(",",$_POST['multimedia']);
$corporate = implode(",",$_POST['corporate']);
$other = $_POST['other'];
$comments = $_POST['comments'];


//Execute the query

$dbc = mysqli_connect('localhost', 'root', '', 'webd153');

//Insert into db table
$query = "INSERT INTO clients (id, Name, Company, Email, Phone, URL, City, StateCountry, Contact, Website, Programming, DatabaseServices, Multimedia, CorporateDesign, Other, Comments) VALUES
(0, '$name', '$company', '$email', '$phone', '$url', '$city', '$state', '$contact', '$web', '$programming', '$dbservices', '$multimedia', '$corporate', '$other', '$comments')";

//Check query
if (@mysqli_query($dbc, $query)) {
	print '<p>Your information has been submitted!</p>';
} else {
	print '<p style="color: red;">Could not submit information because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}

mysqli_close($dbc);
?>

0 个答案:

没有答案