使用php将我的sql数据库上传到我的html表单中

时间:2014-04-30 15:28:59

标签: php html mysql

我需要一些关于这个项目的帮助。我走得很远,没有结果,查询表格有效,但我需要用我的sql表填充它。我只能屏幕拍摄我的sql,我不知道如何把它放在这里。以下是场景:直接将信息上传到查询表

在这种情况下,我将使用我在其中一个项目中创建的查询表单,以及在另一个项目中使用SQL创建的名为查询的表。必须使用MySQL语句将用户响应'上传到查询表中。添加代码以将信息上载到email.php文件(用于从查询表单发送电子邮件的文件)的查询表中。要测试结果是否已上传到表中,请使用if ... else语句显示结果是否已上载到屏幕。添加至少3个查询回复。

注意:确保查询表单的字段名称与创建的值变量名称匹配,以确保将结果上载到表中。如果上传结果出现问题,请先查看字段名称和列出的订单。

这是我的询问.php:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head> 
<body>
<?php
  $name = $_POST ['name'];
  $address = $_POST ['address'];
  $city = $_POST ['city'];
  $state = $_POST ['state'];
  $zip = $_POST ['zip'];
  $phone = $_POST ['phone'];
  $email = $_POST ['email'];
  $major = $_POST ['major'];
  $year = $_POST ['year'];
  $semester = $_POST ['semester'];
  $to="bgoog@mail.nira.edu, ther691@gmail.com"; //an email is sent to your email account and to 
  $message="To Whom It May Concern, <br>";
  $message.="My information is listed below: <br><br>";
  $message.="Name: $name<br>";
  $message.="Address: $address<br>";
  $message.="City: $city<br>";
  $message.="State: $state<br>";
  $message.="Zip: $zip<br>";
  $message.="Phone: $phone<br>";
  $message.="Email: $email<br>";
  $message.="Major: $major<br><br>";
  $message.="I plan to enter in the $semester of $year. <br>";
  $message.="Please assist me in this matter.<br><br>";
  $message.="Respectfully,<br>";
  $message.="B Good<br>";
  $subject="University - Inquiry Form";//The subject of the email is �University - Inquiry Form�
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers .= "From: <therock691@gmail.com>" . "\r\n";//the email should like it was sent from the person contacting University
  $response=mail($to,$subject,$message,$headers);
  if($response)
  {
    Echo "Email sent successfully";
  }
  else //
  {
    Echo "Error Occured while sending email";           
  }
  $hostname = "localhost";
  $username = "vde_bgood";
  $password = "s0123456";
  $database = "vde_bgood";
  $table = "inquiry";
  mysql_connect($hostname, $username, $password) or die("Unable to connect to database");
  mysql_select_db($database) or die ("Unable to select db");
  $fields = "id, name, address, city, state, zip, phone, email, major, semester year";
  $values = "'$id', '$name', '$address', '$city', 'state', 'zip', 'phone', 'email', 'major'. 'semester', 'year'";
  $query = "INSERT INTO $table ($fields) VALUES ($values)";
  $result = mysql_query($query);
  if($result)
    echo "Thank you for completing the Inquiry Form";
  else
    echo "Sorry, this is not currently working";
?>
<br>
<br>
<a href='inquiry.html'>Go back to contact form</a></body>
</html>

HTML文档:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
  function MM_validateForm()
  { //v4.0
    if (document.getElementById)
    {
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (i=0; i<(args.length-2); i+=3)
      {
        test=args[i+2];
        val=document.getElementById(args[i]);
        if (val)
        {
          nm=val.name;
          if ((val=val.value)!="")
          {
            if (test.indexOf('isEmail')!=-1)
            {
              p=val.indexOf('@');
              if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
            }
            else if (test!='R')
            {
              num = parseFloat(val);
              if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
              if (test.indexOf('inRange') != -1)
              {
                p=test.indexOf(':');
                min=test.substring(8,p);
                max=test.substring(p+1);
                if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
              }
            }
          }
          else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
        }
      }
      if (errors) alert('The following error(s) occurred:\n'+errors);
      document.MM_returnValue = (errors == '');
    }
  }
</script>
</head>

<body>
<form action="inquiry.php" method="post" name="form1" id="form1">
  <h2>Nira University Enrollment Form</h2>
    <table width="500" border="1">
      <tr>
        <td width="171"><label for="name">Name:</label></td>
        <td width="313"><input name="name" type="text" id="name" form="form1" title="name"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">Street Address:</label></td>
        <td width="313"><input type="text" name="address" id="address"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">City:</label></td>
        <td width="313"><input type="text" name="city" id="city"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">State:</label></td>
        <td width="313"><input type="text" name="state" id="state"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">Zip Code:</label></td>
        <td width="313"><input type="text" name="zip" id="zip"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">Telephone:</label></td>
        <td width="313"><input name="phone" type="text" id="phone" onBlur="MM_validateForm('name','','R','address','','R','city','','R','state','','R','zipcode','','RisNum','phone','','R','email','','RisEmail','major','','R');return document.MM_returnValue"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">Email Address:</label></td>
        <td width="313"><input type="text" name="email" id="email"></td>
      </tr>
      <tr>
        <td width="171"><label for="name">Intended Major:</label></td>
        <td width="313"><input type="text" name="major" id="major"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><label for="select">Year:</label></td>
        <td><select name="year" id="year" >
          <option value="">Select Year</option>
          <option value="2014">2014</option>
          <option value="2015">2015</option>
          <option value="2016">2016</option>
          <option value="2017">2017</option>
        </select></td>
        <label for="select3"></label>
      <tr>
        <td><label for="select">Semester:</label></td>
        <td width="313"><select name="semester" id="semester"  title="semester">
          <option value="">Select Semester</option>
          <option value="Spring">Spring</option>
          <option value="Summer">Summer</option>
          <option value="Fall">Fall</option>
        </select></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td width="171"><input name="submit" type="submit" id="submit" onClick="MM_validateForm('name','','R','address','','R','city','','R','state','','R','zip','','RisNum','phone','','R','email','','NisEmail','major','','R');return document.MM_returnValue" value="Submit"></td>
      </tr>
    </table>
  </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我甚至不知道你应该问我们什么?但是,如果问题来自您的mysql查询,请尝试:

  $result = mysql_query("INSERT INTO inquiry($fields) VALUES('$values')");

答案 1 :(得分:0)

由于存在一些不一致,您可能需要检查$ fields和$ values变量。您没有在$ fields中使用逗号分隔学期和年份,但是您在$ values中将它们分开。此外,您在主要和学期之间的$值中有句号,而不是逗号。

现在,如果我理解这一点,那么您的表格中包含您的信息,现在想要使用此信息填充表单。您将不得不查询您的数据库并选择信息,然后使用变量或数组在表单上显示它。

$query = "SELECT * FROM table_name";
$result = mysqli_query($con, $query);

$data = mysqli_fetch_array($result);

然后您可以这样使用:

$name = $data['name'];
$address = $data['address'];

然后使用PHP,您可以将这些变量插入您想要显示它们的位置。您可以在此处找到更多信息:http://us1.php.net/manual/en/book.mysqli.php和此处:http://www.tutorialspoint.com/php/mysql_select_php.htm

希望这有帮助。