Php mysql\u查询未将表单信息发送到数据库
我似乎不明白为什么我的表单数据没有被发送到数据库。我尝试过多种不同的编码方式,这是我唯一能得到结果的方式 这是我的密码:Php mysql\u查询未将表单信息发送到数据库,php,mysql,database,Php,Mysql,Database,我似乎不明白为什么我的表单数据没有被发送到数据库。我尝试过多种不同的编码方式,这是我唯一能得到结果的方式 这是我的密码: <?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_n
<?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 = '';
}
}
?>
插入失败使用mysqli_查询函数,您可以将连接放在第一位。但是,您使用的是mysql_查询,它将查询放在第一位,连接放在第二位
不过,正如评论中提到的,您确实不应该只是将用户通过$\u POST提交的数据直接插入数据库。评论中也提到,您应该改用mysqli_*。这将使您的语法正确
要将它转换成mysqli,没有很多工作要做。基本上,您只需在每个通常称为mysql的位置的末尾添加一个i。您还需要切换连接和查询,使连接位于第一位,就像您在代码中所做的那样。最后,您的连接现在使用第4个参数而不是3个参数,这很好,因为您不需要单独调用来指定要使用的数据库
下面是一个例子:
<?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'];
}
无需使用连接mysql\u查询功能。 如果您使用的是mysqli_查询函数,则必须在该函数中使用连接参数。 因此,您的问题的解决方案是。
mysql\u查询$insertsql或DIEENSERT失败。mysql\u错误$lrconnect 你的列的类型和长度是什么?你能告诉我们你的数据库连接代码吗$lRConnect不再支持mysql_*函数,它们已经不再维护,将来也会维护。您应该使用或更新您的代码,以确保将来项目的功能。您的代码正在受到影响。您没有在此处关闭括号:{$nolast=;
<?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'];
}