如何将php表单与mysql数据库链接?

时间:2015-11-22 10:27:16

标签: php mysql

我想将我的php表单与mysql数据库链接,后者在数据库中提交信息。没有出现错误,但数据没有存储在反手中。 这是我的HTML代码。

    <form method="POST" action="feedback.php">
          <div class="form-group">
    <label for="InputName">Name</label>
    <input type="text" class="form-control" id="InputName" name = "name" placeholder="enter name">
  </div>
  <div class="form-group">
    <label for="InputEmail1">Email</label>
    <input type="email" class="form-control" id="InputEmail1" name="emailid" placeholder="">
  </div>
  <div class="form-group">
    <label for="Inputnumber">Contact Number</label>
    <input type="Number" class="form-control" id="InputPassword1"  name  = "no" placeholder="">
  </div>
  <div class="form-group">
    <label for="inputtext3">Nature of Feedback</label>
    <select class="form-control" name = "nature">
    <option>      </option>
    <option>Query</option>
    <option>Complaint</option>
    <option>Suggestion</option>
    </select>
</div>
 <div class="form-group">
    <label for="contactmessage">Feedback</label>
    <textarea class="form-control" id="contactmessage" name  = "feedback"></textarea>

  </div>
  <div class="radio">
  <label for="How would you rate our customer support?" class="control-label">How would you rate our customer support?</label>
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
    Excellent
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
   Good
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3">
    Fair
  </label>
  <div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3">
    Poor
  </label>
</div>
 <div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3">
    Terrible
  </label>
</div>

  <button type="submit" class="btn btn-danger" name="submit">Submit</button>
</form>

这是我的PHP代码。

<?php
if(isset($_POST['Submit']))
{
$link = mysqli_connect('localhost','root','','shree rathnam cafetaria') or        die("Unable to connect");
$custname = $_POST['name'];
$custemail = $_POST['emailid'];
$custphone  = $_POST['no'];
$ftype = $_POST['nature'];
$feedback = $_POST['feedback'];
$rating  = $_POST['optionsRadios'];
$query = 'INSERT INTO cust_feedback(Name,email,Phone,nature_of_feedback,feedback,rating) VALUES($custname,$custemail,$custphone,$ftype,$feedback,$rating)';
$result = mysqli_query($link,$query) or die(mysqli_error($link));
echo "Thanku for your feedback!";
}
?>

3 个答案:

答案 0 :(得分:0)

你没有用引号封装sql中的值,它们是字符串..

<?php
    if( isset( $_POST['Submit'] ) ){

        $link = mysqli_connect('localhost','root','','shree rathnam cafetaria') or die("Unable to connect");

        $custname = $_POST['name'];
        $custemail = $_POST['emailid'];
        $custphone  = $_POST['no'];
        $ftype = $_POST['nature'];
        $feedback = $_POST['feedback'];
        $rating  = $_POST['optionsRadios'];

        $query = 'INSERT INTO `cust_feedback` (`Name`,`email`,`Phone`,`nature_of_feedback`,`feedback`,`rating`) VALUES("$custname","$custemail","$custphone","$ftype","$feedback","$rating")';
        $result = mysqli_query( $link,$query) or die( mysqli_error( $link ) );

        echo "Thanku for your feedback!";
    }
?>

答案 1 :(得分:0)

在您的代码中if(isset($_POST['Submit']))尝试检查来自您表单的HTTP feedback.php请求调用的POST。不幸的是,您的提交按钮<button type="submit" class="btn btn-danger" name="submit">Submit</button>有名称&#34;提交&#34;小写不是&#34;提交&#34;标题案例。

只需将您的feedback.php更正为:

<?php
if(isset($_POST['submit']))
{
$link = mysqli_connect('localhost','root','','shree rathnam cafetaria') or        die("Unable to connect");
$custname = $_POST['name'];
$custemail = $_POST['emailid'];
$custphone  = $_POST['no'];
$ftype = $_POST['nature'];
$feedback = $_POST['feedback'];
$rating  = $_POST['optionsRadios'];
$query = 'INSERT INTO cust_feedback(Name,email,Phone,nature_of_feedback,feedback,rating) VALUES($custname,$custemail,$custphone,$ftype,$feedback,$rating)';
$result = mysqli_query($link,$query) or die(mysqli_error($link));
echo "Thanku for your feedback!";
}
?>

答案 2 :(得分:0)

您的代码容易受到SQL注入攻击。我建议您使用BananaDB将PHP与MySQL连接:https://github.com/LaNsHoR/BananaDB