简体   繁体   English

mysql_query 不向数据库发送表单信息

[英]mysql_query not sending form information to database

I can't seem to figure out why my form data isn't being sent to the database.我似乎无法弄清楚为什么我的表单数据没有被发送到数据库。 I've tried multiple variations of coding and this is the only one where I could get a "result"我尝试了多种编码变体,这是唯一可以得到“结果”的变体

here's my code:这是我的代码:

<?php
  #if the submit button has be selected...
  if(isset($_POST['submit_registration'])) {
  # assign variables to each form control to capture the values
    $first = $_POST['first_name'];
    $last = $_POST['last_name'];
    $email = $_POST['email'];
    $address1 = $_POST['address1'];
    $address2 = $_POST['address2'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipcode = $_POST['zipcode'];
    $phone = $_POST['phone'];
    $distance = $_POST['distance'];
  # assign null to values for use with isset function to identitfy required         fields with no value
    $nofirst = null;
    $nolast = null;
    $noemail = null;
    $noaddress1 = null;
    $noaddress2 = '';
    $nocityErr = null;
    $nostate = null;
    $nozipcode = null;
    $nophone = null;
    $nodistance = null;
  # if value of variable for required field is nothing, assign something other than null to $no variable
    if($first == "") {$nofirst = '';}
    if($last == "") {$nolast = '';           if($email == "") {$noemail = '';}
    if($address1 == "") {$noaddress1 = '';}
    if($address2 == "") {$noaddress2 ='';}
    if($city == "") {$nocity = '';}
    if($state == "") {$nostate = '';}
    if($zipcode == "") {$nozipcode = '';}
    if($phone == "") {$nophone = '';}
    else {
      $insertsql = "INSERT INTO `runner`(`fname`, `lname`, `email`, `address1`, `address2`, `city`, `state`, `postalcode`, `phone`, `distance`)
                VALUES ('$first','$last','$email','$address1','$address2','$city','$state','$zipcode','$phone','$distance')";
          echo $insertsql;
      mysql_query($lrconnect, $insertsql) or die("Insert failed ". mysql_error($lrconnect));
        echo "connected";
      $inserted = '';
    }
}
?>

Here's my the error I get:这是我得到的错误:

INSERT INTO `runner`(`fname`, `lname`, `email`, `address1`, `address2`, `city`, `state`, `postalcode`, `phone`, `distance`) VALUES ('Crystal','Yang','cykher@gmail.com','55555 Avenue','','Chicago','FL','39485','5555555555','5K')

Insert failed插入失败

Using mysqli_query functions, you would put the connection first as you have.使用mysqli_query函数,您可以将连接放在首位。 However, you are using mysql_query instead which puts the query first and the connection second.但是,您使用的是mysql_query ,它将查询放在第一位,然后将连接放在第二位。

http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-query.php

As mentioned in the comments, though, you really should not be just inserting data that users submit through $_POST directly into the database.但是,正如评论中提到的,您真的不应该只是将用户通过$_POST提交的数据直接插入到数据库中。 Also mentioned in the comments, you should be using mysqli_* instead.评论中还提到,您应该改用mysqli_* This would have made your syntax correct.这将使您的语法正确。

There's not a lot of work to do to convert it to mysqli .将其转换为mysqli没有太多工作要做。 Basically you just add an i on to the end of each place where you'd normally call mysql .基本上,您只需在通常调用mysql的每个位置的末尾添加一个i You'd also switch the connection and query to have the connection come first, as you have in your code.您还可以切换连接和查询以使连接先出现,就像您在代码中所做的那样。 Finally, your connection takes a 4th parameter now instead of 3, which is nice because you don't need a separate call to specify which database you want to use.最后,您的连接现在采用第 4 个参数而不是 3 个参数,这很好,因为您不需要单独调用来指定要使用的数据库。

Here's an example:下面是一个例子:

<?php

$link = mysqli_connect('HOST', 'USER', 'PASS', 'DATABASE');

$q_test = "SELECT id FROM table";

$r_test = mysqli_query($link, $q_test) or trigger_error("Cannot Get ID: (".mysqli_error().")", E_USER_ERROR);

while ($row_test = mysqli_fetch_array($r_test)) {

    print "ID: ".$row_test['id'];

}

No need to use connection with mysql_query function.无需使用与 mysql_query 函数的连接。
If you are using mysqli_query function than you must have to use connection parameter in that function.如果您使用 mysqli_query 函数,则必须在该函数中使用连接参数。 so solution of your problem is.所以你的问题的解决方案是。
mysql_query($insertsql) or die("Insert failed ". mysql_error($lrconnect)); mysql_query($insertsql) 或 die("插入失败"。mysql_error($lrconnect));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM