将表单连接到我的SQL数据库?

时间:2014-03-11 14:16:23

标签: php mysql database

我正在尝试将此表单提交到数据库,但我没有运气。我是一个完整的新手,并将此代码基于另一种已经使用的形式。任何人都可以发现会导致其无法正常工作的错误吗?任何帮助都会很大。

<?php
include("includes/captcha.php");
if (!empty($_POST['submitcontestant'])) { 
$addcontestantsql = "INSERT INTO ".$config['db_dbpaper'].".contest (company, name, address, phone)";
$company = mysql_real_escape_string($_POST['company']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
$captcha = mysql_real_escape_string($_POST['captcha']);
<?php
include("includes/captcha.php");
if (!empty($_POST['submitcontestant'])) { 
$addcontestantsql = "INSERT INTO ".$config['db_dbpaper'].".contest (company, name, address, phone)";
$company = mysql_real_escape_string($_POST['company']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
$captcha = mysql_real_escape_string($_POST['captcha']);

$addcontestantsql .= " VALUES('$company', '$name', '$email', '$phone')";
$allowed = false;
if (empty($company) && empty($name) && empty($address) && empty ($email) && empty($phone)) {
    echo '<strong>Please go back and fill out all the fields!</strong>';
}
elseif ($captcha != $captchaans) {
    echo '<strong>CAPTCHA incorrect!</strong>';
}
else {
    $allowed = true;
    echo '<strong>You are in the contest!</strong>';
}
if ($allowed) {
    db_exec($addcontestantql);
}
}
?>
<?php 
$companysql = "SELECT DISTINCT company FROM ".$config['db_dbpaper'].".contest ";
$mainsql = "SELECT * FROM ".$config['db_dbpaper'].".contest ";
if (!empty($_POST['submit'])) {
$companyname = mysql_real_escape_string($_POST['company']);
$mainsql .= "WHERE company like '$companyname'";
}
$company = db_list($companysql);
$contest = db_list($mainsql);
?>
<div id="fullwidthpage">

<h2>Contest</h2>
<p>Please fill the out form and then click submit. Thank you.</p>
<form method="post">
  <table width="491" border="0">
<tr>
  <td width="107">Company:</td>
  <td width="374"><input name="company" type="text" /></td>
</tr>
<tr>
  <td>Name:</td>
  <td><input name="name" type="text" /></td>
</tr>
<tr>
  <td>Email:</td>
  <td><input name="address" type="text" /></td>
</tr>
<tr>
  <td>Phone:</td>
  <td><input name="phone" type="text" /></td>
    </tr>
  </table>
  <p><?php echo '<img src="'.BASE_URL.'images/captcha/'.date('n').'.jpg">'; ?><br />
          What color is this?:<br />
      <input name="captcha" size="25" style="text-transform: lowercase;"/>
      <input name="submitcontestant" type="submit" value="Submit" />
    </p>
    </form>

2 个答案:

答案 0 :(得分:0)

id建议您从这里开始:http://se2.php.net/manual/en/intro.mysqli.php

此刻您已经偏离轨道并且基本上要求某人为您编码。

帖子更新后

:似乎问题是你根本没有连接到sql尝试使用$ link = mysql_connect(&#39; host&#39;,&#39; mysql_user&#39;, &#39; mysql_password&#39);

答案 1 :(得分:0)

所有设置,我在错误的数据库中创建了数据表,在适当的数据库中添加了新表,并且都更好:)。感谢所有给我意见的人!